linux系统http服务器部署
Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。同时Apache音译为阿帕奇,是北美印第安人的一个部落,叫阿帕奇族,在美国的西南部。也是一个基金会的名称、一种武装直升机等等。
apache 俄罗斯 稳定 同步阻塞 认证多
nginx 美国 高效 异步非阻塞 门户式 没有很多用户
一、安装 apache
[[email protected] ~]# rpm -e httpd php php-mysql #卸载http、php、php-mysql
[[email protected] ~]# rm -fr /var/www #删除apache共享目录/var/www
以防之前做的实验对本实验影响
[[email protected] ~]# yum install httpd -y #安装阿帕奇
[[email protected] ~]# systemctl start httpd #开启http服务
[[email protected] ~]# netstat -antlupe | grep httpd #查看http监听端口(80)
tcp6 0 0 :::80 :::* LISTEN 0 393259 3939/httpd
二、指定默认发布目录及文件
[[email protected] ~]# cd /var/www/html #默认发布目录[[email protected] html]# vim index.html #默认发布文件
[[email protected] html]# systemctl stop firewalld
浏览器测试:
172.25.254.102
[[email protected] html]# vim test.html
浏览器测试:
172.25.254.102/test.html
1、修改访问端口
默认端口是80,修改访问端口为8080
[[email protected] html]# vim /etc/httpd/conf/httpd.conf #编辑http主配置文件
[[email protected] html]# systemctl restart httpd #重启服务
[[email protected] html]# netstat -antlupe | grep httpd #查看http端口
tcp6 0 0 :::8080 :::* LISTEN 0 397448 4222/httpd
浏览器测试:
172.25.254.102 测不到
http://172.25.254.102:8080/
2、修改默认发布目录
[[email protected] html]# vim /etc/httpd/conf/httpd.conf
[[email protected] html]# systemctl restart httpd
[[email protected] html]# mkdir -p /westos/html #建立目录/westos/html
[[email protected] html]# cd /westos/html
[[email protected] html]# vim index.html #编辑默认发布文件
[[email protected] html]# vim /etc/httpd/conf/httpd.conf
[[email protected] html]# systemctl restart httpd
浏览器测试:
172.25.254.102
3、修改默认发布文件
[[email protected] html]# vim test.html
[[email protected] html]# vim /etc/httpd/conf/httpd.conf
[[email protected] html]# systemctl restart httpd
172.25.254.102
4、添加发布目录及文件
[[email protected] html]# mkdir linux
[[email protected] html]# cd linux/
[[email protected] linux]# vim index.html
[[email protected] linux]# vim test.html
[[email protected] linux]# vim /etc/httpd/conf/httpd.conf
120 DocumentROOT "/westos/html"
121 <Directory "/westos/html/linux">
122 DirectoryIndex index.html
123 </Directory>
124 <Directory "/westos/html">
125 require all granted
126 DirectoryIndex test.html
127 </Directory>
[[email protected] linux]# systemctl start httpd
172.25.254.102/linux
三、访问控制
1、基于ip的访问控制
[[email protected] ~]# cd /var/www/html
[[email protected] html]# mkdir westos
[[email protected] html]# cd westos/
[[email protected] westos]# vim index.html
[[email protected] westos]# vim /etc/httpd/conf/httpd.conf
删掉之前写的
118 #
119 DocumentRoot "/var/www/html"
120 <Directory "/var/www/html/westos">
121 Order Allow,Deny #顺序(若allow写在前面,则先读取allow行,在读取deny行)
122 Allow from All #允许所有人访问
123 Deny from 172.25.254.102 #禁止172.25.254.102这台主机访问
124 </Directory>
[[email protected] westos]# systemctl restart httpd
172.25.254.102/westos
用ip为172.25.254.102这台主机测试:
其他主机测试:
121 Order Deny,Allow #读取顺序
122 Allow from 172.25.254.102
123 Deny from All
表示禁止所有用户访问,只有172.25.254.102这台主机访问
2、基于用户的访问控制
[[email protected] ~]# cd /etc/httpd/
[[email protected] httpd]# htpasswd -cm apacheuser admin
#c:creat,没有apacheuser自动建立 m:建立admin 如果已经有apacheuser,-c会覆盖
New password:
Re-type new password:
Adding password for user admin
[[email protected] httpd]# cat apacheuser
admin:$apr1$eNoRsMz8$GzuuMPy4DiZIwRYUTlhyN/
[[email protected] httpd]# htpasswd -m apacheuser tom
New password:
Re-type new password:
Adding password for user tom
[[email protected] httpd]# cat apacheuser
admin:$apr1$eNoRsMz8$GzuuMPy4DiZIwRYUTlhyN/
tom:$apr1$7IgxBLhi$QB111YkRyI9qFuNsdMETO0
[[email protected] httpd]# vim /etc/httpd/conf/httpd.conf
注释或删除之前写的
127 <Directory "/var/www/html/westos">
128 AuthUserFile /etc/httpd/apacheuser #认证文件
129 AuthName "Please input user and password !!" #警告信息
130 AuthType basic #认证方式:基本认证
131 Require user admin #认证用户admin
132 </Directory>
[[email protected] westos]# systemctl restart httpd
172.25.254.102/westos
输入用户和密码后可以看到内容
四、apache 的虚拟主机
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf127 <Directory "/var/www/html/westos">
128 AuthUserFile /etc/httpd/apacheuser
129 AuthName "Please input user and password !!"
130 AuthType basic
131 #Require user admin
132 Require valid-user #所有用户都可以通过认证
133 </Directory>
[[email protected] ~]# systemctl restart httpd
在客户端:
172.25.254.102 www.westos.com news.westos.com music.westos.com
客户端浏览器测试:
www.westos.com
news.westos.com
music.westos.com
显示内容都相同(显示内容为/var/www/html/index.html)
[[email protected] ~]# cd /etc/httpd
[[email protected] httpd]# ls
apacheuser conf conf.d conf.modules.d logs modules run
[[email protected] httpd]# cd conf.d/
[[email protected] conf.d]# ls
autoindex.conf README userdir.conf welcome.conf
[[email protected] conf.d]# vim default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined #logs:/etc/httpd/logs/ combined:所有日志,包括error,安全
</Virtualhost>
[[email protected] conf.d]# mkdir /var/www/virtual/westos.com/news -p #建立news的专属文件
[[email protected] conf.d]# mkdir /var/www/virtual/westos.com/music -p #建立music的专属文件
[[email protected] conf.d]# vim /var/www/virtual/westos.com/news/index.html #编辑news默认访问文件
[[email protected] conf.d]# vim /var/www/virtual/westos.com/music/index.html #编辑music默认访问文件
[[email protected] conf.d]# vim news.conf #编辑news的配置文件
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot "/var/www/virtual/westos.com/news/"
CustomLog "logs/news.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/news/">
Require all granted
</Directory>
[[email protected] conf.d]# cp news.conf music.conf
[[email protected] conf.d]# vim music.conf
:%s/news/music/g
[[email protected] conf.d]# systemctl restart httpd
客户端浏览器测试:
www.westos.com
news.westos.com
music.westos.com
显示内容不相同
五、https
1.http与https
HTTP:超文本传输协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少。
HTTPS:安全套接字层超文本传输协议,是以安全为目标的HTTP通道,简单讲是HTTP的安全版,即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。
HTTPS协议的主要作用可以分为两种:一种是建立一个信息安全通道,来保证数据传输的安全;另一种就是确认网站的真实性。
HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全,为了保证这些隐私数据能加密传输,于是网景公司设计了SSL(Secure Sockets Layer)协议用于对HTTP协议传输的数据进行加密,从而就诞生了HTTPS。
简单来说,HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,要比http协议安全。
HTTPS和HTTP的区别主要如下:
1、https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用。
2、http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议。
3、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。
4、http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。
http:地球标示 https:锁子--->邮箱
2.设置为https浏览方式
[[email protected] ~]# yum install mod_ssl -y
[[email protected] ~]# ls /etc/httpd/conf.d/
autoindex.conf music.conf README userdir.conf
default.conf news.conf ssl.conf welcome.conf
[[email protected] ~]# systemctl restart httpd
浏览器:
https://www.westos.com/
先显示一个地球
点击改页面最下面的选项 I Understand the Risk
点击下载认证 Add Exception...
点击 Get Certificate #下载证书
点击坐下角 Confirm Security Exception
内容出现,锁子出现,点击锁子
点击选项 More information...
点击 Viiew Certificate #查看证书
但此时查看证书是默认证书
3.自定义自签名证书
[[email protected] ~]# yum install crypto-utils -y
[[email protected] ~]# genkey www.westos.com
可以在虚拟机输入任何字母增加进度条速度
生成**时不选CA认证:
图形配置完毕后,终端会显示
/etc/pki/tls/certs/www.westos.com.crt #证书
/etc/pki/tls/private/www.westos.com.key #**
[[email protected] ~]# vim /etc/httpd/conf.d/ssl.conf
100 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
107 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
[[email protected] ~]# systemctl restart httpd
重新获取证书
看到安全证书被修改,实验完成
注意:
当**生成错误时,进入目录/etc/pki/tls/certs删除证书,然后进入目录cd ../private/删除**,重新生成**后还是不能下载证书则进行Edit–>Advanced–>View..–>删除本机证书后即可下载。
六、网页重写
[[email protected] ~]# cd /etc/httpd/conf.d
[[email protected] conf.d]# ls
autoindex.conf music.conf README userdir.conf
default.conf news.conf ssl.conf welcome.conf
[[email protected] conf.d]# cp news.conf login.conf
[[email protected] conf.d]# vim login.conf
:%s/news/login/g
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot "/var/www/virtual/westos.com/login/"
CustomLog "logs/login.log" combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
<Directory "/var/www/virtual/westos.com/login/">
Require all granted
</Directory>
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
^(/.*)$ #客户主机在浏览器里写的内容
https://%{HTTP_HOST}$1 #去掉协议部分
[redirect=301] #临时转换
[[email protected] conf.d]# systemctl restart httpd
[[email protected] conf.d]# mkdir -p /var/www/virtual/westos.com/login/
[[email protected] conf.d]# vim /var/www/virtual/westos.com/login/index.html
[[email protected] conf.d]# systemctl restart httpd
在客户端:
[[email protected] ~]# vim /etc/hosts
login.westos.com
浏览器测试:输入login.westos.com直接跳转为https://login.westos.com
接下来的步骤和上文自签名证书中下载证书的步骤一样
七、php语言支持
网页类型
xxxx.html xxxx.php xxxx.cgi 比较常用
xxxx.jsp xxx.asp xxx.pl
[[email protected] conf.d]# cd /var/www/html
[[email protected] html]# ls
index.html test.html westos
[[email protected] html]# yum install php -y
[[email protected] html]# vim index.php
<?php
phpinfo();
?>
[[email protected] html]# vim /etc/httpd/conf/httpd.conf
178 DirectoryIndex index.php index.html
[[email protected] html]# systemctl restart httpd
浏览器测试:
172.25.254.102
八、CGI
通用网关接口(CGI)是网站上放置动态内容的最筒单的方法。CGI 脚本可用于许多目的,但是谨慎控制使用哪个 CGI 脚本以及允许谁添加和运行这些脚本十分重要。编写质量差的 CGI 脚本可能为外部攻击者提供了破坏网站及其内容安全性的途径。困此,在 Web 服务器级别和 SELinux 策略级别,都存在用于限制 CGI 脚本使用的设置
[[email protected] html]# mkdir cgi
[[email protected] html]# ls
cgi index.html index.php teat.html westos
[[email protected] html]# vim cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[[email protected] html]# chmod +x cgi/index.cgi
[[email protected] html]# ./cgi/index.cgi
浏览器测试:
http://172.25.254.102/cgi/index.cgi
显示脚本,没有执行
[[email protected] html]# cd /etc/httpd/conf.d/
[[email protected] conf.d]# ls
autoindex.conf login.conf news.conf README userdir.conf
default.conf music.conf php.conf ssl.conf welcome.conf
[[email protected] conf.d]# vim default.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined
</Virtualhost>
<Directory "/var/www/html/cgi">
Options +ExecCGI #对cgi文件可执行
AddHandler cgi-script .cgi
DirectoryIndex index.cgi #默认执行文件index.cgi
</Directory>
[[email protected] conf.d]# systemctl restart httpd
浏览器测试:
浏览器刷新一下可以显示时间,操作成功!
九、安装论坛
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# cd /var/www/html/
下载 Discuz_X3.2_SC_UTF8.zip
[[email protected] html]# ls
cgi Discuz_X3.2_SC_UTF8.zip index.html index.php test.html westos
[[email protected] html]# unzip Discuz_X3.2_SC_UTF8.zip
[[email protected] html]# chmod 777 /var/www/html/upload -R
[[email protected] html]# yum install php-mysql -y
[[email protected] html]# systemctl restart httpd
[[email protected] ~]# systemctl start mariadb
浏览器
http://172.25.254.102/upload/forum.php
十、代理
正向代理
正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。
代理主机(可上网的主机):
[[email protected] html]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=none
IPADDR0=172.25.254.102
NETMASK=255.255.255.0
GATEWAY0=172.25.254.60
DNS1=192.168.43.1
IPV6INIT=no
USERCTL=yes
PEERDNS=yes
ONBOOT=yes
PERSISTENT_DHCLIENT=1
[[email protected] html]# systemctl restart network
[[email protected] html]# yum install squid -y
[[email protected] html]# vim /etc/squid/squid.conf
56 http_access allow all
62 cache_dir ufs /var/spool/squid 100 16 256
3128端口是Squid HTTP代理服务器的默认端口
[[email protected] html]# systemctl restart squid
在真机浏览器
点击菜单--->选择perferences--->Advanced--->network
点击settings
在本机浏览器测试:www.baidu.com
反向代理
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
一个做squid反向代理,一个做阿帕奇
在server
[[email protected] ~]# hostnamectl set-hostname shenzhen.example.com #202
httpd
[[email protected] html]# vim index.html
[[email protected] html]# systemctl stop firewalld
[[email protected] conf.d]# systemctl start httpd
在desktop
[[email protected] html]#hostnamectl set-hostname xian.example.com #102
squid
[[email protected] ~]# vim /etc/squid/squid.conf
59 http_port 80 vhost vport
60 cache_peer 172.25.254.202 parent 80 0 proxy-only
#当访问80端口时从172.25.254.202这个主机缓存东西,0代表没有候补端口,proxy-only表示仅允许缓存
[[email protected] ~]# systemctl restart squid.service
[[email protected] ~]# systemctl stop firewalld
真机浏览器:
http://172.25.254.102/
http://172.25.254.202/
显示内容一样,则表示反向代理配置完成