2、Apache基于端口的虚拟主机配置
如一台服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置。
2.1 假设服务器有个IP地址为192.168.2.154
[root@localhost conf]
[root@localhost conf]
ens33:4: flags&#61;4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.2.154 netmask 255.255.255.0 broadcast 192.168.2.255ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)
2.2 需要配置的虚拟主机分别为7081、8081和9081&#xff0c;配置主机的host文件便于测试。
[root&#64;localhost conf]
[root&#64;localhost conf]
192.168.2.154 www.test154.com
2.3 建立虚拟主机存放网页的根目录&#xff0c;并创建首页文件index.html
[root&#64;localhost conf]
[root&#64;localhost www]
[root&#64;localhost www]
[root&#64;localhost port]
[root&#64;localhost port]
7081 8081 9081
[root&#64;localhost port]
[root&#64;localhost port]
[root&#64;localhost port]
2.4 修改 httpd.conf 在文件末尾加入以下配置
[root&#64;localhost port]# vim /usr/local/apache/conf/httpd.confListen 192.168.2.154:7081
Listen 192.168.2.154:8081
Listen 192.168.2.154:9081
Include conf/vhost/*.conf
2.5 编辑每个端口的配置文件
[root&#64;localhost vhost]
[root&#64;localhost vhost]
192.168.2.154:7081>ServerName www.test154.comDocumentRoot "/data/www/port/7081"DirectoryIndex index.html index.htm index.php"/data/www/port/7081">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory>
VirtualHost>
[root&#64;localhost vhost]
[root&#64;localhost vhost]
192.168.2.154:8081>ServerName www.test154.comDocumentRoot "/data/www/port/8081"DirectoryIndex index.html index.htm index.php"/data/www/port/8081">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory>
VirtualHost>
[root&#64;localhost vhost]
[root&#64;localhost vhost]
192.168.2.154:9081>ServerName www.test154.comDocumentRoot "/data/www/port/9081"DirectoryIndex index.html index.htm index.php"/data/www/port/9081">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory>
VirtualHost>
2.6 配置完以后可以启动Apache服务并进行测试
[root&#64;localhost ~]
Syntax OK
[root&#64;localhost ~]
[root&#64;localhost ~]
port 7081
[root&#64;localhost ~]
port 8081
[root&#64;localhost ~]
port 9081