与机器同名的 Apache2 VirtualHost

Zer*_*ges 6 virtualhost apache-2.4

我正在使用 Debian 的具有规范名称的服务器(/etc/hostname在反向 DNS 中定义)server.example.com

root@server:~# uname -a
Linux server.example.com 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1 (2015-05-24) x86_64 GNU/Linux

Apache/2.4.10 (Debian) 服务器在 server.example.com 端口 80

现在我安装了 apache2 并做了一些基本的配置。然后我/etc/apache2/sites-availablesites-enabled. 一切工作正常,除了VirtualHostServerName server.example.com。当我尝试在资源管理器中访问该 URI 时,我获得了默认虚拟主机的内容(未ServerName指定)。如果我将server.example.com.conf的 ServerName更改为不同的内容(如test.example.com)并重新加载 apache 配置,那么我可以毫无问题地访问 URI。我想知道 VirtualHostServerName与机器的规范名称相同有什么问题。

此外,server.example.com.conf还有DocumentRoot /var/www/subs/server其中包含index.htmlmunin文件夹。默认 VirtualHost 中DocumentRoot /var/www/html没有munin文件夹。但是,当我尝试访问时,http://server.example.com/munin我可以进入/var/www/subs/server/munin, whilehttp://server.example.com/index.html带我/var/www/html/index.html.

最后,当我Redirect 404 /输入默认的 VirtualHost 时,404当我尝试访问这两个http://server.example.com/http://server.example.com/munin

添加了 VirtualHosts 的配置

/etc/apache2/sites-available/forum.example.com.conf

# Working subdomain configuration
<VirtualHost *:80>
        ServerName forum.example.com
        ServerAdmin zereges@example.com
        DocumentRoot /var/www/subs/forum
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

/etc/apache2/sites-available/server.example.com.conf

# Not working server.example.com configuration
<VirtualHost *:80>
        ServerName server.example.com
        ServerAdmin zereges@example.com
        DocumentRoot /var/www/subs/server
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

/etc/apache2/sites-available/default.conf

# Default VirtualHost configuration 
<VirtualHost *:80>
        ServerAdmin zereges@example.com
        DocumentRoot /var/www/html
        #Redirect 404 /
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

root@server:~# apache2ctl -t
语法OK


添加了输出 root@server:~# apache2 -S

[Tue Aug 11 11:07:32.705684 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOCK_DIR} is not defined
[Tue Aug 11 11:07:32.706119 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_PID_FILE} is not defined
[Tue Aug 11 11:07:32.706250 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_RUN_USER} is not defined
[Tue Aug 11 11:07:32.706406 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_RUN_GROUP} is not defined
[Tue Aug 11 11:07:32.706566 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
(... repeating few more times)
[Tue Aug 11 11:07:32.744292 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOG_DIR} is not defined

AH00526: Syntax error on line 74 of /etc/apache2/apache2.conf:
Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}
Run Code Online (Sandbox Code Playgroud)

尽管,

/etc/apache2/apache2.conf:74:Mutex file:${APACHE_LOCK_DIR} default
/etc/apache2/envvars:21:export APACHE_LOCK_DIR=/var/lock/apache2$SUFFIX
Run Code Online (Sandbox Code Playgroud)


运行后/etc/apache2/envvar我通过执行得到这个apache2 -S

VirtualHost configuration:
*:80     is a NameVirtualHost
         default server server.example.com (/etc/apache2/sites-enabled/default.conf:1)
         port 80 namevhost server.example.com (/etc/apache2/sites-enabled/default.conf:1)
         port 80 namevhost download.example.com (/etc/apache2/sites-enabled/download.example.com.conf:1)
         port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1)
                 alias www.example.com
         port 80 namevhost forum.example.com (/etc/apache2/sites-enabled/forum.example.com.conf:1)
         port 80 namevhost forumold.example.com (/etc/apache2/sites-enabled/forumold.example.com.conf:1)
         port 80 namevhost forumtest.example.com (/etc/apache2/sites-enabled/forumtest.example.com.conf:1)
         port 80 namevhost mail.example.com (/etc/apache2/sites-enabled/mail.example.com.conf:1)
         port 80 namevhost ro.example.com (/etc/apache2/sites-enabled/ro.example.com.conf:1)
         port 80 namevhost server.example.com (/etc/apache2/sites-enabled/server.example.com.conf:1)
         port 80 namevhost anotherexample.com (/etc/apache2/sites-enabled/anotherexample.com.conf:1)
                 alias www.anotherexample.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: 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
Run Code Online (Sandbox Code Playgroud)

cov*_*ner 4

在基于 debian 的发行版上,默认虚拟主机根本不指定 ServerName,这实际上意味着它最终以系统主机名作为其 ServerName。

由于它首先列出,因此它将覆盖具有相同 ServerName 的任何后续虚拟主机。

我建议禁用附带的默认虚拟主机并添加您自己的指定服务器名称但不使用您感兴趣的服务器名称。