文章转自 http://www.prochao.com/articl...
对服务器 www.example.com和blog.example.com都做了DNS域名解释,在服务器上能ping通。
(服务器供应商一般都提供设置DNS域名解释服务,本地的修改hosts文件)
方法一
修改配置文件httpd.conf
1.注释
#DocumentRoot "/usr/local/httpd/htdocs"
2.在文件最后处加上
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot "/usr/local/httpd/htdocs"
ServerName www.example.com
ErrorLog "/usr/local/httpd/logs/error.log"
CustomLog "/usr/local/httpd/logs/custom.log" combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot "/usr/local/httpd/htdocs/blog"
ServerName blog.example.com
ErrorLog "/usr/local/httpd/logs/blog_error.log"
CustomLog "/usr/local/httpd/logs/blog_custom.log" combined
</VirtualHost>
3.重启服务器成功
4.访问www.example.com成功
5.访问blog.example.com失败 404NOT FOUND
6.最后检查发现再配置文件上面 有一行 ServerAdmin you@example .com,将其注释再重启服务器则访问成功
#ServerAdmin you@example.com
方法二
1.将httpd.conf配置文件的两行注释
#DocumentRoot "/usr/local/httpd/htdocs"
#ServerAdmin you@example.com
2.然后引入虚拟服务器配置文件,打开Virtual hosts下面的Include注释
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
3.在配置文件 conf/extra/httpd-vhosts.conf同样加入如下内容
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot "/usr/local/httpd/htdocs"
ServerName www.example.com
ErrorLog "/usr/local/httpd/logs/error.log"
CustomLog "/usr/local/httpd/logs/custom.log" combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot "/usr/local/httpd/htdocs/blog"
ServerName blog.example.com
ErrorLog "/usr/local/httpd/logs/blog_error.log"
CustomLog "/usr/local/httpd/logs/blog_custom.log" combined
</VirtualHost>