如何配置apache虚拟主机的实例小结
1、基于ip地址的虚拟主机
listen 80
<virtualhost 172.20.30.40>
documentroot /home/httpd/html1
servername www.ok1.com
errorlog /usr/local/apache/logs/error1_log
customlog /usr/local/apache/logs/access1_log combined
</virtualhost>
<virtualhost 172.20.30.50>
documentroot /home/httpd/html2
servername www.ok2.com
errorlog /usr/local/apache/logs/error2_log
customlog /usr/local/apache/logs/access2_log combined
</virtualhost>
2、基于ip 和多端口的虚拟主机配置
listen 172.20.30.40:80
listen 172.20.30.40:8080
listen 172.20.30.50:80
listen 172.20.30.50:8080
<virtualhost 172.20.30.40:80>
documentroot /www/example1-80
servername www.example1.com
</virtualhost>
<virtualhost 172.20.30.40:8080>
documentroot /www/example1-8080
servername www.example1.com
</virtualhost>
<virtualhost 172.20.30.50:80>
documentroot /www/example2-80
servername www.example1.org
</virtualhost>
<virtualhost 172.20.30.50:8080>
documentroot /www/example2-8080
servername www.example2.org
</virtualhost>
3、单个ip 地址的服务器上基于域名的虚拟主机配置
# ensure that apache listens on port 80
listen 80
# listen for virtual host requests on all ip addresses
namevirtualhost *:80
<virtualhost *:80>
documentroot /www/example1
servername www.example1.com
serveralias example1.com. *.example1.com
# other directives here
</virtualhost>
<virtualhost *:80>
documentroot /www/example2
servername www.example2.org
# other directives here
</virtualhost>
如果您感觉上面的文章还不够详细可以看下下面的文章:
实验目标:在apache实现基于域名的虚拟主机
实验用的xampp版本为1.7.7,内含apache版本为2.2.21
实验前准备:
1. 为了测试不同的域名,在windows/system32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 -
127.0.0.1 test1.net
127.0.0.1 test2.net
如此,则在浏览器中输入该俩域名时,windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问xampp的欢迎页。
2. 在apache目录下建立目录,以放置您不同的网站。为保护xampp原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 -
apache/htdocs1/test1.net - 放置test1.net网站内容
apache/htdocs1/test2.net - 放置test2.net网站内容
在这两个目录中各新建hello world一网页 index.html,内容 -
<html>
<head></head>
<body>
<h1>hello~, 这是[对应的网站名,用以区别].net</h1></body>
</html>
实验步骤:
1. 找到apache/conf/httpd.conf, 将其中的
serveradmin
servername
documentroot
注释掉。
2. 在httpd.conf中,找到行
include "conf/extra/httpd-vhosts.conf"
如被注释则解注。该文件记载了虚拟主机的参数。[以前虚拟主机参数是直接填写在httpd.conf中的,为了更好地组织文件,将其分离出去,类似于某些编程语言一样。因此httpd.conf中include它,即相当于把它的内容填在了httpd.conf中。]
3. 这个httpd-vhosts.conf文件格式基本如下 -
#blah-blah
namevirtualhost *:80
#blah-blah
#blah-blah
<virtualhost *:80>
serveradmin xxxxxxxx
documentroot "xxxxxxxx"
servername xxxxxxx
serveralias xxxxxx
errorlog "logs/xxxxxx-error.log"
customlog "logs/xxxxxxx-error.log" combined
</virtualhost>
需要修改的,就是<virtualhost>中的参数了。这个可以参见apache官方文档。根据实验域名,可以增加两个<virtualhost>:
<virtualhost *:80>
serveradmin adm@test1.net
documentroot "c:/xampp/htdocs1/test1.net"
servername test1.net
serveralias www.test1.net
errorlog "logs/test1-error.log"
customlog "logs/test1-access.log" combined
<directory "c:/xampp/htdocs1/test1.net">
order allow,deny
allow from all
</directory>
</virtualhost>
<virtualhost *:80>
serveradmin adm@test2.net
documentroot "c:/xampp/htdocs1/test2.net"
servername test2.net
serveralias www.test2.net
errorlog "logs/test1-error.log"
customlog "logs/test1-access.log" combined
<directory "c:/xampp/htdocs1/test2.net">
order allow,deny
allow from all
</directory>
</virtualhost>
注意,如果不在各virtualhost中定义directory的可访问性,你将遇到的是access forbidden!就连原来的localhost也是。
4. 由于之前注释掉了httpd.conf中的servername, documentroot等,为了仍然能以localhost访问原xampp欢迎页,就在增加一个virtualhost,如下 -
<virtualhost *:80>
serveradmin adm@localhost
documentroot "c:/xampp/htdocs"
servername localhost
errorlog "logs/localhost-error.log"
customlog "logs/localhost-access.log" combined
<directory "c:/xampp/htdocs">
order allow,deny
allow from all
</directory>
</virtualhost>
为了避免出错,把它放置在第一个virtualhost位置。
至此,apache基于域名的虚拟主机配置完成。可以通过http://localhost访问xampp欢迎页,通过http://test1.net和http://test2.net访问各自的主页。
#
# virtual hosts
#
# if you want to maintain multiple domains/hostnames on your
# machine you can setup virtualhost containers for them. most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# ip addresses. this is indicated by the asterisks in the directives below.
#
# please see the documentation at
# <url:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# you may use the command line option '-s' to verify your virtual host
# configuration.
#
# use name-based virtual hosting.
#
namevirtualhost *:80
#
# virtualhost example:
# almost any apache directive may go into a virtualhost container.
# the first virtualhost section is used for all requests that do not
# match a servername or serveralias in any <virtualhost> block.
#
<virtualhost *:80>
serveradmin kongdaoxian@gmail.com
documentroot "e:/skydao/apache2/htdocs"
servername localhost
serveralias www.skydao.com
errorlog "logs/localhost-error.log"
customlog "logs/localhost-access.log" combined
<directory "e:/skydao/apache2/htdocs">
order allow,deny
allow from all
</directory>
</virtualhost>
<virtualhost *:80>
serveradmin kongdaoxian@gmail.com
documentroot "e:/skydao/apache2/htdocs/project1"
servername project1.com
serveralias www.project1.com
errorlog "logs/project1-error.log"
customlog "logs/project1-access.log" combined
<directory "e:/skydao/apache2/htdocs/project1">
order allow,deny
allow from all
</directory>
</virtualhost>
<virtualhost *:80>
serveradmin kongdaoxian@gmail.com
documentroot "e:/skydao/apache2/htdocs/zendtest/public"
servername zendtest.com
serveralias www.zendtest.com
directoryindex index.php
<directory />
options followsymlinks
allowoverride none
order allow,deny
allow from all
</directory>
</virtualhost>
<virtualhost *:80>
serveradmin kongdaoxian@gmail.com
documentroot "e:/skydao/apache2/htdocs/testrewrite"
servername testrewrite.com
serveralias www.testrewrite.com
# directoryindex index.php
<directory />
options followsymlinks
allowoverride none
order allow,deny
allow from all
</directory>
</virtualhost>
<virtualhost *:80>
serveradmin kongdaoxian@gmail.com
documentroot "e:/skydao/apache2/htdocs/test"
servername test.com
serveralias www.test.com
errorlog "logs/zendtest-error.log"
customlog "logs/zendtest-access.log" combined
<directory "e:/skydao/apache2/htdocs/test">
order allow,deny
allow from all
</directory>
</virtualhost>
上一篇: 最完的htaccess文件用法收集整理
下一篇: ssh连接超时解决方法