在火山互联申请了免费linux vps后,自己动手实践能力就要更为丰富了,本文为配置vhost.conf文件达到linux下apache的虚拟主机功能
环境:CentOS 5.2 + Apache 2.2.6 + PHP 5.2.5 + MySQL 5.1.3
首先要apache服务认得虚拟主机配置文件,则要修改/usr/local/apache2/conf/httpd.conf
在其底部加入:
Include "/usr/local/apache2/conf/vhost.conf"
定位到apache安装目录下的配置文件,/usr/local/apache2/conf/vhost.conf
打开后内容如下:
DocumentRoot /data/web/www/
ServerName www.vhost.cn
将它修改为我们所需要的:
NameVirtualHost *:80
ServerName is36.cn
ServerAlias www.is36.cn
DocumentRoot /data/web/www/
ServerName blog.is36.cn
ServerAlias www.blog.is36.cn
DocumentRoot /data/web/blog/
修改完成后重启apache服务即可:apache2 -k restart
(用ln做链接,类似windows下的快捷方式指向全局变量地址,就可以在终端任何地方输入apache2进行对apache服务的操作了)
ln -s /usr/local/apache2/bin/apachectl /usr/sbin/apache2
重启过程中如果出现如下情况需要去建立对应的blog文件夹:
[root@linuxserver ~]# apache2 -k restart
Warning: DocumentRoot [/data/web/blog/] does not exist
据网上资料还要在httpd.conf下加入如下的配置条文,本过程中未加入,后续观察:
Options Indexes FollowSymLinks
AllowOverride all
Order Allow,Deny
Allow from all
Options Indexes FollowSymLinks
AllowOverride all
Order Allow,Deny
Allow from all
附/usr/local/apache2/conf/extra/httpd-vhosts.conf 作为配置参考文件
#
# 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
# 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 block.
#
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/www/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log common"
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/www/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log common"