a2ensite 站点名
a2dissite 站点名
安装好apache之后默认有一个叫default的虚拟主机。新建虚拟主机时能够直接复制默认虚拟主机的配置文件,在其基础上修改新虚拟主机的配置参数。apache
#copy /etc/apache2/site-available/default /etc/apache2/site-available/sitename
咱们都知道,若是咱们想在单台机器上设置多个域名或主机名时,咱们就要用到基于名称的虚拟主机了。那么要如何进行设置呢?这就是本文章想解决的问题了。在 Ubuntu 的 /etc/apache2/ 目录下有个 Apache2 的主配置文件 apache2.conf。在该文件中咱们能够看到下列字段:浏览器
# Include the virtual host configurations: Include /etc/apache2/sites-enabled/[^.#]*(12.04版本里无[^.#]*)
这行的意思代表该文件包含了 /etc/apache2/sites-enabled/ 目录中文件名不含 "." 或 "#" 这两个字符的全部文件。而当咱们列出该目录的文件时,发现只有一个 000-default 的软连接文件,实际链接的是 /etc/apache2/sites-available 目录中的 default 文件,不难看出该文件的文件名中并不包含 "." 或 "#"。因此这个文件固然是要被配置文件 apache2.conf 所包含的了。打开该文件,发现它实际上是一个虚拟主机的配置文件,不过因为该文件中的虚拟主机为 *,因此它其实是一个通用配置文件。若是咱们要创建虚拟主机的话,那么就要把该文件改为以下所示:服务器
<VirtualHost *:80> ServerName www.example.com ServerAdmin admin@mail.example.com DocumentRoot /var/www/ <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny Allow from all # This directive allows us to have apache2's default start page # in /apache2-default/, but still have / go to the right place # Commented out for Ubuntu #RedirectMatch ^/$ /apache2-default/ </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 /var/log/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access.log combined ServerSignature On 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>
下面咱们来分析一下上面这段设置中与虚拟主机有关的设置语句:ide
这样咱们就配置了一个虚拟主机 www.firehare.com。但因为这是缺省配置,因此在 Apache2 重启以后,不管你输入 DNS 服务器中指向这个主机的任何域名,都会被导向 www.firehare.com 这个缺省配置所指向的 /var/www 这个目录的。除非该域名被其余虚拟主机配置所用,好比咱们还配置了 edunuke.firehare.com 指向本机,且配置了相应的虚拟主机,这样的话,输入域名 edunuke.firehare.com 就会被对应该域名的目录中。测试
为了说明清楚 咱们再添加一个虚拟主机站点 example.com,首先到 /etc/apache2/sites-available/ 目录中创建一个文件 edunuke,编辑该文件:spa
<VirtualHost *:80> ServerName edunuke.example.com ServerAdmin edunuke@mail.example.com DocumentRoot "/var/www/edunuke/" ErrorLog "/var/log/apache2/edunuke_errors.log" CustomLog "/var/log/apache2/edunuke_accesses.log" common </VirtualHost>
设置的具体含义同上面的类似,这是我就再也不多说了。而后再运行命令:操作系统
sudo a2ensite edunuke
注:我这里会提示从新加载配置,按照提示信息执行便可使配置文件生效(通过本人测试,使用重启apache的方式没有成功加载配置文件):debug
这样的话,虚拟主机站点 edunuke.example.com 就已经安装好了。这时你也能够在 /etc/apache2/sites-enabled/ 目录中发现多了一个到 /etc/apache2/sites-available/edunuke 的软连接。接下来就是将 Apache2 重启来使虚拟主机站点运行起来:rest
sudo /etc/init.d/apache2 restart
这样你在浏览器上输入 edunuke.example.com 的话,就会被指向 /var/www/edunuke 目录了,而输入其余指向本机的域名则都会指到缺省配置中的 /var/www 目录中。熟悉 Apache2 的朋友会问为何这样麻烦,放在一个文件中不也是能够吗?为何要用两个文件呢?其实很简单,由于若是我要对 edunuke 站点进行维护时,我只要运行命令:code
sudo a2dissite edunuke
sudo /etc/init.d/apache2 restart
便可,这样既能够维护 edunuke 这个站点,同时还不影响其余站点的正常运行。
上面谈了一下简单的虚拟主机配置方法。这个基本上能知足咱们大部分的须要。但若是要是安装 Zope+Plone 的话,上面的这点设置是远远不够的,因为 Zope+Plone 结构所采用的端口并不是是80端口,因此咱们还得作端口重定向。为了可以作这个,咱们得激活 Rewrite 和 Proxy 两个模块。激活模块很简单,同站点配置目录同样,在 Apache2 中也有两个模块配置目录:mods-available 和 mods-enabled。在 mods-available 目录中的是全部可用的模块,而在 mods-enabled 目录中的则是已被安装到 Apache2 中的模块。因为在 mods-available 目录中已经有了 Rewrite 和 Proxy 模块的配置引导文件,因此只须要简单地将其安装到 Apache2 中便可。使用命令:
sudo a2enmod rewrite
sudo a2enmod proxy
而后,添加虚拟主机站点 www.songchaoke.cn,同 edunuke 站点建立类似在/etc/apache2/sites-available/ 目录中创建一个文件 plone。显然这个文件名中是没有 "." 或 "#" 这两个字符的了。而后编辑该文件:
<VirtualHost www.songchaoke.cn:80> ServerName www.songchaoke.cn ServerAdmin francis@mail.songchaoke.cn ErrorLog "/var/log/apache2/plone_errors.log" CustomLog "/var/log/apache2/plone_accesses.log" common RewriteEngine on RewriteRule ^/(.*) http://127.0.0.1:8081/VirtualHostBase/http/www.songchaoke.cn:80/plone/VirtualHostRoot/$1 [L,P] <Proxy *> Order Deny,Allow Deny from all Allow from all </Proxy> </VirtualHost>
这样就安装好了 www.songchaoke.cn 虚拟主机站点,能够在浏览器中地址栏中输入 http://www.songchaoke.cn 就能够重定向到站点去了。