开放虚拟主机文件
修改主配置文件 解开注释,使用虚拟主机配置文件。
vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf
虚拟主机参数详解
<VirtualHost >:指定虚拟主机 DocumentRoot:指定URL目录 ServerName:指定域名地址 CustomLog:指定日志文件 Serveradmin:管理员邮箱 ServerAlias:域名别名(可写多行) Errorlog:错误日志 Customlog:访问日志 </VirtualHost>:结尾
虚拟主机配置
基于IP :使用多个IP 访问不同的资源的虚拟主机
1.创建多个子IP
ifconfig eth0:1 192.168.1.131 ifconfig eth0:2 192.168.1.132 ifconfig eth0:3 192.168.1.133
2.创建多个URL资源
vim 资源路径1/index.html 内容: 1 vim 资源路径2/index.html 内容: 2 vim 资源路径3/index.html 内容: 3
3.修改虚拟主机配置文件
vim httpd-vhosts.conf 内容: # 基于IP虚拟主机1 <VirtualHost 192.168.1.131:80> DocumentRoot "/usr/local/html1" ServerName 123.com <Directory "/usr/local/html1"> Require all granted </Directory> </VirtualHost> # 基于IP虚拟主机2 <VirtualHost 192.168.1.132:80> DocumentRoot "/usr/local/html2" ServerName 123.com <Directory "/usr/local/html2"> Require all granted </Directory> </VirtualHost> # 基于IP虚拟主机3 <VirtualHost 192.168.1.133:80> DocumentRoot "/usr/local/html3" ServerName 123.com <Directory "/usr/local/html3"> Require all granted </Directory> </VirtualHost>
基于域名:使用1个IP绑定多个域名进行多资源访问的虚拟主机
1.修改hosts文件,或者DNS配置域名
文件目录:C:WindowsSystem32driversetchosts 底行添加内容: 192.168.1.107 www.1.com 192.168.1.107 www.2.com 192.168.1.107 www.3.com
2.创建多个URL资源
vim 资源路径1/index.html 内容: 1 vim 资源路径2/index.html 内容: 2 vim 资源路径3/index.html 内容: 3
3.修改虚拟主机配置文件
vim httpd-vhosts.conf 内容: # 基于域名1 <VirtualHost *:80> DocumentRoot "/usr/local/html1" ServerName www.1.com <Directory "/usr/local/html1"> Require all granted </Directory> </VirtualHost> # 基于域名2 <VirtualHost *:80> DocumentRoot "/usr/local/html2" ServerName www.2.com <Directory "/usr/local/html2"> Require all granted </Directory> </VirtualHost> # 基于域名3 <VirtualHost *:80> DocumentRoot "/usr/local/html3" ServerName www.3.com <Directory "/usr/local/html3"> Require all granted </Directory> </VirtualHost>
基于端口:使用1个IP绑定多个端口进行多资源访问的虚拟主机
1.修改主配置文件添加端口
vim httpd.conf 添加内容: Listen 801 Listen 802 Listen 803
2.修改虚拟主机配置文件
vim httpd-vhosts.conf 内容: # 基于端口1 <VirtualHost 192.168.1.107:801> DocumentRoot "/usr/local/html1" ServerName www.1.com <Directory "/usr/local/html1"> Require all granted </Directory> </VirtualHost> # 基于端口2 <VirtualHost 192.168.1.107:802> DocumentRoot "/usr/local/html2" ServerName www.2.com <Directory "/usr/local/html2"> Require all granted </Directory> </VirtualHost> # 基于端口3 <VirtualHost 192.168.1.107:803> DocumentRoot "/usr/local/html3" ServerName www.3.com <Directory "/usr/local/html3"> Require all granted </Directory> </VirtualHost>
3.重启后查看端口是否开放
netstat -lnp | grep 80 执行结果: tcp 0 0 :::801 :::* LISTEN 1504/./httpd tcp 0 0 :::802 :::* LISTEN 1504/./httpd tcp 0 0 :::803 :::* LISTEN 1504/./httpd