Centos

apache虛擬主機配置CentOS7

  • June 28, 2016

我的主機是 Windows 7,來賓機器是 CentOS 7。我是 Apache 的新手,我正在嘗試在 CentOS7 上創建 VirtualHost。我已經閱讀了很多關於 StackCommunity ( apache symlinks )虛擬主機 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然後我為in創建了符號連結/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/允許Symlink

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並得到了 output => 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

我認為您對符號連結的閱讀過多。httpd如果您有文件符號連結到index.html在任何一種情況下呼叫的文件,則不會查看。它會查找在 DocumentRoot 中命名index.html的文件。

FollowSymLinks這樣的文件存在並且是符號連結時需要 。(想像一下,如果出於偶然或惡意,一個符號連結,/etc/passwd或者您的私人 SSH 密鑰最終出現在您的 DocumentRoot 中!)

引用自:https://unix.stackexchange.com/questions/258712