Apache表示DocumentRoot不存在


12

我使用Webmin创建了以下虚拟主机:

<VirtualHost *:80>
        DocumentRoot "/var/www/whatever"
        ServerName whatever.ourdomain
        <Directory "/var/www/whatever">
                allow from all
                Options +Indexes
        </Directory>
</VirtualHost>

当重新启动Apache时,我得到

Starting httpd: Warning: DocumentRoot [/var/www/whatever] does not exist

问题是,该目录绝对存在。我盯着它看。pwd告诉我这是我当前的目录,等等。拼写正确并不难。我在httpd日志中找不到其他任何错误或警告。apache:apache拥有目录和所有子目录/文件。这里没有任何符号链接或任何相关内容。我想念什么,或者应该看什么以确定为什么?

操作系统是CentOS 6.0


su给Apache用户并查看它是否可以访问DocumentRoot,这可能使您对Web服务器正在查看的内容有所了解。您可能还想检查路径中的其他目录,尽管如果确实在/var/www/这些目录下应该不成问题
voretaq7 2011年

Answers:


8

我想到的第一件事是Apache是​​否有权访问该目录?

另外,这是:https : //stackoverflow.com/questions/3948038/apache-says-my-documentroot-directory-doesnt-exist


1
就像我说的,是目录归所有者所有apache:apache,但是我点击了该链接(出于某种原因在SO上?),而SELinux确实是问题所在。SELinux会带来更多问题,因为它可以很好地处理imo。
杰克·威尔逊

selinux最初很烦人,但是如果您知道管理访问的命令,它实际上并不那么令人生畏。这是一个很好的工具,一旦您习惯了就可以使用。
Rilindo 2011年

我遇到了相同的问题(符号链接上不存在),并对其进行了setenforce 0修复,但是查看权限ls-laZ,符号链接与它可以访问的其他文件具有相同的权限,除了chmod之外。文件是-rw-r--r--,符号链接是lrwxrwxrwx。这可能是它不适用于setenforce 1的原因吗?
TMH 2014年

@JakeWilson SELinux在您第一次习惯它时会感到非常沮丧。您越了解如何使用它,我保证您会欣赏它的更多。
斯宾塞·威廉姆斯

16

这是SELinux案例的教学方法:

找出SELinux是否处于活动状态:

 $ sestatus
 SELinux status:                 enabled
 SELinuxfs mount:                /selinux
 Current mode:                   enforcing
 Mode from config file:          enforcing
 Policy version:                 24
 Policy from config file:        targeted

如果是这样,进行一些比较检查可能会有所帮助。例如,服务器的默认DocumentRoot位于/var/www/html,但我们希望它在其他位置/path/to/document/root

如果SELinux没有主动弄乱资源,ls -dZ则目录上将显示类似以下内容:

$ ls -dZ /path/to/document/root
? /path/to/document/root/

另一方面,如果应用SELinux上下文,则ls -dZ看起来更像:

$ ls -dZ /path/to/document/root
drwxrws--x+ cfgadm cfgadmin system_u:object_r:file_t:s0 /path/to/document/root

如果我们将其与正常工作的DocumentRoot相比较,它将看起来像:

$ ls -dZ /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html

_r_t涉及到-r--role-t--type)参数chcon这里是一个切下的手册页:

NAME
   chcon - change file security context

SYNOPSIS
   chcon [OPTION]... CONTEXT FILE...
   chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...
   chcon [OPTION]... --reference=RFILE FILE...

DESCRIPTION
   Change the security context of each FILE to CONTEXT.  With --reference,
   change the security context of each FILE to that of RFILE.

   --reference=RFILE
          use RFILE's security context rather than  specifying a CONTEXT value

   -R, --recursive
          operate on files and directories recursively

乍一看,以下似乎有效,但可能无效。

$ sudo chcon -R -t httpd_sys_content_t /path/to/document/root

如果Web服务器仍然看不到DocumentRoot,请注意上下文一直到根都很重要:

$ sudo chcon -R -t httpd_sys_content_t /path/to/document
$ sudo chcon -R -t httpd_sys_content_t /path/to
$ sudo chcon -R -t httpd_sys_content_t /path

此时,Web服务器可以看到目录。

是的,我今晚学会了艰难的方法。

注意:从RedHat文档(5.6.1。临时更改:chcon)来看,使用chcon在概念上有一个缺点,其中指出:

The chcon command changes the SELinux context for files. However, changes
made with the chcon command do not survive a file system relabel, or the
execution of the restorecon command.

使用semanagerestorecon进行更永久的更改。一个简单的例子:

 $ sudo semanage fcontext --add -t httpd_sys_content_t -s system_u \
     "/path/to/document/root(/.*)?"
 $ sudo restorecon -FR /path/to/document/root

关于restorecon,请注意,需要-F来影响整个上下文(即用户和类型)。同样,-R意味着递归进行更改。参数-v-p可以以冗长或简洁的方式显示进度。使用-FRnv来查看会发生什么,而无需实际进行任何更改。

一旦semanage的是采用这种方式,就可以查看用以下命令本地安全更改:

$ sudo semanage export

语义存储导出的输出可以保存并由语义存储导入使用,以使其更容易将一组更改应用于各种系统。

注意:此答案为站点提供了最基本的类型上下文。安全性可以更加精细。例如,使用以下命令查看可应用于Web服务器页面的类型列表:

$ seinfo -t | grep http

注意:默认情况下可能未安装诸如semanageseinfo之类的实用程序。至少在某些发行版中,必需的软件包可能会这样命名:

policycoreutils-python
setools-console

详细,有效并节省大量时间-谢谢!
NorthBridge

6

听起来像SELinux。我建议您使用它。在/ var / log / audit目录中进行确认。

更糟糕的是,您可以始终关闭selinux,如前所述,但是我建议您改为使用它。例如,如果要创建一个供Apache使用的目录,则该目录将没有正确的上下文,如此处所述。

[root@amp23140 www]# ls -Z
drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 error
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 icons
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 whatever

因此,如果发生这种情况,我只需从另一个目录(在本例中为html)中应用上下文即可:

[root@amp23140 www]# chcon whatever --reference=html
[root@amp23140 www]# ls -lZ
drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 error
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 icons
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 whatever

0

在根目录中使用此命令来更改“ httpd_sys_content_t”的安全上下文,该上下文允许Apache执行。

chcon -R -h -t httpd_sys_content_t /var/www/whatever

使用ls -dZ /var/www/whatever查看详细信息安全角色

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.