如何使用 Ubuntu 12.04 设置“基于名称的”虚拟主机?

Cor*_*rey 7 apache2

如何使用 Ubuntu 12.04 设置“基于名称的”虚拟主机?

我已按照https://help.ubuntu.com/12.04/serverguide/httpd.html#http-configuration 中给出的说明进行操作。

我做了以下...

  1. cp default newsite
  2. 在新站点中将“/var/www”替换为“/var/www/newsite”
  3. 将“ServerName newsite.example.com”添加到newsite

经过一番研究,我发现了一篇博客文章,其中指出我需要使用a2dissite default. 在我这样做之后,它起作用了。那是对的吗?这在 Ubuntu 服务器指南中从未提及。该指南还包括这一行...

“默认的虚拟主机没有指定 ServerName 指令,因此它将响应与另一个虚拟主机中的 ServerName 指令不匹配的所有请求。”

这似乎意味着默认站点和其他站点可以共存。

我正在运行 12.04 Server 的全新安装,并且每次进行调整时都重新加载了 apache 配置。

总之......在/etc/apache2/sites-available下添加一个新文件(添加了ServerName指令的“默认”文件的更改副本)和/etc/apache2/sites-enabled下的相应符号链接后,是是否有必要禁用或重命名默认站点符号链接以使新站点正常运行?下面给出的文档和一个答案似乎推断没有必要这样做,但如果是这样,我做错了什么?使用下面的配置,当我尝试访问 newsite.example.com 时,我得到了默认站点。

/etc/apache2/sites-available$ cat 默认

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                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>
Run Code Online (Sandbox Code Playgroud)

/etc/apache2/sites-available$ cat newsite

<VirtualHost *:80>

        DocumentRoot /var/www/newsite
        ServerName newsite.example.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/newsite/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                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

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

/etc/apache2/sites-enabled# ls -l

root@sandbox:/etc/apache2/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 26 Mar 18 09:56 000-default -> ../sites-available/default
lrwxrwxrwx 1 root root 26 Mar  7 13:36 newsite -> ../sites-available/newsite
Run Code Online (Sandbox Code Playgroud)

ari*_*ica 5

这更像是一个 apache 配置问题,而不是一个 Ubuntu 问题。

是的,您可以在一台主机上运行多个虚拟服务器,每个虚拟服务器提供不同的内容,前提是它们都映射(例如通过 DNS)到同一服务器。

可以在此处找到有关如何创建虚拟服务器(2.2 版,但此功能在版本之间没有根本变化)的官方文档:

httpd.apache.org/docs/2.2/vhosts/

简短的回答是你需要:

  • 定义你的虚拟主机
  • 包括您的主机名和它们提供的内容之间的一些映射

这是通过在一些 apache 配置文件中添加一个虚拟主机子句来完成的,例如在/etc/apache2/sites-available/000-add-my-virtual-hosts 下(名称专门设计为按字母顺序位于 000-default 名称之前)

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName hostname1.mydomain.com
    DocumentRoot /home/www/hostname1
</VirtualHost>

<VirtualHost *:80>
    ServerName hostname2.mydomain.com
    DocumentRoot /home/www/hostname2
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

请注意,如果您需要的站点已经在后者而不是前者,您可能还需要添加从/etc/apache2/sites-enabled//etc/apache2/sites-available的链接。

编辑 1:
阅读a2dissite的手册页,很明显它所做的就是从/etc/apache2/sites-enabled/ 中删除符号链接。关键是要了解处理这些配置的顺序会影响最终结果。默认站点称为000-default以便首先加载。如果它匹配所有站点,即充当“任何其他”通配符,那么您将看不到其他站点。尝试将链接重命名为更高的数字,例如999-default,以便最后加载(在其他站点匹配之后)。

编辑 2: 对于您更新的问题:是的,有必要重命名或删除默认站点,因为它的配置文件名以“000”开头,使其首先加载,并且由于通配符匹配而“接管”。我想在这一点上可以改进文档。

编辑 3: 服务器名称出现的顺序、其重要性以及更多内容记录在此 apache 页面Name-based vhost 部分中,其中一个相关句子说:

The first vhost on this list (the first vhost in the config file with the
specified IP address) has the highest priority and catches any request to
an unknown server name or a request without a Host: header field.
Run Code Online (Sandbox Code Playgroud)

后来在Observations下:

... the ordering of name-based vhosts for a specific address set is significant.
The one name-based vhosts that comes first in the configuration file has the
highest priority for its corresponding address set.
Run Code Online (Sandbox Code Playgroud)

  • 我的问题特定于基于 Ubuntu 的服务器。在遵循官方 Ubuntu 指南后,我无法运行基于名称的虚拟主机。仅显示默认站点。我必须禁用默认站点才能使我的新站点正常工作。指南中从未提到过这一点。在我看来,该指南有缺陷或遗漏了一步,但我正在寻找某人来确认或纠正我。 (3认同)