如何在一个 apache 实例上运行多个站点 [英] How to run multiple sites on one apache instance

查看:30
本文介绍了如何在一个 apache 实例上运行多个站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照我在网上能找到的每一个指南,我花了好几个小时转圈子.

Spent hours going in circles following every guide I can find on the net.

我想在一个 apache 实例上运行两个站点,就像这样 -192.168.2.8/site1和192.168.2.8/site2

I want to have two sites running on a single apache instance, something like this - 192.168.2.8/site1 and 192.168.2.8/site2

我一直在兜圈子,但目前我在可用站点(符号链接到启用站点)"中有两个 conf 文件,看起来像这样-

I’ve been going round in circles, but at the moment I have two conf files in ‘sites-available (symlinked to sites-enabled)’ that look like this-

<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>

<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>

http.conf 看起来像这样-

http.conf looks like this-

NameVirtualHost *:2000
NameVirtualHost *:3000

目前我收到此错误-

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

Ports.conf 看起来像这样——(虽然没有指南提到需要编辑这个)

Ports.conf looks like this – (although no guides have mentioned any need to edit this)

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>

谁能给出一些简单的说明来运行它?我找到的每个指南都说要以不同的方式来做,每个指南都会导致不同的错误.我显然做错了什么,但没有找到明确的解释.

Can anyone give some simple instructions to get this running? Every guide I’ve found says to do it a different way, and each one leads to different errors. I'm obviously doing something wrong but have found no clear explanation of what that might be.

只希望一个站点可在端口 2000 上访问,而另一个站点可在端口 3000 上访问(或其他任何端口,只需选择这些端口进行测试即可).

Just want one site accessible on port 2000 and the other accessible on port 3000 (or whatever, just picked those ports to test with).

我正在运行 Ubuntu 服务器 12.04...

I’m running Ubuntu server 12.04…

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

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

遵循另一个指南"...

Followed another 'guide'...

我现在已经在可用站点中进行了设置:

I've now set this up in sites-available:

<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 中设置:

Have set this in apache2.conf:

ServerName site1
ServerName site2

已将此添加到ports.conf:

Have added this to ports.conf:

Listen 192.168.2.10:80

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

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

它现在可以工作了,我把它放在启用站点的 conf 文件中:

It now works, I put this in a conf file in site-enabled:

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

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

我在ports.conf中有这个:

I have this in ports.conf:

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

我在 apache2.conf 中有这个:

I have this in apache2.conf:

ServerName site1
ServerName site2

我在任何指南中都没有找到这个,我只是通过一整天的反复试验使其工作,所以我不知道这是否是一个好的解决方案.但它至少现在按照我想要的方式工作.

I didn't find this in any guides I just got it working through an entire day of trial and error so I don't know if this is a good solution. But it's at least working how I want it to now.

推荐答案

您的问题混合了几个不同的概念.你一开始说你想在同一台服务器上使用相同的域运行站点,但在不同的文件夹中.这不需要任何特殊设置.运行单个域后,您只需在该文档根目录下创建文件夹即可.

Your question is mixing a few different concepts. You started out saying you wanted to run sites on the same server using the same domain, but in different folders. That doesn't require any special setup. Once you get the single domain running, you just create folders under that docroot.

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

Based on the rest of your question, what you really want to do is run various sites on the same server with their own domain names.

关于该主题的最佳文档是 虚拟主机apache 手册中的文档.

The best documentation you'll find on the topic is the virtual host documentation in the apache manual.

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

There are two types of virtual hosts: name-based and IP-based. Name-based allows you to use a single IP address, while IP-based requires a different IP for each site. Based on your description above, you want to use name-based virtual hosts.

您得到的初始错误是由于您使用了与 NameVirtualHost 行不同的端口.如果您确实希望从 80 以外的端口为站点提供服务,则需要为每个端口设置一个 NameVirtualHost 条目.

The initial error you were getting was due to the fact that you were using different ports than the NameVirtualHost line. If you really want to have sites served from ports other than 80, you'll need to have a NameVirtualHost entry for each port.

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

Assuming you're starting from scratch, this is much simpler than it may seem.

如果您使用的是 2.3 或更早版本,您需要做的第一件事就是告诉 Apache 您将使用基于名称的虚拟主机.

If you are using 2.3 or earlier, the first thing you need to do is tell Apache that you're going to use name-based virtual hosts.

NameVirtualHost *:80

如果您使用的是 2.4 或更高版本,请不要添加 NameVirtualHost 行.Apache 2.4 版 弃用了 NameVirtualHost 指令,并且会在以后的版本中移除.

If you are using 2.4 or later do not add a NameVirtualHost line. Version 2.4 of Apache deprecated the NameVirtualHost directive, and it will be removed in a future version.

现在您的虚拟主机定义:

Now your vhost definitions:

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

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

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

You can run as many sites as you want on the same port. The ServerName being different is enough to tell Apache which vhost to use. Also, the ServerName directive is always the domain/hostname and should never include a path.

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

If you decide to run sites on a port other than 80, you'll always have to include the port number in the URL when accessing the site. So instead of going to http://example.com you would have to go to http://example.com:81

这篇关于如何在一个 apache 实例上运行多个站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆