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

时间:2018-07-18 20:26:21

标签: laravel apache ubuntu alias virtualhost

我已经在本地网络中安装了Apache 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)

问题是,到192.168.2.134/wiki的链接一直都能正常工作,但是到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,则指向192.168.2.134/sip的链接也可以正常工作。当我再次启用192.168.2.134/wiki时,192.168.2.134/sip再次无效。

一般来说,这是可能的吗,我想做什么?!

btw:/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/$1 [L,R=301]

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

2 个答案:

答案 0 :(得分:3)

ServerName指令不应包含/ blah,而应仅包含[scheme://]域名| ip地址[: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>

答案 1 :(得分:0)

不能在同一域(在本例中为IP)上将2个虚拟主机与不同的DocumentRoot一起使用。 Apache将始终只使用多个已定义的VirtualHost之一。

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

您可以将一台VirtualHost用于IP地址,并将var / www作为DocumentRoot。 如果两个站点都位于/ var / www下,则不需要别名。 也许文件系统上的软链接比Apache中的别名更好。

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

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