httpd支持的虚拟主机类型包括以下三种
- 基于域名:为每个虚拟主机使用不同的域名、但其对应的IP使相同的。
- 基于IP地址:为每个虚拟主机使用不同的域名,切各自对应的IP地址也不相同。
- 基于端口:这种方式并不使用域名、IP地址来区分不同的站点内容,而是使用了不同的TCP端口号,因此用户在浏览不同的虚拟主机站点时需要同时指定端口号才能访问。
1、基于域名的虚拟主机
- 为虚拟主机提供域名解析
可以自行搭建测试使用的DNS服务器、这里采用本地hosts解析
- 为虚拟主机准备网页文档
为每个虚拟web主机准备网站目录及网页文档
[[email protected] /]# mkdir -p /var/www/html/bbs [[email protected] /]# mkdir -p /var/www/html/blog [[email protected] /]# echo "<h1>bbs.httpd.com<h1/>" > /var/www/html/bbs/index.html [[email protected] /]# echo "<h1>blog.httpd.com<h1/>" > /var/www/html/blog/index.html
- 添加虚拟主机配置
[[email protected] /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf //配置独立的配置文件 NameVirtualHost *:80 //设置虚拟主机监听地址 <Directory "/var/www/html"> //设置目录访问权限 Order allow,deny Allow from all </Directory> <VirtualHost *:80> DocumentRoot "/var/www/html/bbs" ServerName bbs.httpd.com ErrorLog "logs/bbs.httpd.com.error_log" CustomLog "logs/bbs.httpd.com.access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/var/www/html/blog" ServerName blog.httpd.com ErrorLog "logs/blog.httpd.com.error_log" CustomLog "logs/dummy-host2.example.com.access_log" common </VirtualHost> [[email protected] /]# vim /usr/local/httpd/conf/httpd.conf Include conf/extra/httpd-vhosts.conf //加载独立的配置文件
[[email protected] /]# /usr/local/httpd/bin/apachectl restart //重启服务使配置生效
- 修改hosts文件
[[email protected] /]# vim /etc/hosts 192.168.119.131 bbs.httpd.com 192.168.119.131 blog.httpd.com
- 在客户机中访问虚拟web主机
[[email protected] /]# curl bbs.httpd.com <h1>bbs.httpd.com<h1/> [[email protected] /]# curl blog.httpd.com <h1>blog.httpd.com<h1/>
2、基于IP地址的虚拟主机
配置基于IP地址的虚拟web主机时,不使用NameVirtualHost配置项来指定监听服务的IP地址
[[email protected] /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf //配置独立的配置文件 <Directory "/var/www/html"> //设置目录访问权限 Order allow,deny Allow from all </Directory> <VirtualHost 192.168.119.131:80> DocumentRoot "/var/www/html/bbs" ServerName bbs.httpd.com ErrorLog "logs/bbs.httpd.com.error_log" CustomLog "logs/bbs.httpd.com.access_log" common </VirtualHost> <VirtualHost 192.168.119.132:80> DocumentRoot "/var/www/html/blog" ServerName blog.httpd.com ErrorLog "logs/blog.httpd.com.error_log" CustomLog "logs/dummy-host2.example.com.access_log" common </VirtualHost> [[email protected] /]# vim /usr/local/httpd/conf/httpd.conf Include conf/extra/httpd-vhosts.conf //加载独立的配置文件 [[email protected] /]# /usr/local/httpd/bin/apachectl restart //重启服务使配置生效
3、基于端口的虚拟主机
配置基于端口的虚拟web主机时,不使用NameVirtualHost配置项来指定监听服务的IP地址
[[email protected] /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf //配置独立的配置文件 <Directory "/var/www/html"> //设置目录访问权限 Order allow,deny Allow from all </Directory> <VirtualHost 192.168.119.131:80> DocumentRoot "/var/www/html/bbs" ServerName bbs.httpd.com ErrorLog "logs/bbs.httpd.com.error_log" CustomLog "logs/bbs.httpd.com.access_log" common </VirtualHost> <VirtualHost 192.168.119.131:80> DocumentRoot "/var/www/html/blog" ServerName blog.httpd.com ErrorLog "logs/blog.httpd.com.error_log" CustomLog "logs/dummy-host2.example.com.access_log" common </VirtualHost> [[email protected] /]# vim /usr/local/httpd/conf/httpd.conf Include conf/extra/httpd-vhosts.conf //加载独立的配置文件 Listen 192.168.119.131:80 //监听80端口 Listen 192.168.119.131:8080 //监听8080端口 [[email protected] /]# /usr/local/httpd/bin/apachectl restart //重启服务使配置生效
时间: 2024-10-07 06:13:23