httpd.conf配置文件
1)全局配置项
<Directory /> //定义“/”目录区域的开始
Options FollowSymLinks //控制选项,允许使用符号链接
AllowOverride None //不允许隐含控制文件覆盖配置
Require all denied //禁止任何人访问此区域
</Directory> //定义“/”目录区域的结束
httpd服务的访问控制
Require [not] ip <ip地址或网段列表>:表示允许或拒绝指定ip地址或网段访问
允许从任何客户机访问
<Directory "/usr/local/httpd/htdocs">
……………… //省略部分内容
Require all granted
</Directory>
允许ip
<Directory "/usr/local/httpd/htdocs">
……………… //省略部分内容
Require ip 173.17.17.173
</Directory>
不允许ip
<Directory "/usr/local/httpd/htdocs">
……………… //省略部分内容
<RequireAll>
Require all granted
Require not ip 192.168.0.0/24 192.168.1.0/24
</RequireAll>
</Directory>
<Directory "/usr/local/httpd/htdocs">
…………
AuthName "welcome"
AuthType Basic
AuthUserFile /usr/local/httpd/conf/.awspwd
Require valid-user
</Directory>
构建虚拟web主机
和TCP端口号是相同的,这是使用最为普遍的虚拟WEB主机类型。
第一个WEB站点 | 第二个WEB站点 | |
FQDN | www.test.com | www.abc.com |
IP | 192.168.36.128 | 192.168.36.128 |
TCP PORT | 80 | 80 |
网站根目录 | /var/www/test/html | /var/www/abc/html |
主页 | index.html | index.html |
分别准备两个虚拟站点的网页文件:
# mkdir -p /var/www/test/html /var/www/abc/html
# echo "www.test.com" > /var/www/test/html/index.html
# echo "www.abc.com" > /var/www/abc/html/index.html
添加虚拟主机配置
# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf //修改现有内容,配置如下:
<VirtualHost www.test.com:80> //设置test虚拟站点区域
ServerAdmin webmaster@test.com
DocumentRoot "/var/www/test/html"
ServerName www.test.com
ServerAlias test.com
ErrorLog "logs/www.test.com-error_log"
CustomLog "logs/www.test.com-access_log" common
<Directory "/var/www/test/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost www.abc.com:80> //设置abc虚拟站点区域
ServerAdmin webmaster@abc.com
DocumentRoot "/var/www/abc/html"
ServerName www.abc.com
ServerAlias abc.com
ErrorLog "logs/www.abc.com-error_log"
CustomLog "logs/www.abc.com-access_log" common
<Directory "/var/www/abc/html">
Require all granted
</Directory>
</VirtualHost>
修改主配置文件httpd.conf,加载httpd-vhosts.conf文件
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
………… //省略部分内容
Include conf/extra/httpd-vhosts.conf //加载独立的配置文件
执行httpd -t命令检测httpd配置文件语法正确性
重启httpd服务使配置生效
[root@localhost ~]# systemctl restart httpd
在/etc/hosts的文件中添加域名和ip地址的映射记录。
192.168.36.128 www.test.com
192.168.36.128 www.abc.com
验证:在客户机访问虚拟web主机 www.test.com www.abc.com
注意:C:\Windows\System32\drivers\etc\hosts文件,添加ip和域名的映射
第一个WEB站点 | 第二个WEB站点 | |
FQDN | www.nihao.com | www.hello.com |
IP | 192.168.36.120 | 192.168.36.120 |
TCP PORT | 801 | 802 |
网站根目录 | /var/www/nihao/html | /var/www/hello/html |
主页 | index.html | index.html |
给网卡添加一个新的ip地址
IPADDR1=192.168.36.128
PREFIX1=24
IPADDR2=192.168.36.120
PREFIX2=24
重启network服务
# systemctl restart network
查看网卡的ip信息
分别准备两个虚拟站点的网页文件:
# mkdir -p /var/www/nihao/html /var/www/hello/html
# echo "www.niaho.com" > /var/www/nihao/html/index.html
# echo "www.hello.com" > /var/www/hello/html/index.html
添加虚拟主机配置
# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf //在文件尾部添加如下内容,配置如下:
…………… //省略部分内容
<VirtualHost 192.168.36.120:801>
ServerAdmin webmaster@nihao.com
DocumentRoot "/var/www/nihao/html"
ServerName www.nihao.com
ServerAlias nihao.com
ErrorLog "logs/www.nihao.com-error_log"
CustomLog "logs/www.nihao.com-access_log" common
<Directory "/var/www/nihao/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.36.120:802>
ServerAdmin webmaster@hello.com
DocumentRoot "/var/www/hello/html"
ServerName www.heloo.com
ServerAlias hello.com
ErrorLog "logs/www.heloo.com-error_log"
CustomLog "logs/www.hello.com-access_log" common
<Directory "/var/www/hello/html">
Require all granted
</Directory>
</VirtualHost>
Listen 801
Listen 802
修改主配置文件httpd.conf,加载httpd-vhosts.conf文件
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
………… //省略部分内容
Include conf/extra/httpd-vhosts.conf //加载独立的配置文件
执行httpd -t命令检测httpd配置文件语法正确性
重启httpd服务使配置生效
[root@localhost ~]# systemctl restart httpd
验证:在客户机访问虚拟web主机 www.nihao.com:801 www.hello.com:802
注意:要通过hosts文件或dns解析域名
vim /etc/hosts
第一个WEB站点 | 第二个WEB站点 | |
FQDN | www.a.com | www.b.com |
IP | 192.168.36.12 | 192.168.36.22 |
TCP PORT | 80 | 80 |
网站根目录 | /var/www/a/html | /var/www/b/html |
主页 | index.html | index.html |
给网卡添加2个新的ip地址
# vim /etc/sysconfig/network-scripts/ifcfg-ens33
………… //部分省略
IPADDR3=192.168.36.12
PREFIX3=24
IPADDR4=192.168.36.22
PREFIX4=24
重启network服务
# systemctl restart network
分别准备两个虚拟站点的网页文件:
# mkdir -p /var/www/a/html /var/www/b/html
# echo "www.a.com" > /var/www/a/html/index.html
# echo "www.b.com" > /var/www/b/html/index.html
添加虚拟主机配置
# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf //在文件尾部添加如下内容,配置如下:
…………… //省略部分内容
<VirtualHost 192.168.36.12:80>
ServerAdmin webmaster@a.com
DocumentRoot "/var/www/a/html"
ServerName www.a.com
ServerAlias a.com
ErrorLog "logs/www.a.com-error_log"
CustomLog "logs/www.a.com-access_log" common
<Directory "/var/www/a/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.36.22:80>
ServerAdmin webmaster@b.com
DocumentRoot "/var/www/b/html"
ServerName www.b.com
ServerAlias b.com
ErrorLog "logs/www.b.com-error_log"
CustomLog "logs/www.b.com-access_log" common
<Directory "/var/www/b/html">
Require all granted
</Directory>
</VirtualHost>
修改主配置文件httpd.conf,加载httpd-vhosts.conf文件
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
………… //省略部分内容
Include conf/extra/httpd-vhosts.conf //加载独立的配置文件
执行httpd -t命令检测httpd配置文件语法正确性
重启httpd服务使配置生效
[root@localhost ~]# systemctl restart httpd
验证:在客户机访问虚拟web主机
192.168.36.12 192.168.36.22