Apache的虚拟主机功能是服务器基于用户请求的不同IP地址、主机域名或端口号,实现提供多个网站同时为外部提供访问服务的技术,用户所需要的资源不同,最终获取到的网页内容也不相同。
Apache虚拟主机功能的配置(使用ip地址配置)
第一步:为虚拟主机添加两个ip地址
编辑网卡配置文件,并为其添加IPADDR1、IPADDR2,保存退出。
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
#static assignment
NM_CONTROLLED=no #表示该接口将通过该配置文件进行设置,而不是通过网络管理>器进行管理
ONBOOT=yes #开机启动
BOOTPROTO=static #静态IP
IPADDR=192.168.146.100 #本机地址
IPADDR1=192.168.146.110
IPADDR2=192.168.146.120
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.146.2 #默认网关
DNS1=192.168.146.2
第二步:测试网络是否连通
若是网络连接不同,请仔细检查网卡配置文件是否正确。
[root@localhost ~]# ping 192.168.146.100
PING 192.168.146.100 (192.168.146.100) 56(84) bytes of data.
64 bytes from 192.168.146.100: icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from 192.168.146.100: icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from 192.168.146.100: icmp_seq=3 ttl=64 time=0.083 ms
64 bytes from 192.168.146.100: icmp_seq=4 ttl=64 time=0.083 ms
^C
--- 192.168.146.100 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.070/0.080/0.084/0.005 ms
[root@localhost ~]# ping 192.168.146.110
PING 192.168.146.110 (192.168.146.110) 56(84) bytes of data.
64 bytes from 192.168.146.110: icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from 192.168.146.110: icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from 192.168.146.110: icmp_seq=3 ttl=64 time=0.079 ms
^C
--- 192.168.146.110 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2013ms
rtt min/avg/max/mdev = 0.062/0.075/0.084/0.009 ms
[root@localhost ~]# ping 192.168.146.120
PING 192.168.146.120 (192.168.146.120) 56(84) bytes of data.
64 bytes from 192.168.146.120: icmp_seq=1 ttl=64 time=0.064 ms
64 bytes from 192.168.146.120: icmp_seq=2 ttl=64 time=0.088 ms
64 bytes from 192.168.146.120: icmp_seq=3 ttl=64 time=0.109 ms
^C
--- 192.168.146.120 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2022ms
rtt min/avg/max/mdev = 0.064/0.087/0.109/0.018 ms
[root@localhost ~]#
第三步:创建保存数据网络的目录
注意,在这里创建保存网络的数据需要在/home/wwwroot,因为在前期搭建Apache服务时我将保存网络数据的默认目录修改至/home/wwwroot
[root@localhost ~]# mkdir -p /home/wwwroot/100
[root@localhost ~]# mkdir -p /home/wwwroot/110
[root@localhost ~]# mkdir -p /home/wwwroot/120
第四步:给保存网络数据的目录中写入数据
[root@localhost ~]# echo "IP:192.168.146.100" > /home/wwwroot/100/index.html
[root@localhost ~]# echo "IP:192.168.146.110" > /home/wwwroot/110/index.html
[root@localhost ~]# echo "IP:192.168.146.120" > /home/wwwroot/120/index.html
第五步:修改配置文件,写入三个基于ip地址的虚拟主机网站参数
大有在配置文件的113行左右开始编写,具体信息如下
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
113 <VirtualHost 192.168.146.100>
114 DocumentRoot /home/wwwroot/100
115 ServerName www.baidu.com
116 <Directory /home/wwwroot/100 >
111 # below.
112 #
113 <VirtualHost 192.168.146.100>
114 DocumentRoot /home/wwwroot/100
115 ServerName www.baidu.com
116 <Directory /home/wwwroot/100 >
117 AllowOverride None
118 Require all granted
119 </Directory>
120 </VirtualHost>
121 <VirtualHost 192.168.146.110>
122 DocumentRoot /home/wwwroot/110
123 ServerName bbs.baidu.com
124 <Directory /home/wwwroot/110 >
125 AllowOverride None
126 Require all granted
127 </Directory>
130 DocumentRoot /home/wwwroot/120
131 ServerName tech.baidu.com
132 <Directory /home/wwwroot/120 >
133 AllowOverride None
134 Require all granted
135 </Directory>
136 </VirtualHost>
第六步:重启httpd服务,并访问网站
[root@localhost ~]# systemctl restart httpd
访问网站结果如下:
如果此时刷新网页时,不能出现上图所示的结果,则与SELinux安全上下文设置有关,需要重新设置安全上下文。
第七步:配置SELinux安全上下文,并使其立即生效(如果第六步访问成功,则不需要配置)
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/100
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/100/*
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/110
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/110/*
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/120
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/120/*
[root@localhost ~]# restorecon -Rv /home/wwwroot
此刻,再次访问网页,即可成功。我们可以看到如上图所示的数据。