`
hipeace87
  • 浏览: 170479 次
  • 性别: Icon_minigender_1
  • 来自: 山东菏泽
社区版块
存档分类
最新评论

Ubuntu Linux下apache基于域名的虚拟主机配置

阅读更多

  比如有两个域名www.a.com 和www.b.com两个域名要做解析

 

 在/etc/apache2/sites-available 目录下建立a和b两个文件,这里只是为了使用的方便,你仔细观察可以看出在sites-enabled文件下也会有相关的文件连接(只是一个软连接)到sites-available 文件下相应的文件上

为了方便管理,这里做的都使用一个名称也容易记忆。

编辑相应文件内容如下

NameVirtualHost www.a.com
<VirtualHost www.a.com>
    ServerName www.a.com
    ServerAlias a.com *.a.com
    ServerAdmin webmaster@a.com

    DocumentRoot /var/www/a/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/a/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
        # This directive allows us to have apache2's default start page
        # in /apache2-default/, but still have / go to the right place
        # Commented out for Ubuntu
        #RedirectMatch ^/$ /apache2-default/
    </Directory>

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

    ErrorLog /var/log/apache2/a.log

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

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

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

 

文件b里的内容基本和a是一样的,只需要所少数几处改一下

执行a2ensite a              a2ensite b 命令使虚拟主机生效

重启apache

 

/etc/init.d/apache2 restart

或者重新加载apache2

  

/etc/init.d/apache2 reload   

 修改hosts文件

vi /etc/hosts

 

插入下面两行

127.0.0.1 www.a.com

127.0.0.1 www.b.com

现在访问两个域名,会发现解析到了不同的目录

如果哪个虚拟主机不想使用只需要执行命令

a2dissite b

 

这样再访问www.b.com就不能正确解析

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics