标签:
httpd支持的虚拟主机类型包括以下三种
1、基于域名的虚拟主机
可以自行搭建测试使用的DNS服务器、这里采用本地hosts解析
为每个虚拟web主机准备网站目录及网页文档
[root@www /]# mkdir -p /var/www/html/bbs [root@www /]# mkdir -p /var/www/html/blog [root@www /]# echo "<h1>bbs.httpd.com<h1/>" > /var/www/html/bbs/index.html [root@www /]# echo "<h1>blog.httpd.com<h1/>" > /var/www/html/blog/index.html
[root@www /]# 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> [root@www /]# vim /usr/local/httpd/conf/httpd.conf Include conf/extra/httpd-vhosts.conf //加载独立的配置文件
[root@www /]# /usr/local/httpd/bin/apachectl restart //重启服务使配置生效
[root@www /]# vim /etc/hosts 192.168.119.131 bbs.httpd.com 192.168.119.131 blog.httpd.com
[root@www /]# curl bbs.httpd.com <h1>bbs.httpd.com<h1/> [root@www /]# curl blog.httpd.com <h1>blog.httpd.com<h1/>
2、基于IP地址的虚拟主机
配置基于IP地址的虚拟web主机时,不使用NameVirtualHost配置项来指定监听服务的IP地址
[root@www /]# 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> [root@www /]# vim /usr/local/httpd/conf/httpd.conf Include conf/extra/httpd-vhosts.conf //加载独立的配置文件 [root@www /]# /usr/local/httpd/bin/apachectl restart //重启服务使配置生效
3、基于端口的虚拟主机
配置基于端口的虚拟web主机时,不使用NameVirtualHost配置项来指定监听服务的IP地址
[root@www /]# 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> [root@www /]# 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端口 [root@www /]# /usr/local/httpd/bin/apachectl restart //重启服务使配置生效
标签:
原文地址:http://www.cnblogs.com/zzzhfo/p/5934181.html