laravel Apache 2.4 多个具有不同别名和路径的站点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51410460/
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-14 17:55:00  来源:igfitidea点击:

Apache 2.4 multiple sites with different alias and path

laravelapacheubuntualiasvirtualhost

提问by Daniel Ne

I have installed Ubuntu 16.04 with apache 2.4.18 in my local network.

我已经在本地网络中安装了 Ubuntu 16.04 和 apache 2.4.18。

I want to have two diffrent websites, identified by a /blah, there for i want to work with aliases, like:

我想有两个不同的网站,由 a 标识/blah,因为我想使用别名,例如:

192.168.2.134/wiki=> /var/www/dokuwiki

192.168.2.134/wiki=> /var/www/dokuwiki

192.168.2.134/sip=> /var/www/rsip/public(Laravel)

192.168.2.134/sip=> /var/www/rsip/public(Laravel)

For that i have configured /etc/apache2/sites-available/dokuwiki.confwith:

为此,我配置/etc/apache2/sites-available/dokuwiki.conf了:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ServerName 192.168.2.134:80/wiki
    Alias /wiki /var/www/dokuwiki
    <Directory /var/www/dokuwiki>
        Order deny,allow
        Allow from all
        Options FollowSymLinks
    </Directory>
</VirtualHost>

And /etc/apache2/sites-available/rsip.confwith:

/etc/apache2/sites-available/rsip.conf与:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ServerName 192.168.2.134:80/sip
    Alias /sip /var/www/rsip/public
    <Directory /var/www/rsip/public/>
        AllowOverride All
        Require all granted
        Options FollowSymLinks
    </Directory>
</VirtualHost>

In /etc/apache2/apache2.confthere is:

/etc/apache2/apache2.conf有:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options -Indexes
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

I have enabled the two sites and reloaded apache2.

我已经启用了这两个站点并重新加载了 apache2。

Result of apachectl -Sis:

结果apachectl -S是:

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server 192.168.2.134 (/etc/apache2/sites-enabled/dokuwiki.conf:1)
         port 80 namevhost 192.168.2.134 (/etc/apache2/sites-enabled/dokuwiki.conf:1)
         port 80 namevhost 192.168.2.134 (/etc/apache2/sites-enabled/rsip.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

Result of apachectl -Mis:

结果apachectl -M是:

Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php7_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)

The problem is, the link to 192.168.2.134/wikiworks fine all the time, but the link to 192.168.2.134/sipbrings up a 404 File not foundand also maps to the wrong path, as i can see in /var/log/apache2/error.log:

问题是,指向的链接一直192.168.2.134/wiki工作正常,但是指向192.168.2.134/sipa的链接404 File not found也映射到错误的路径,正如我在/var/log/apache2/error.log以下内容中看到的:

[Wed Jul 18 22:00:16.601623 2018] [core:info] [pid 4686] [client 192.168.2.156:25896] AH00128: File does not exist: /var/www/sip/

If i try 192.168.2.134/rsip/publicit works.

如果我尝试192.168.2.134/rsip/public它的工作原理。

I think it has to do with the sequence of virtualhosts, like seen when executing apachectl -S, like shown above..

我认为这与虚拟主机的顺序有关,就像在执行时看到的apachectl -S那样,如上所示..

then if i do a2dissite dokuwiki.confand reload apache2, the link to 192.168.2.134/sipworks also fine. When i enable the 192.168.2.134/wikiagain, the 192.168.2.134/sipagain doesn't works.

然后,如果我这样做a2dissite dokuwiki.conf并重新加载 apache2,则链接192.168.2.134/sip也可以正常工作。当我192.168.2.134/wiki再次启用时,192.168.2.134/sip再次不起作用。

Is it - in general - possible to do, what i am trying to do?!

是否 - 一般来说 - 可以做,我正在尝试做的事情?!

btw: in /var/www/rsip/public/.htaccessis:

顺便说一句:在/var/www/rsip/public/.htaccess是:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        #Options -MultiViews -Indexes
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteBase /sip

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_URI} (.+)/$
#    RewriteRule ^ %1 [L,R=301]

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ dashboard/ [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

回答by Andre Gelinas

ServerName directive should not have a /blah but only [scheme://]domain-name|ip-address[:port]. You should have something like this instead :

ServerName 指令不应包含 /blah,而应包含 [scheme://]domain-name|ip-address[:port]。你应该有这样的东西:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ServerName 192.168.2.134:80
    Alias /wiki /var/www/dokuwiki
    Alias /sip /var/www/rsip/public
    <Directory /var/www/dokuwiki>
        Order deny,allow
        Allow from all
        Options FollowSymLinks
    </Directory>
    <Directory /var/www/rsip/public/>
        AllowOverride All
        Require all granted
        Options FollowSymLinks
    </Directory>
</VirtualHost>

回答by Mixas

It is not possible to use 2 VirtualHosts on the same Domain (in this case the IP) with different DocumentRoot. Apache would always use only one of the multiple defined VirtualHosts.

不可能在具有不同 DocumentRoot 的同一域(在本例中为 IP)上使用 2 个 VirtualHost。Apache 将始终仅使用多个定义的 VirtualHosts 之一。

ServerName should be only name or IP. The Port is already defined in VirtauHost *:80.

ServerName 应该只是名称或 IP。该端口已在 VirtauHost *:80 中定义。

You could to use one VirtualHost for your IP with var/www as DocumentRoot. The Alias is not needed, when both sites a placed under /var/www . Maybe a softlink on the filesystem is better than a Alias in Apache.

您可以使用一个 VirtualHost 作为您的 IP,并将 var/www 作为 DocumentRoot。当两个站点都放在 /var/www 下时,不需要别名。也许文件系统上的软链接比 Apache 中的别名更好。

Softlinks on filesystem can be made with:

文件系统上的软链接可以通过以下方式制作:

ln -s /var/www/rsip/public /var/www/sip