Zhang Jiuan’ Notes

虚拟主机配置经验

自已租了一台主机,装了一个apache,但问题也随之页来?如何让我的一个主机对外提供多个服务呢?首先我想到的是端口,比如有两个blog: blogA; blogB,一个blogA监听80端口,另一个blogB则监听8080端口,这样的确是一个不错的办法,我也轻页易举的实现了这样的配置。如下:

NameVirtualHost *:80
Listen 80

NameVirtualHost *:8080
Listen 8080

<VirtualHost *:80>
    ServerAdmin webmaster@jiuansafe.cn

    DocumentRoot /home/blog/blog

    <Directory />
        Options FollowSymLinks
        AllowOverride all
    </Directory>
    <Directory /home/blog/blog>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory “/usr/lib/cgi-bin”>
        AllowOverride all
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ “/usr/share/doc/”
    <Directory “/usr/share/doc/”>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride all
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

<VirtualHost *:8080>
    ServerAdmin webmaster@jiuansafe.cn

    DocumentRoot /home/blog/blog

    <Directory />
        Options FollowSymLinks
        AllowOverride all
    </Directory>
    <Directory /home/blog/blog>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory “/usr/lib/cgi-bin”>
        AllowOverride all
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ “/usr/share/doc/”
    <Directory “/usr/share/doc/”>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride all
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

嗯效果不错。

后来我申请了两个域名blog.niukey.comwww.lisablog.cn两个域名,现在问题又来了我如何让我的主机只监听一个80端口,当是域名blog.niukey.com的时候则以jiuanblog的对应文件对外提供服务。我首先想到的是Apache的Rewrite功能,通过REQUEST_HOST的确可以做一些事,但总觉得解决的不够完美,于是baidu了一把,方知道通过虚拟主机的ServerName配置可以完成如上功能。

NameVirtualHost *:80
Listen 80

<VirtualHost *:80>
    ServerAdmin webmaster@jiuansafe.cn

    DocumentRoot /home/blog/jiuanblog

    ServerName blog.niukey.com

    <Directory />
        Options FollowSymLinks
        AllowOverride all
    </Directory>
    <Directory /home/blog/blog>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory “/usr/lib/cgi-bin”>
        AllowOverride all
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ “/usr/share/doc/”
    <Directory “/usr/share/doc/”>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride all
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@jiuansafe.cn

    DocumentRoot /home/blog/lisablog

    ServerName www.lisablog.cn

    <Directory />
        Options FollowSymLinks
        AllowOverride all
    </Directory>
    <Directory /home/blog/blog>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory “/usr/lib/cgi-bin”>
        AllowOverride all
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ “/usr/share/doc/”
    <Directory “/usr/share/doc/”>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride all
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

好万事大吉,于是运行如下命令

apachectl restart重启apache

在流览器输入

blog.niukey.com

www.lisablog.cn

OK效果果然不错。

 

thx

张久安

If you enjoyed this post, make sure you subscribe to my RSS feed!

No Comments, Comment or Ping

Reply to “虚拟主机配置经验”

You must be logged in to post a comment.

返回顶部