本文介绍下,在ubuntu系统中的apache2环境下,绑定多个域名的例子,用于掌握apache2下虚拟主机的配置方法,有需要的朋友可以参考下。

本节主要内容:
apache2绑定多域名

系统环境:
服务器ip地址:1.1.1.1
二个域名www.a.com、www.b.com。

要求实现:
www.a.com绑定到/var/www/a下。
www.b.com绑定到/var/www/b下。
用基于域名的方式配置虚拟主机。

操作步骤:
1、将 http://www.a.com 与 http://www.b.com 的DNS解析到服务器IP上。
2、进入 /etc/apache2/sites-enabled/ ; 删除 000-default 文件。
3、在 /etc/apache2/sites-available/ 目录,创建2个文件。文件名用 a.conf 和 b.conf 。

在 a.conf 中添加内容:
 

复制代码代码示例:

<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 ServerName www.a.com
 DocumentRoot /var/www/a
 <Directory />
  Options FollowSymLinks
  DirectoryIndex index.php index.html index.htm
  AllowOverride None
 </Directory>
 <Directory /var/www/a>
# Options Indexes FollowSymLinks MultiViews
  Options FollowSymLinks
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php
  AllowOverride All
  Order allow,deny
  allow from all
 </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 ${APACHE_LOG_DIR}/error.log

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

 CustomLog ${APACHE_LOG_DIR}/access.log combined

    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.conf 中添中内容:
 

复制代码代码示例:

<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 ServerName www.b.com
 DocumentRoot /var/www/b
 <Directory />
  Options FollowSymLinks
  DirectoryIndex index.php index.html index.htm
  AllowOverride None
 </Directory>
 <Directory /var/www/b>
# Options Indexes FollowSymLinks MultiViews
  Options FollowSymLinks
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php
  AllowOverride All
  Order allow,deny
  allow from all
 </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 ${APACHE_LOG_DIR}/error.log

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

 CustomLog ${APACHE_LOG_DIR}/access.log combined

    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>

4、在 /etc/apache2/sites_enabled/ 中创建ln链接:
 

复制代码代码示例:

ln -s   /etc/apache2/sites-available/a.conf /etc/apache2/sites-enabled/a.conf
ln -s   /etc/apache2/sites-available/b.conf /etc/apache2/sites-enabled/b.conf

5、重启apache
 

复制代码代码示例:

sudo /etc/init.d/apache2 restart

使ubuntu下apache2 多域名虚拟主机的配置生效。

http://www.jbxue.com/article/13135.html