Apache 2 站点可用配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1939686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:31:18  来源:igfitidea点击:

Apache 2 Sites-Available Configuration

apachevirtualhosts

提问by Schodemeiss

I am trying to write about 5 websites all on one Apache server which is all on one IP address.

我正在尝试在一台 Apache 服务器上编写大约 5 个网站,而这些服务器都在一个 IP 地址上。

For example:

例如:

  • /var/www/site1
  • /var/www/site2
  • /var/www/site3
  • /var/www/site4
  • /var/www/site5
  • /var/www/site1
  • /var/www/site2
  • /var/www/site3
  • /var/www/site4
  • /var/www/site5

However, if I create a link on site 2 just using, for example. /index.php, you would expect it to look in /var/www/site2/index.php... but it actually resolves to /var/www/index.php.

但是,如果我在站点 2 上创建一个链接只是使用,例如。/index.php,你会期望它在/var/www/site2/index.php 中查找...但它实际上解析为/var/www/index.php。

Is there anyway of setting up Apache to know that the separate site folders need to behave and resolve to the separate folders?

无论如何设置Apache以知道单独的站点文件夹需要表现并解析为单独的文件夹?

This is my sites-available file at the moment. A fairly default setup I believe:

这是目前我的站点可用文件。我相信一个相当默认的设置:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride all
            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 /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/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>

Any help would be apreciated.

任何帮助将不胜感激。

Thank-you kindly.

非常感谢你。

Andrew Barlow

安德鲁·巴洛

回答by Zeograd

You need a ServerName directive inside the <VirtualHost>. It will tell the server which virtual host is currently in use depending on the browser request (if your client access http://site1.example.comor http://site2.example.com, they will connect to the same IP, hence server, but the request contains the original request url). You'll have to duplicate your <VirtualHost>block to have one per hosted site. Each block will differ by their ServerName and DocumentRoot mainly. You can use "apache2ctl -S" to see how apache understood your virtual host settings.

您需要在<VirtualHost>. 它会根据浏览器请求告诉服务器当前正在使用哪个虚拟主机(如果您的客户端访问http://site1.example.comhttp://site2.example.com,它们将连接到相同的 IP,因此是服务器,但请求包含原始请求 url)。您必须复制您的<VirtualHost>块才能在每个托管站点上拥有一个。每个块的不同之处主要在于它们的 ServerName 和 DocumentRoot。您可以使用“apache2ctl -S”来查看 apache 如何理解您的虚拟主机设置。

You can use a single file with this kind of content :

您可以使用具有此类内容的单个文件:

<VirtualHost *:80>
  ServerName site1.myserver.com
  DocumentRoot /var/www/site1
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site2.myserver.com
  DocumentRoot /var/www/site2
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site3.myserver.com
  DocumentRoot /var/www/site3
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site4.myserver.com
  DocumentRoot /var/www/site4
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName site5.myserver.com
  DocumentRoot /var/www/site5
  ...
</VirtualHost>

Of course, maybe sure that the dns for all of those name ends up on your IP. It needs not to be subdomains as long as they land on your server ip and you have a correspond ServerName for it. If you need extra names for a single site, you can add them with "ServerAlias secondname.myserver.com thirdname.myserver.com" below ServerName

当然,也许可以确定所有这些名称的 dns 最终都在您的 IP 上。它不需要是子域,只要它们落在您的服务器 ip 上并且您有相应的 ServerName 即可。如果您需要单个站点的额外名称,您可以在 ServerName 下方添加“ServerAlias secondname.myserver.comthirdname.myserver.com”

回答by bigmike7801

@Zeograd's answer is probably the correct answer for most solutions, however I had a similar issue and all I needed to do was enable the rewrite module with the a2enmodcommand.

@Zeograd 的答案可能是大多数解决方案的正确答案,但是我遇到了类似的问题,我需要做的就是使用a2enmod命令启用重写模块。

This is what I ran via command line in Ubuntu:

这是我在Ubuntu 中通过命令行运行的:

sudo a2enmod rewrite
sudo service apache2 restart

If sudo service apache2 restartdoesn't work, you can try:

如果sudo service apache2 restart不起作用,您可以尝试:

sudo /etc/init.d/apache2 restart