apache虚拟主机配置CentOS7

apache虚拟主机配置CentOS7

我的主机是 Windows 7,来宾机是 CentOS 7。我是 Apache 的新手,我正在尝试在 CentOS7 上创建 VirtualHost。我在 StackCommunity 上阅读了很多文档和很多答案(阿帕奇符号链接虚拟主机CentOS7等等。这就是我所做的。我的/etc/httpd/conf/httpd.conf文件配置没有添加任何更改:

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@localhost

ServerName localhost
<Directory />
AllowOverride none
Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

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

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

<Files ".ht*">
Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""     combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

</IfModule>CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>

<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on
IncludeOptional conf.d/*.conf

我添加了index.html,其中/var/www/html包含以下内容:

<html>
<title> The library </title>
<body>
<h2> Welcome to our library </h2>
<br /> <hr> <br />
<img width = "600" height = "400" src = "images/library.jpg">
<body/>
</html>

然后我创建了符号/var/www/html/index.html链接/var/www/my_host/my_host.html

ln -s /var/www/html/index.html /var/www/html/my_host.html

下一步:我创建my_host.conf/etc/httpd/conf.d/允许符号链接:

vim /etc/httpd/conf.d/my_host.conf

<VirtualHost *:80>
ServerName www.my_host.com
ServerAlias *.my_host.com
DocumentRoot /var/www/my_host
<Directory "/var/www/my_host">
Options FollowSymLinks Indexes
AllowOverride All
</Directory>
ErrorLog /var/www/my_host/error_log
LogFormat "%a %v %p %U %m %T" common_new_format
CustomLog /var/www/my_host/custom_log common_new_format
</VirtualHost>

然后我用这个命令检查了 apache:apachectl configtest并得到了输出 => Syntax OK。完成所有这些步骤后,我启动了 httpd:

systemctl start httpd

一切开始都很完美。

但是我的虚拟主机的 error_log 中出现了一个问题:

AH01276: Cannot serve directory /var/www/my_host/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive

我知道我可以添加DirectoryIndex my_host.html到我的my_host.conf文件中,但为什么我的符号链接 my_host.html -> ../html/index.html没有像我预期的那样工作?我认为允许FollowSymLinks在我的/var/www/my_host/目录中就足够了,我不需要指向另一个DirectoryIndex?我的问题是:如果我已经将符号链接添加到index.html,为什么还FollowSymLinks不足以指出?DirectoryIndex

答案1

我认为您对符号链接的解读太多了。httpd不会查看您是否有与index.html在这两种情况下调用的文件的符号链接的文件。它会查找index.html在您的 DocumentRoot 中命名的文件。

FollowSymLinks当此类文件存在并且是符号链接时,需要。(想象一下,如果意外或恶意,一个符号链接/etc/passwd或您的私人 SSH 密钥最终出现在您的 DocumentRoot 中!)

相关内容