Apache2 2.2.14使用多个虚拟主机进行本地测试


4

我已经通过这种方法设置了Apache 2虚拟主机。

/etc/apache2/sites-available 我创造了一个 dummy_site_1.conf

dummy_site_1.conf

<VirtualHost local.dummy_site_1:80>
    ServerName local.dummy_site_1
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/dummy_site_1
    ErrorLog /var/log/apache2/dummy_site_1__error.log
    TransferLog /var/log/apache2/dummy_site_1__access.log
    <Directory /var/www/dummy_site_1>
        AllowOverride Options Limit None
        Options -Indexes FollowSymLinks
    Order deny,allow
    Allow from all
    </Directory>
    DirectoryIndex index.php
    #DISABLE HTTP TRACE
    #RewriteEngine On
    #RewriteCond %{REQUEST_METHOD} ^TRACE
    #RewriteRule .* - [F]
</VirtualHost>

我也添加了这一行 httpd.conf

ServerName local.dummy_site_1

现在所有人都在使用这个设置,我打字 local.dummy_site_1 在URL中,我可以看到我的网站。

问题是当我尝试添加另一个站点配置时 dummy_site_2.conf 并创建大约相同的设置

dummy_site_2.conf

<VirtualHost local.dummy_site_2:80>
    ServerName local.dummy_site_2
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/dummy_site_2
    ErrorLog /var/log/apache2/dummy_site_2__error.log
    TransferLog /var/log/apache2/dummy_site_2__access.log
    <Directory /var/www/dummy_site_2>
        AllowOverride Options Limit None
        Options -Indexes FollowSymLinks
    Order deny,allow
    Allow from all
    </Directory>
    DirectoryIndex index.php
    #DISABLE HTTP TRACE
    #RewriteEngine On
    #RewriteCond %{REQUEST_METHOD} ^TRACE
    #RewriteRule .* - [F]
</VirtualHost>

我也添加了这一行 httpd.conf

ServerName local.dummy_site_2

我收到此错误:

user@host:/etc/apache2$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                                                                                                                                                                     [Wed Sep 15 14:00:43 2010] [error] (EAI 5)No address associated with hostname: Could not resolve host name local.dummy_site_2 -- ignoring!
 ... waiting [Wed Sep 15 14:00:44 2010] [error] (EAI 5)No address associated with hostname: Could not resolve host name local.dummy_site_2 -- ignoring!

为什么?我配置错了吗?

Answers:


6

你不需要' ServerName local.dummy_site_2 '在httpd.conf中,所以拿出来,但添加这个:

NameVirtualHost ip-address-of-server:80

例如:NameVirtualHost 192.168.1.10:80

完成更改后,您需要重新启动Apache。

如果事情仍然没有按预期工作,请在服务器名称行下面的每个虚拟主机配置文件中添加一行,如下所示:

ServerName local.dummy_site_1
ServerAlias *.local.dummy_site_1 local.dummy_site_1

ServerName local.dummy_site_2
ServerAlias *.local.dummy_site_2 local.dummy_site_2

再次,重新启动Apache以检查更改。

编辑:愚蠢的我 - 刚刚发现问题的另一部分 - 所有的VirtualHost声明应该使用虚拟服务器的IP地址 - 例如:

<VirtualHost 192.168.1.10:80>

感谢您的帮助,我确实踩了一下并得到了一个错误。第2步和同样的错误以及原始错误:apache2:无法可靠地确定服务器的完全限定域名,使用127.0.1.1 for ServerName
Phill Pafford

如果重要的话,我正在使用静态IP
Phill Pafford

嗯 - 如果你把NameVirtualHost行放入,我不确定错误 - 我刚检查了一个托管8个虚拟站点的服务器,并检查语法。有关完全限定域名的消息不应该停止工作,但检查/ etc / hosts是否有一行127.0.0.1列出服务器的名称,例如:mine是127.0.0.1 rusty.localdomain rusty localhost.localdomain localhost
Linker3000

看到我对原始回复的编辑 - 只是看到了拼图的最后一点!
Linker3000

嗯,这也给出了同样的错误,同样当我点击第一个网站时,它会发现来自Apache的It工作消息
Phill Pafford

1

实际上,您可以完全删除IP地址和/或站点名称。

例:

NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>
ServerAdmin emailaddress@domain.tld
DocumentRoot "/path/to/vhost/documents"
ServerName servername.domain.tld
ServerAlias serveralias.domain.tld
ErrorLog "/path/to/logfiles/servername.domain.tld-error.log"
CustomLog "/path/to/logfiles/servername.domain.tld-access.log" common
<Directory "/path/to/vhost/documents">
allow from all
Option +Indexes
</Directory>
</VirtualHost>

<VirtualHost *:443>
ServerAdmin emailaddress@domain.tld
DocumentRoot "/path/to/vhost/documents"
ServerName servername.domain.tld
ServerAlias serveralias.domain.tld
SSLEngine On
SSLCertificateFile /path/to/certificate/file.cert
SSLCertificateKeyFile /path/to/certificate/file.key
ErrorLog "/path/to/logfiles/servername.domain.tld-error.log"
CustomLog "/path/to/logfiles/servername.domain.tld-access.log" common
<Directory "/path/to/vhost/documents">
allow from all
Option +Indexes
</Directory>
</VirtualHost>

(现在,让我们举一个vhost结合mod_proxy的例子; ip = numeric-octet,下面)

<VirtualHost *:80>
ProxyPreserveHost Off
ProxyPass / http://ip.ip.ip.ip/
ProxyPassReverse / http://ip.ip.ip.ip/
ServerName servername.domain.tld
</VirtualHost>

基本上,只要您的DNS信息与您配置的虚拟主机匹配,那么它应该都可以正常工作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.