Ubuntu Apache 基于域名的多站点设置
在 Ubuntu 的/etc/apache2/sites-enabled/ 目录中发现只有一个000-default的软链接文件,实际连接的是/etc/apache2/sites-available目录中的default文件,这个文件是被配置文件 apache2.conf 所包含的。打开该文件,发现它其实是一个虚拟主机的配置文件,不过由于该文件中的虚拟主机为*,所以它实际上是一个通用配置文件。
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
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 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>
由于这是缺省配置,所以在Apache2重启之后,无论你输入DNS服务器中指向这个主机的任何域名,都会被导向gunn.cn这个缺省配置所指向的/var/www这个目录的。除非该域名被其他虚拟主机配置所用,我们再配置一个test.gunn.cn指向本机,且配置了相应的虚拟主机,这样的话,输入域名test.gunn.cn就会被对应该域名的目录中。 在/etc/apache2/sites-enabled目录中建立一个文件test
<virtualhost *:80>
ServerName test.gunn.cn
ServerAdmin [email protected]
DocumentRoot "/var/www/test/"
ErrorLog "/var/www/test/errorlog/errors.log"
CustomLog "/var/www/test/customlog/accesses.log" common
</VirtualHost>
sudo a2ensite test
sudo service apache2 restart
这样就OK了,在浏览器输入test.gunn.cn就会被指向到/var/www/test 目录