2012-09-09 60 views
40

按照我在网上可以找到的每个指南,用完了几个小时。如何在一个apache实例上运行多个站点

我希望有一个单一的Apache运行两个网站,像这样 - 192.168.2.8/site1 和 192.168.2.8/site2

我在圈子里被兜了一圈,但在那一刻我在“网站可用(符号链接到网站启用 - )”,看起来像这 -

<VirtualHost *:2000> 

ServerAdmin [email protected] 
ServerName site1 
ServerAlias site1 

# Indexes + Directory Root. 
DirectoryIndex index.html 
DocumentRoot /home/user/site1/ 

# CGI Directory 
ScriptAlias /cgi-bin/ /home/user/site1/cgi-bin/ 

Options +ExecCGI 

# Logfiles 
ErrorLog /home/user/site1/logs/error.log 
CustomLog /home/user/site1/logs/access.log combined 

</VirtualHost> 

<VirtualHost *:3000> 

ServerAdmin [email protected] 
ServerName site2 
ServerAlias site2 

# Indexes + Directory Root. 
DirectoryIndex index.html 
DocumentRoot /home/user/site2/ 

# CGI Directory 
ScriptAlias /cgi-bin/ /home/user/site2/cgi-bin/ 

Options +ExecCGI 

# Logfiles 
ErrorLog /home/user/site2/logs/error.log 
CustomLog /home/user/site2/logs/access.log combined 

</VirtualHost> 
个conf文件

的http.conf看起来像这 -

NameVirtualHost *:2000 
NameVirtualHost *:3000 

目前我得到这个错误 -

[error] VirtualHost *:80 — mixing * ports and non-* ports with a NameVirtualHostaddress is not supported, proceeding with undefined results 

Ports.conf看起来是这样的 - (虽然没有导游提到任何需要编辑这个)

NameVirtualHost *:80 

Listen 80 
<IfModule mod_ssl.c> 
# If you add NameVirtualHost *:443 here, you will also have to change 
# the VirtualHost statement in /etc/apache2/sites-available/default-ssl 
# to <VirtualHost *:443> 
# Server Name Indication for SSL named virtual hosts is currently not 
# supported by MSIE on Windows XP. 
Listen 443 
</IfModule> 

<IfModule mod_gnutls.c> 
Listen 443 
</IfModule> 

任何人都可以给一些简单的指示,让这个运行?我发现的每一个指南都是以不同的方式去做,而每一个指南都会导致不同的错误。我显然做错了事,但没有找到可能的解释。

只想要一个站点可以在端口2000上访问,另一个站点可以在端口3000上访问(或者其他,只需选择那些端口来测试)。

我运行Ubuntu服务器12.04 ...

=============

编辑

接着一个 '引导' ......

我现在已经在此设置站点可用:

<VirtualHost *:80> 
    DocumentRoot "/home/user/site1/" 
    ServerName 192.168.2.10/site1 
</VirtualHost> 

<VirtualHost *:80> 
    DocumentRoot "/home/user/site2/" 
    ServerName 192.168.2.10/site2 
</VirtualHost> 

已经将这个在apache2.conf:

ServerName site1 
ServerName site2 

已经加入这个ports.conf:

Listen 192.168.2.10:80 

==============

编辑

现在的工作,我把它放在启用了site的conf文件中:

​​

我在ports.conf中有这个:

Listen *:80 
Listen *:81 
Listen *:82 

我在apache2有这个。CONF:

ServerName site1 
ServerName site2 

我没有在我刚刚才通过试错的一整天工作的任何指南找到这个,所以我不知道这是一个很好的解决方案。但至少现在我正在努力工作。

+0

在我看来,你需要在NameVirtualHost中指定虚拟主机名。 –

+0

干杯,我已经尝试了很多事情,你能更具体地说我应该尝试放在那里吗? – Exbi

+0

经过整整一天的努力才得以发挥作用,我终于偶然发现了答案。我感到精神枯竭,我要去睡觉了。 我怀疑我的狡猾的apache黑客解决方案会对任何人都感兴趣,但我会将其编辑到我的问题的最后。 – Exbi

回答

62

你的问题是混合几个不同的概念。你开始说你想在同一个服务器上使用同一个域名运行站点,但是在不同的文件夹中。这不需要任何特殊的设置。一旦你运行了单个域,你就可以在该文件根目录下创建文件夹。

根据问题的其余部分,您真正想要做的是在同一台服务器上运行各自的站点,并使用自己的域名。

关于该主题,您可以找到的最佳文档是apache手册中的virtual host文档。

有两种类型的虚拟主机:基于名称和基于IP。基于名称允许您使用单个IP地址,而基于IP的每个站点需要不同的IP。根据您的描述,您想使用基于名称的虚拟主机。

您得到的最初错误是由于您使用的端口号不同于NameVirtualHost。如果您确实希望从80以外的端口提供站点,则每个端口都需要有一个NameVirtualHost条目。

假设你从头开始,这比看起来简单得多。

您需要做的第一件事就是告诉apache您要使用基于名称的虚拟主机。

NameVirtualHost *:80 

现在阿帕奇知道你想做什么,你可以设置你的虚拟主机定义:

<VirtualHost *:80> 
    DocumentRoot "/home/user/site1/" 
    ServerName site1 
</VirtualHost> 

<VirtualHost *:80> 
    DocumentRoot "/home/user/site2/" 
    ServerName site2 
</VirtualHost> 

注意,只要你想在同一端口上,你可以运行尽可能多的网站。 ServerName是不同的就足以告诉Apache使用哪个虚拟主机。此外,ServerName指令始终是域/主机名,不应包含路径。

如果您决定在80以外的端口上运行网站,则在访问该网站时,您必须始终在网址中包含端口号。因此,而不是去http://example.com你就必须去http://example.com:81

+0

可否请您详细说明80以外的端口的情况。我在实现这个过程中真的很困惑。 :) – user79307

+1

@accssharma默认情况下,apache(和所有其他网络服务器软件)侦听端口80上的连接。因此,浏览器在请求网站时编码为连接到端口80。如果您在80以外的端口上运行网站,则用户在请求站点时将始终需要包含端口号。我强烈建议不要这样。 – bradym

+0

@accssharma如果您选择在80以外的端口上运行站点,则需要指定端口的NameVirtualHost行,然后指定同一端口的VirtualHost容器。基本上,复制我上面的内容,并将80替换为您打算运行该站点的端口。 – bradym

3

是用虚拟主机,只要你想,你可以有很多并行程序:

打开

的/ etc/httpd的/ conf目录/ httpd的。CONF

Listen 81 
Listen 82 
Listen 83 

<VirtualHost *:81> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/site1/html 
    ServerName site1.com 
    ErrorLog logs/site1-error_log 
    CustomLog logs/site1-access_log common 
    ScriptAlias /cgi-bin/ "/var/www/site1/cgi-bin/" 
</VirtualHost> 

<VirtualHost *:82> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/site2/html 
    ServerName site2.com 
    ErrorLog logs/site2-error_log 
    CustomLog logs/site2-access_log common 
    ScriptAlias /cgi-bin/ "/var/www/site2/cgi-bin/" 
</VirtualHost> 

<VirtualHost *:83> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/site3/html 
    ServerName site3.com 
    ErrorLog logs/site3-error_log 
    CustomLog logs/site3-access_log common 
    ScriptAlias /cgi-bin/ "/var/www/site3/cgi-bin/" 
</VirtualHost> 

重新启动Apache

service httpd restart

现在,您可以参考站点1:

http://<ip-address>:81/ 
http://<ip-address>:81/cgi-bin/ 

站点2:

http://<ip-address>:82/ 
http://<ip-address>:82/cgi-bin/ 

Site3:

http://<ip-address>:83/ 
http://<ip-address>:83/cgi-bin/ 

如果路径中没有任何脚本硬编码那么你的网站应该无缝协作。

相关问题