系统环境:
服务器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链接:
5、重启apache
使ubuntu下apache2 多域名虚拟主机的配置生效。