如何在一个apache实例上运行多个站点

Exb*_*xbi 48 configuration web-applications apache2

我可以在网上找到每个指南后花费的时间.

我希望在一个apache实例上运行两个站点,如下所示 - 192.168.2.8/site1和192.168.2.8/site2

我一直在圈子里,但是目前我在'sites-available(符号链接到网站启用)'中有两个conf文件,看起来像这样 -

<VirtualHost *:2000>

ServerAdmin webmaster@site1.com
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>
Run Code Online (Sandbox Code Playgroud)

<VirtualHost *:3000>

ServerAdmin webmaster@site2.com
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>
Run Code Online (Sandbox Code Playgroud)

http.conf看起来像这样 -

NameVirtualHost *:2000
NameVirtualHost *:3000
Run Code Online (Sandbox Code Playgroud)

目前我收到这个错误 -

[error] VirtualHost *:80 — mixing * ports and non-* ports with a NameVirtualHostaddress is not supported, proceeding with undefined results
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

任何人都可以给出一些简单的指令来运行吗?我发现的每一个指南都说不同的方式,每一个导致不同的错误.我显然做错了什么,但没有找到可能是什么的明确解释.

只想在端口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>
Run Code Online (Sandbox Code Playgroud)

在apache2.conf中设置了这个:

ServerName site1
ServerName site2
Run Code Online (Sandbox Code Playgroud)

已将此添加到ports.conf:

Listen 192.168.2.10:80
Run Code Online (Sandbox Code Playgroud)

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

编辑

它现在有效,我把它放在启用网站的conf文件中:

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

<VirtualHost *:82>
    DocumentRoot "/home/user/site2/"
    ServerName site2
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我在ports.conf中有这个:

Listen *:80
Listen *:81
Listen *:82
Run Code Online (Sandbox Code Playgroud)

我在apache2.conf中有这个:

ServerName site1
ServerName site2
Run Code Online (Sandbox Code Playgroud)

我没有在任何指南中找到这个,我只是通过一整天的试验和错误,所以我不知道这是一个很好的解决方案.但它至少是我现在想要的方式.

bra*_*dym 70

你的问题是混合了几个不同的概念.您开始说您希望使用相同的域在同一服务器上运行站点,但是在不同的文件夹中.这不需要任何特殊设置.一旦您运行单个域,您只需在该docroot下创建文件夹.

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

您将在该主题中找到的最佳文档是apache手册中的虚拟主机文档.

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

您获得的初始错误是由于您使用的不同端口而不是NameVirtualHost线路.如果您确实希望从80以外的端口提供站点,则需要NameVirtualHost为每个端口提供一个条目.

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

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

NameVirtualHost *:80
Run Code Online (Sandbox Code Playgroud)

既然apache知道你想做什么,你可以设置你的vhost定义:

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

<VirtualHost *:80>
    DocumentRoot "/home/user/site2/"
    ServerName site2
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

请注意,您可以在同一端口上运行任意数量的站点.该NameVirtualHost是不同的,就足以告诉Apache虚拟主机使用.此外,该ServerName指令始终是域/主机名,并且永远不应包含路径.

如果您决定在80以外的端口上运行站点,则在访问站点时,您始终必须在URL中包含端口号.所以,不要去http://example.com,你必须去http://example.com:81


Sri*_*nth 5

是的,对于Virtual Host,您可以根据需要拥有任意数量的并行程序:

打开

/etc/httpd/conf/httpd.conf

Listen 81
Listen 82
Listen 83

<VirtualHost *:81>
    ServerAdmin webmaster@site1.com
    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 webmaster@site2.com
    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 webmaster@site3.com
    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>
Run Code Online (Sandbox Code Playgroud)

重新启动Apache

service httpd restart

您现在可以参考Site1:

http://<ip-address>:81/ 
http://<ip-address>:81/cgi-bin/
Run Code Online (Sandbox Code Playgroud)

Site2:

http://<ip-address>:82/
http://<ip-address>:82/cgi-bin/
Run Code Online (Sandbox Code Playgroud)

Site3:

http://<ip-address>:83/ 
http://<ip-address>:83/cgi-bin/
Run Code Online (Sandbox Code Playgroud)

如果路径未在任何脚本中进行硬编码,则您的网站应无缝运行。