RHEL7 Apache 服务测试
把防火墙和selinux关闭,这样试验过程中就不用配置相关策略了。
实验一、安装apache,并提供服务
在RHEL1上
#yum install -y httpd
#echo basictest > /var/www/html/index.html
#systemctl restart httpd
#systemctl enable httpd
#netstat -anplut| grep httpd
#firewall-cmd --permanent --add-service=http
#firewall-cmd --reload
浏览器访问测试:
http://192.168.100.1
实验二、软链接网站
#mkdir /local
#echo lxjtest > /local/index.html
#semanage fcontext -a -t httpd_sys_content_t '/local(/.*)?'
#restorecon -vvFR /local
#ln -s /local/ /var/www/html/soft
浏览器访问测试:
http://192.168.100.1/soft
实验三、基于域名的虚拟主机
#vi /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf #模板文件---下面的内容可以从模板中拷贝
# mkdir /var/www/lxj
# mkdir /var/www/lxj2
# echo lxj1 >/var/www/lxj/index.html
# echo lxj2 >/var/www/lxj2/index.html
#默认网站/var/www/html的配置
# vi /etc/httpd/conf.d/0.conf <VirtualHost 192.168.100.1:80> ServerAdmin root@rusky.com DocumentRoot "/var/www/html" ServerName www.rusky.com ErrorLog "/var/log/httpd/192.168.100.1-error_log" CustomLog "/var/log/httpd/192.168.100.1-access_log" comon </VirtualHost>
#域名www.lxj.com的配置
# vi /etc/httpd/conf.d/lxj.conf <VirtualHost 192.168.100.1:80> ServerAdmin root@ruksy.com DocumentRoot "/var/www/lxj" ServerName www.lxj.com Errorlog "/var/log/httpd/www.lxj.com-error_log" CustomLog "/var/log/httpd/www.lxj2.com-access_log" common </VirtualHost>
#域名www.lxj2.com的配置
# cp /etc/httpd/conf.d/lxj.conf /etc/httpd/conf.d/lxj2.conf # vi /etc/httpd/conf.d/lxj2.conf <VirtualHost 192.168.100.1:80> ServerAdmin root@ruksy.com DocumentRoot "/var/www/lxj2" ServerName www.lxj2.com Errorlog "/var/log/httpd/www.lxj2.com-error_log" CustomLog "/var/log/httpd/www.lxj2.com-access_log" common </VirtualHost>
#systemctl restart httpd
浏览器访问测试:
http://www.rusky.com/ --默认网站,内容为basictest
http://www.lxj.com --lxj网站,内容为lxj
http://www.lxj2.com --lxj2网站,内容为lxj2
说明:必须配置DNS域名解析。请参考http://www.cnblogs.com/rusking/p/7581877.html
或者如果你不想配置DNS域名解析,你可以直接修改物理机C:\Windows\System32\drivers\etc\hosts文件,添加如下内容:
192.168.100.1 www.rusky.com
192.168.100.1 www.lxj.com
192.168.100.1 www.lxj2.com
这样,你的物理机就可以ping通上面三个域名了,你可以直接使用物理机的浏览器来进行访问测试。
实验四、基于IP的虚拟主机
添加一块网卡,设置IP为192.168.100.11/24
[root@rhel1 ~]# nmcli connection show NAME UUID TYPE DEVICE enp0s3 59118063-4e4e-403c-ad25-374704d6f6f1 802-3-ethernet enp0s3 virbr0 46056c19-c40a-40cc-a0c5-296cf7049362 bridge virbr0 [root@rhel1 ~]# nmcli device show | grep -i device GENERAL.DEVICE: virbr0 GENERAL.DEVICE: enp0s3 GENERAL.DEVICE: enp0s8 GENERAL.DEVICE: lo GENERAL.DEVICE: virbr0-nic
添加了一块网卡enp0s8。
添加配置文件,并配置网络参数
[root@rhel1 ~]# nmcli connection add type ethernet con-name enp0s8file ifname enp0s8 [root@rhel1 ~]# nmcli connection modify enp0s8file ipv4.method manual ipv4.addresses 192.168.100.11/24 ipv4.gateway 192.168.100.100 autoconnect yes ipv4.dns 192.168.100.1 [root@rhel1 ~]# nmcli connection show NAME UUID TYPE DEVICE enp0s3 59118063-4e4e-403c-ad25-374704d6f6f1 802-3-ethernet enp0s3 enp0s8file 982798e1-2f91-43f1-9644-5d0ea9839440 802-3-ethernet enp0s8 virbr0 46056c19-c40a-40cc-a0c5-296cf7049362 bridge virbr0 [root@rhel1 ~]# nmcli connection down enp0s8file [root@rhel1 ~]# nmcli connection up enp0s8file
添加配置文件:
[root@rhel1 ~]# vi /etc/httpd/conf.d/11.conf <VirtualHost 192.168.100.11:80> DocumentRoot "/var/www/test11" --其它参数可以不写,有这两行参数就可以了。 ServerName 192.168.100.11 </VirtualHost>
[root@rhel1 ~]# mkdir /var/www/test11
[root@rhel1 ~]# echo test11 >/var/www/test11/index.html
systemctl restart httpd
浏览器访问测试:
http://192.168.100.1 ---访问默认网站,显示basictest内容
http://192.168.100.11 ---访问test11网站,显示 test11 内容。
实验五、基于端口的虚拟主机
[root@rhel1 ~]# semanage port -l | grep http http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 http_cache_port_t udp 3130 http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989
[root@rhel1 ~]# semanage port -a -t http_port_t -p tcp 8888
[root@rhel1 ~]# firewall-cmd --add-port=8888/tcp --permanent
[root@rhel1 ~]#firewall-cmd --reload
[root@rhel1 ~]#vi /etc/httpd/conf/httpd.conf
Listen 8888 #增加一行,监听8888端口,这一行也可以添加到11.conf配置文件的开头;注意,如果要求开启tls加密,则修改为 Listen 8888 https。
[root@rhel1 ~]# mkdir /var/www/testport8888
[root@rhel1 ~]# echo 88888888 >/var/www/testport8888/index.html
#直接修改上面的配置文件11.conf来进行测试
[root@rhel1 conf.d]# vi 11.conf <VirtualHost 192.168.100.11:80> ServerAdmin root@ruksy.com DocumentRoot "/var/www/test11" ServerName 192.168.100.11 </VirtualHost> <VirtualHost 192.168.100.11:8888> ServerAdmin root@ruksy.com DocumentRoot "/var/www/testport8888" ServerName 192.168.100.11 </VirtualHost>
#systemctl restart httpd
浏览器访问测试:
http://192.168.100.1 ---默认80端口,显示test11内容。
http://192.168.100.11:8888 --显示内容为:88888888
实验六、LAMP
#yum install -y php* mariadb*
#unzip Discuz.zip ---下载地址:http://www.discuz.net/forum.php 开源的PHP论坛
#cp -rf upload/* /var/www/Discuz/
#semanage fcontext -l | grep http | grep rw
#chcon -R -t httpd_sys_rw_content_t /var/www/Discuz/
#chown -R apache:apache /var/www/Discuz/
#systemctl restart mariadb
#systemctl enable mariadb
#mysqladmin -u root password '123456'
#systemctl restart httpd
浏览器访问测试:
http://192.168.100.1/Discuz #需要输入数据库密码123456,更改表前缀
实验七、Alias
还原RHEL1虚拟机到初始化环境。
#mkdir /var/www/lxj
#mkdir /var/www/lxj2
#echo lxj11111 >/var/www/lxj/index.html
#echo lxj22222 >/var/www/lxj2/index.html
# vi /etc/httpd/conf.d/0.conf <Virtualhost *:80> Servername 192.168.100.1 Documentroot /var/www/lxj Alias /lxj2 /var/www/lxj2 </Virtualhost> <Directory /lxj2> #增加,添加对文件夹/lxj2的设置 AllowOverride none #增加,不允许覆盖写入 Require all granted #增加,允许所有人访问 </Directory>
访问测试:
http://192.168.100.1
结果:进入默认网站,显示内容为lxj11111
http://192.168.100.1/lxj2
结果:进入lxj2目录,显示内容为lxj22222
实验八-调用脚本
创建三个测试脚本:shell.sh、perl.pl、python.py
#vim /var/www/cgi-bin/shell.sh
#!/bin/bash echo -en "Content-Type: text/html; charset=UTF-8\n\n"; date +%c
#vim /var/www/cgi-bin/perl.pl
#!/usr/bin/perl print "Content-Type: text/html; charset=UTF-8\n\n"; $now=localtime(); print "$now\n";
#yum install -y mod_wsgi #apache调用python脚本时需要用到这个模块
#vim /var/www/cgi-bin/python.py
#!/usr/bin/env python import time def application (environ, start_response): response_body = 'UNIX EPOCH time is now: %s\n' % time.time() status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', '1'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body]
#chmod a+x /var/www/cgi-bin/shell.sh
#chmod a+x /var/www/cgi-bin/perl.pl
#shell和pear必须有执行权限,python调用模块可以不加执行权限
#vim /etc/httpd/conf.d/testscript.conf
<VirtualHost *:80> ServerAdmin root@rusky.com DocumentRoot /var/www/html ServerName 192.168.100.1 ErrorLog "/var/log/httpd/192.168.100.1-error_log" CustomLog "/var/log/httpd/192.168.100.1-access_log" common <IfModule alias_module> #增加 ScriptAlias /jiaoben/ "/var/www/cgi-bin/" #增加,支持shell和perl。 /jiaoben是虚拟目录,对应后面的脚本所在的目录 </IfModule> #增加 WSGIScriptAlias /python /var/www/cgi-bin #增加,支持python。/python也是虚拟目录 </VirtualHost>
#systemctl restart httpd
访问测试:
http://192.168.100.1/jiaoben/perl.pl
http://192.168.100.1/jiaoben/shell.sh
http://192.168.100.1/python/python.py
实验九-拒绝访问
#vim /etc/httpd/conf.d/testscript.conf --添加如下内容
<Directory "/var/www/html"> order allow,deny #顺序:先允许,再拒绝 allow from all deny from 192.168.100.2 #网段采用192.168.100 </Directory>
#systemctl restart httpd
测试:
192.168.100.2 不可以访问http://192.168.100.1/index.html,其他机器都可以访问
其它一些例子
Order deny,allow //默认充许所有主机访问
Deny from 192.168.0.100 //单独禁止
Order deny,allow
allow from all
deny from 219.204.253.8
#全部都可以通行
-------------------------------
Order deny,allow
deny from 219.204.253.8
allow from all
#全部都可以通行
-------------------------------
Order allow,deny
deny from 219.204.253.8
allow from all
#只有219.204.253.8不能通行
-------------------------------
Order allow,deny
allow from all
deny from 219.204.253.8
#只有219.204.253.8不能通行
-------------------------------
-------------------------------
Order allow,deny
deny from all
allow from 219.204.253.8
#全部都不能通行
-------------------------------
Order allow,deny
allow from 219.204.253.8
deny from all
#全部都不能通行
-------------------------------
Order deny,allow
allow from 219.204.253.8
deny from all
#只允许219.204.253.8通行
-------------------------------
Order deny,allow
deny from all
allow from 219.204.253.8
#只允许219.204.253.8通行
-------------------------------
--------------------------------
Order deny,allow
#全部都可以通行(默认的)
-------------------------------
Order allow,deny
#全部都不能通行(默认的)
-------------------------------
Order allow,deny
deny from all
#全部都不能通行
-------------------------------
Order deny,allow
deny from all
#全部都不能通行
-------------------------------
对于上面两种情况,如果换成allow from all,则全部都可以通行!
-------------------------------
Order deny,allow
deny from 219.204.253.8
#只有219.204.253.8不能通行
-------------------------------
Order allow,deny
deny from 219.204.253.8
#全部都不能通行
-------------------------------
Order allow,deny
allow from 219.204.253.8
#只允许219.204.253.8通行
-------------------------------
Order deny,allow
allow from 219.204.253.8
#全部都可以通行
实验十、SSL加密
在RHEL1上制作用于认证网站的证书和key
#cd /etc/pki/tls/certs
#make lxjtest.crt
Enter pass phrase: #输入123.com Verifying - Enter pass phrase: #输入123.com Enter pass phrase for lxjtest.key: #输入123.com Country Name (2 letter code) [XX]: #输入CN State or Province Name (full name) []: #输入BEIJING Locality Name (eg, city) [Default City]: #输入BEIJING Organization Name (eg, company) [Default Company Ltd]: #输入REDHAT Organizational Unit Name (eg, section) []: #输入WEB Common Name (eg, your name or your server's hostname) []: #输入www.rusky.com
之后,再该目录下生成两个文件:证书 lxjtest.crt 和密钥 lxjtest.key
#cp lxjtest.key /etc/pki/tls/private/lxjtest.key #复制之后,可以把原来的lxjtest.key文件删除。
#相当于已经有了CA中心,并且CA中心已经颁发了证书lxjtest.crt,一般证书保存在certs文件夹下,密钥保存在private文件夹下
#yum install -y mod_ssl.x86_64 #安装ssl模块
#httpd -M | grep -i mod_ssl #查看apache加载的模块
#如果报错AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message,一般是域名解析的错误,可以添加hosts记录,或者添加DNS记录,并更改配置文件的ServerName内容
#vim /etc/httpd/conf.d/ssl.conf
SSLEngine off #更改,今后使用对每个网站的单独引擎(默认为on,)
#firewall-cmd --permanent --add-service=https
#firewall-cmd --reload
#mkdir /var/www/443
#echo html > /var/www/html/index.html
#echo 443 > /var/www/443/index.html
#cat /etc/httpd/conf.d/ssl.conf | grep -i ^ssl #复制尾部5行
#vim /etc/httpd/conf.d/0.conf
<VirtualHost *:80> DocumentRoot /var/www/html ServerName www.rusky.com </VirtualHost>
#vim /etc/httpd/conf.d/443.conf #增加加密的www.rusky.com网站,新增行可用cat /etc/httpd/conf.d/ssl.conf | grep ^SSL | tail -n 5获得
<VirtualHost *:443> DocumentRoot /var/www/443 ServerName www.rusky.com #必须和证书输入的域名一致 SSLEngine on #激活引擎 SSLProtocol all -SSLv2 -SSLv3 #除了-SSLv2和-SSLv3协议,这两个协议不安全。 SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 SSLCertificateFile /etc/pki/tls/cert/lxjtest.crt #证书位置 SSLCertificateKeyFile /etc/pki/tls/private/lxjtest.key #密钥位置 </VirtualHost>
访问测试:
http://www.rusky.com/
https://www.rusky.com/
实验十一、访问http网站自动转为https
# vi 443.conf
<Virtualhost *:80> Servername www.rusky.com Documentroot /var/www/html RewriteEngine on RewriteRule ^/(.*) https://%{HTTP_HOST}$1 [L] </Virtualhost> <Virtualhost *:443> DocumentRoot /var/www/443test ServerName www.rusky.com SSLEngine on SSLProtocol all -SSLv2 -SSLV3 SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA SSLCertificateFile /etc/pki/tls/certs/lxjtest.crt SSLCertificateKeyFile /etc/pki/tls/private/lxjtest.key </Virtualhost>
访问测试:
http://www.rusky.com #自动重定向为https://www.rusky.com