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

Apache 2.4 multiple sites with different alias and path

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

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

192.168.2.134/wiki => /var/www/dokuwiki

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

为此,我已将 /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>

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

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

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

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

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)

问题是,link 到 192.168.2.134/wiki 一直工作正常,但是 link 到 192.168.2.134/sip 会出现 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/

如果我尝试 192.168.2.134/rsip/public 它有效。

我认为这与虚拟主机的顺序有关,如执行 apachectl -S 时所见,如上所示..

然后,如果我执行 a2dissite dokuwiki.conf 并重新加载 apache2,link 到 192.168.2.134/sip 也可以正常工作。当我再次启用 192.168.2.134/wiki 时,192.168.2.134/sip 再次不起作用。

一般来说,我想做的事情是否可行?!

顺便说一句:在 /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>

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>

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

ServerName 只能是名称或IP。端口已经在 VirtauHost 中定义 *:80.

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

文件系统上的软链接可以使用:

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