本文介紹下,在ubuntu系統中的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