/etc/sysconfig/network-scripts/ifcfg-ethX
文件,,并将它们设置为静态IP地址。vi /etc/sysconfig/network-scripts/ifcfg-ens33
# 内容如下:
DEVICE=ens33
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.10.100
NETMASK=255.255.255.0
vi /etc/sysconfig/network-scripts/ifcfg-ens36
# 内容如下:
DEVICE=ens36
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.10.200
NETMASK=255.255.255.0
systemctl restart network
编辑/etc/httpd/conf/httpd.conf
,确保以下行未被注释(即删除#
):
IncludeOptional conf.d/*.conf
然后,在/etc/httpd/conf.d/
目录下创建两个新的配置文件,比如vhost1.conf
和vhost2.conf
,分别用于配置基于IP的虚拟主机:
vim /etc/httpd/conf.d/vhost1.conf
# 内容如下:
<VirtualHost 192.168.10.100:80>
DocumentRoot "/var/www/html/web1"
</VirtualHost>
vim /etc/httpd/conf.d/vhost2.conf
# 内容如下:
<VirtualHost 192.168.10.200:80>
DocumentRoot "/var/www/html/web2"
</VirtualHost>
或者写到一个配置文件中
[root@localhost ~]# cat /etc/httpd/conf.d/vhosts-ip.conf
<VirtualHost 192.168.10.100:80>
DocumentRoot "/var/www/html/web1"
</VirtualHost>
<VirtualHost 192.168.10.200:80>
DocumentRoot "/var/www/html/web2"
</VirtualHost>
mkdir -p /var/www/html/web1 /var/www/html/web2
echo "This is my virtual website1." > /var/www/html/web1/index.html
echo "This is my virtual website2 ." > /var/www/html/web2/index.html
systemctl restart httpd
编辑/etc/httpd/conf.d/
下的新配置文件,例如vhost3.conf
和vhost4.conf
,配置不同的端口:
vim /etc/httpd/conf.d/vhost3.conf
# 内容如下:
Listen 8000
<VirtualHost *:8000>
DocumentRoot "/var/www/html/web3"
</VirtualHost>
vim /etc/httpd/conf.d/vhost4.conf
# 内容如下:
Listen 8888
<VirtualHost *:8888>
DocumentRoot "/var/www/html/web4"
</VirtualHost>
或者全部写到一个配置文件中
[root@localhost ~]# cat /etc/httpd/conf.d/vhosts-port.conf
Listen 8000
<VirtualHost 192.168.10.100:8000>
DocumentRoot "/var/www/html/web3"
</VirtualHost>
Listen 8888
<VirtualHost 192.168.10.100:8888>
DocumentRoot "/var/www/html/web4"
</VirtualHost>
mkdir -p /var/www/html/web3 /var/www/html/web4
echo "This is my virtual website3." > /var/www/html/web3/index.html
echo "This is my virtual website4." > /var/www/html/web4/index.html
systemctl restart httpd
测试基于端口的虚拟主机
再次在Windows系统下使用浏览器访问这两个端口,确保能够看到正确的网页。