转自:http://z132.com/the-virtrual-host-of-apache-in-linux/
转自:http://wiki.ubuntu.org.cn/Apache%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA%E6%8C%87%E5%8D%97
想在单台机器上设置多个域名或主机名时,我们就要用到基于名称的虚拟主机了。在用apt默认安装的系统下,在 Ubuntu 的 /etc/apache2/ 目录中有个 Apache2 的主配置文件 apache2.conf。在该文件中我们可以看到有这么一行内容:
Include /etc/apache2/sites-enabled/[^.#]*
这行的意思表明该文件包含了 /etc/apache2/sites-enabled/ 目录中文件名不含 “.” 或 “#” 这两个字符的所有文件。而在这个目录下,只有一个 000-default 的软链接文件,实际连接的是 /etc/apache2/sites-available 目录中的 default 文件,这实际上是一个通用配置文件。我们可以模仿这个来新建一个我们自己的虚拟主机配置文件。
以创建本机下的test.com主机为例:
在/etc/apache2/sites-available/下创建一个文件test.com,文件的内容如下:
<VirtualHost *:80>
ServerName www.firehare.com
ServerAdmin ******@******
DocumentRoot /home/wwwroot/test
ServerName test.com
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /home/wwwroot/test>
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 All
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/wperror.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 All
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
保存好上述内容后执行如下命令:
sudo a2ensite test.com
系统提示如下:
Enabling site test.com. To activate the new configuration, you need to run: service apache2 reload
再执行sudo service apache2 reload命令之后虚拟主机test.com就建立了,这时可在/etc/apache2/sites-enabled/目录中发现多了一个到 /etc/apache2/sites-available/test.com的软链接。好了,修改下本机host,输入test.com域名,网页将指向/home/wwwroot/test目录,一个完整的虚拟主机创建过程就算圆满完成了。
另外,可用a2dissite这个命令单独对各个虚拟主机进行管理,如执行如下命令:
sudo a2dissite test.com sudo /etc/init.d/apache2 restart
既可以维护 edunuke 这个站点,同时还不影响其他站点的正常运行。
另,mods-available 目录中的是所有可用的模块,而在 mods-enabled 目录中的则是已被安装到 Apache2 中的模块。由于在 mods-available 目录中已经有了 Rewrite 和Proxy 模块的配置引导文件,所以只需要简单地将其安装到 Apache2 中即可。使用命令:
sudo a2enmod rewrite sudo a2enmod proxy
好了,这个就记录到这边。