第三步:建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@Apache ~]# cd /www [root@Apache ~]# mkdir port [root@Apache ~]# cd port [root@Apache ~]# mkdir 6081 [root@Apache ~]# mkdir 7081 [root@Apache ~]# mkdir 9081 [root@Apache ~]# echo "port 6081" > 6081/index.html [root@Apache ~]# echo "port 7081" > 7081/index.html [root@Apache ~]# echo "port 9081" > 9081/index.html第四步:修改httpd.conf在文件末尾加入以下配置
[root@Apache ~]# vim /usr/local/apache2/conf/httpd.conf --文件末尾加入以下配置 Listen 192.168.1.14:6081 Listen 192.168.1.14:7081 Listen 192.168.1.14:9081 Include conf/vhost/*.conf第五步:编辑每个端口的配置文件
[root@Apache ~]# cd /usr/local/apache2/conf/vhost [root@Apache ~]# vim test14.6081.conf --一定要以.conf为后缀,这是第一台机器 <VirtualHost 192.168.1.14:6081> ServerName test14.com DocumentRoot "/www/port/6081" <Directory "/www/port/6081/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost> [root@Apache ~]# vim test14.7081.conf --这是第二台机器 <VirtualHost 192.168.1.14:7081> ServerName test14.com DocumentRoot "/www/port/7081" <Directory "/www/port/7081/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost> [root@Apache ~]# vim test14.9081.conf --这是第三台机器 <VirtualHost 192.168.1.14:9081> ServerName test14.com DocumentRoot "/www/port/9081" <Directory "/data/port/9081/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost>第六步:启动服务并测试
[root@Apache ~]# /usr/local/apache2/bin/apachectl -t --检查配置文件是否正确 Syntax OK [root@Apache ~]# /usr/local/apache2/bin/apachectl restart --重新启动apache 检查虚拟主机是否运行 [root@Apache ~]# curl 192.168.1.14:6081 port 6081 [root@Apache ~]# curl 192.168.1.14:7081 port 7081 [root@Apache ~]# curl 192.168.1.14:9081 port 9081 3. Apache基于域名的虚拟主机配置第一步:使用ifconfig设置1个虚拟ip
[root@Apache ~]# ifconfig ens33:5 192.168.1.15/24第二步:配置主机的hosts文件,便于测试
[root@Apache ~]# echo "192.168.1.15 " >> /etc/hosts [root@Apache ~]# echo "192.168.1.15 " >> /etc/hosts [root@Apache ~]# echo "192.168.1.15 " >> /etc/hosts第三步:建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@Apache ~]# cd /www [root@Apache ~]# mkdir [root@Apache ~]# mkdir [root@Apache ~]# mkdir [root@Apache ~]# echo "www.aa.com" > [root@Apache ~]# echo "www.bb.com" > [root@Apache ~]# echo "www.cc.com" >第四步:修改httpd.conf在文件末尾加入以下配置
[root@Apache ~]# vim /usr/local/apache2/conf/httpd.conf --文件末尾加入以下配置 Listen 192.168.1.15:80 Include conf/vhost/*.conf第五步:编辑每个域名的配置文件
[root@Apache ~]# cd /usr/local/apache2/conf/vhost [root@Apache ~]# vim --一定要以.conf为后缀,这是第一台机器 <VirtualHost 192.168.1.15:80> ServerName DocumentRoot "/www/www.aa.com" <Directory "/www/www.aa.com/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost> [root@Apache ~]# vim --这是第二台机器 <VirtualHost 192.168.1.15:80> ServerName DocumentRoot "/www/www.bb.com" <Directory "/www/www.bb.com/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost> [root@Apache ~]# vim --这是第三台机器 <VirtualHost 192.168.1.15:80> ServerName DocumentRoot "/www/www.cc.com" <Directory "/www/www.cc.com/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost>