环境: ↗ atl.example.com (192.168.1.101)
↗ www.example.com (192.168.1.101)
↗system1.example.com (192.168.1.101)
centos7 (192.168.1.109) dns 服务器 →system2.example.com(192.168.1.105)
↘classroom.example.com(192.168.1.109)
Apache为server端 -> rhel7 server ->(192.168.1.101)(system1.example.com)已设置192.168.1.109为dns解析
其他主机为客户端 -> rhel7 clinet ->(192.168.1.105)(system2.example.com)已设置192.168.1.109为dns解析
1.安装Apache
yum install -y httpd
2.配置防火墙80/tcp对全网开放:firewall-cmd --permanent --add-port=80/tcp && firewall-cmd --reload
或者针对某个ip域开放:firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.1.0/24 service name=http accept'
3.设置开机自启与重新启动:
systemctl enable httpd && systemctl restart httpd
4.设置system1.example.com站点
echo 'system1.example.com' > /var/www/html/index.html
执行systemctl restart httpd 然后再浏览器输入 http://system1.example.com
5.配置虚拟主机www.example.com站点
mkdir /var/www/virtual
cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/
vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/virtual"
ServerName www.example.com
</VirtualHost>
:x 保存退出
执行systemctl restart httpd 然后再浏览器输入http://www.example.com
6.www.example.com网站目录访问 域 和 用户 控制 注意!红色字体为 域 控制语句
域访问控制
<VirtualHost *:80>
DocumentRoot "/var/www/virtual"
ServerName www.example.com
<Directory /var/www/virtual/dwc>
<RequireAll>
Require all granted #允许所有主机访问
Require local #只允许本地主机访问
</RequireAll>
</Directory>
</VirtualHost> #所以这个配置只允许本地主机访问dwc
<VirtualHost *:80>
DocumentRoot "/var/www/virtual"
ServerName www.example.com
<Directory /var/www/virtual/dwc>
<RequireAll>
Require all granted #允许所有主机访问
Require ip 172.25.0.0/24 #允许172.25.0.0/24这个主机访问 <<--填 网段 或者 ip
</RequireAll>
</Directory>
</VirtualHost> #所以这个配置只允许172.25.0.0/24这个域的主机访问dwc
<VirtualHost *:80>
DocumentRoot "/var/www/virtual"
ServerName www.example.com
<Directory /var/www/virtual/dwc>
<RequireAll>
Require all granted #允许所有主机访问
Require not ip 172.25.0.0/24 #拒绝172.25.0.0/24这个域的主机访问 <<--填 网段 或者 i
</RequireAll>
</Directory>
</VirtualHost> #所以这个配置只拒绝172.25.0.0/24这个域的主机访问dwc
用户的访问控制
首先建立Apache用户口令文件:htpasswd -c /etc/httpd/passwd user1
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName system1.example.com
<Directory /var/www/html/test>
AuthType Basic
AuthName "***welcome! to ^_^ web-login!***" #web界面欢迎条文
AuthUserFile /etc/httpd/passwd #用户口令文件存放路径
Require valid-user
</Directory>
</VirtualHost> #此设置只允user1这个用户访问test页面
基于ssl(https)的虚拟主机设置
首先 yum install -y mod_ssl
然后把crt和key文件放到对应目录
<VirtualHost *:443> #!注意侦听443端口
DocumentRoot "/var/www/html"
ServerName system1.example.com
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/system1.crt
SSLCertificateKeyFile /etc/pki/tls/private/system1.key
SSLCertificateChainFile /etc/pki/tls/certs/group1.crt
</VirtualHost>
动态网站非标准端口站点http://alt.example.com:8999
首先安装动态网站插件 yum install -y mod_wsgi
listen 8909 注意侦听8999端口
<VirtualHost *:8909>注意侦听8999端口
WSGIScriptAlias / /var/www/wsgi/webapp.wsgi #wsgi脚本文件目录
ServerName alt.example.com
</VirtualHost>
然后防火墙打开8999端口:
firewall-cmd --permanent --add-port=8999/tcp也可以富规则
selinux也放行8999端口:
semanage port -a -t http_port_t -p tcp 8999
最后重启服务 systemctl restart httpd
浏览器访问 http://alt.example.com:8999
wsgi+https网站https://alt.example.com
<VirtualHost *:443>
WSGIScriptAlias / /var/www/wsgi/webapp.wsgi <----wsgi配置
ServerName alt.example.com
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/system1.crt
SSLCertificateKeyFile /etc/pki/tls/private/system1.key <---https配置
SSLCertificateChainFile /etc/pki/tls/certs/group1.crt
</VirtualHost>