apache 不会从另一个挂载索引文件夹

use*_*406 7 permissions mount indexing apache2 apache2.4

我正在尝试为 Web 根目录之外的文件夹启用目录列表,该文件夹来自使用基本身份验证的不同本地 ext4 安装,但我得到一个空列表并且没有记录错误。奇怪的是,如果我在浏览器中将文件的已知位置放在此目录下,它会很好地下载文件。

在此处输入图片说明

这是我的example.conf文件:

<virtualhost *:80>

  ServerAdmin donotreply@blah.com
  ServerName  example.com
  ServerAlias www.example.com


  DirectoryIndex index.php
  DocumentRoot /var/www/example.com
    <Directory />
    Options FollowSymLinks
    AllowOverride All
    </Directory>

  LogLevel warn
  ErrorLog  /var/apachelogs/error.log
  CustomLog /var/apachelogs/access.log combined

  Alias /blah2 "/blah1/blah2"
    <Location /blah2>
              Options +Indexes +MultiViews +FollowSymLinks
              IndexOptions +FancyIndexing
    </Location>


</virtualhost>
Run Code Online (Sandbox Code Playgroud)

这是我的 .htaccess

AuthType Basic
AuthName "Authentication Required"
AuthUserFile "/home/myusername/.htpasswd"
Require valid-user
Run Code Online (Sandbox Code Playgroud)

另外,我已经IndexIgnore/etc/apache2/mods-enabled/autoindex.conf

#IndexIgnore .??* *~ *# RCS CVS *,v *,t
Run Code Online (Sandbox Code Playgroud)

我已经运行chmod -R 755 /blah1/blah2,chgrp -R www-data /blah1/blah2chmod a+x -R /blah1/blah2。文件夹所有者是 www-data 的成员。如果我运行,sudo usermod -a -G www-data myusername我可以很好地浏览和读取所有文件和文件夹。

做一些测试,如果我将 /blah1/blah2 移动到我的主目录下并更改别名,我的配置工作正常。尽管 apache 可以清楚地读取文件本身,但它在另一个挂载上的东西会弄乱 mod_autoindex。删除身份验证无济于事。随着LogLevel warn我没有得到任何记录的错误。将我的 LogLevel 更改为 trace4 后,这是我的错误日志

这是来自的安装线/etc/fstab

UUID=[theuuid] /blah1 ext4 rw,nosuid,nodev,errors=remount-ro    0    0
Run Code Online (Sandbox Code Playgroud)

编辑 最后注意:确认 www-data 可以读取和写入我的文件夹,我制作了以下 php 脚本:

<?php

mkdir ("testdir");
var_dump(scandir('.'));

?>
Run Code Online (Sandbox Code Playgroud)

结果:目录 testdir 使用所有者 www-data:www-data 创建,目录和文件列表作为变量转储。

EDIT2 我已经运行以下命令来正确设置权限:

chmod 755 /blah1/blah2
chmod 755 /blah1
find /blah1/blah2 -type d -exec chgrp www-data {} +
find /blah1/blah2 -type d -exec chmod o+rx {} +
find /blah1/blah2 -type d -exec chmod g+rwxs {} +
Run Code Online (Sandbox Code Playgroud)

结果还是一样。

pa4*_*080 2

您的配置中有几处可以更改。为了帮助您,我在此提供以下基于默认 Apache2 配置的指南。

处理文件系统权限

为了访问这些文件,Apache 的用户www-data需要具有对文件的读取权限和对目录的读取执行权限,以及对整个路径的读取执行权限。如果您没有任何特殊要求,我建议您使用其他用户权限。

假设您想要通过 Web 服务器建立索引的目录已命名bar,并且位于用户的主目录中foo/home默认情况下,他/home/foo必须具有目录755权限。八进制数的最后一位表示所有其他用户对.755/home/foo

因此,让我们创建目录/home/foo/bar并确保它(及其路径)具有r-x其他用户的权限:

mkdir -p /home/foo/bar                            # create the directory
find /home/foo/bar -type d -exec chmod o+rx {} +  # apply o+rx for the dirs recursively
sudo chmod o+rx /home /home/foo                   # this step is optional
Run Code Online (Sandbox Code Playgroud)

现在让我们创建三个测试文件并确保它们具有其他用户的读取权限:

touch /home/foo/bar/file.{1..3}                  # create three empty test files
find /home/foo/bar -type f -exec chmod o+r {} +  # apply o+r for the files recursively
Run Code Online (Sandbox Code Playgroud)

为了允许www-data写入内容,/home/foo/bar您可以更改目录的组所有权并添加rwxs组权限(更多详细信息):

find /home/foo/bar -type d -exec chgrp www-data {} +
find /home/foo/bar -type d -exec chmod g+rwxs {} +
Run Code Online (Sandbox Code Playgroud)

通过创建另外三个空文件进行测试:

sudo -u www-data touch /home/foo/bar/file.{4..6}
Run Code Online (Sandbox Code Playgroud)


处理Apache的配置

默认情况下,在主配置文件中/etc/apache2/apache2.conf,出于安全原因,对根目录的访问/受到限制。我建议您不要通过虚拟主机配置覆盖这些规则并删除<Directory />标签(以及随附的指令)。

特别是,如果您要为 之外的目录创建别名DocumentRoot,则可能需要显式允许访问目标目录(源Apache 模块 mod_alias)。

让我们首先创建.htpasswd具有足够权限的文件(使用 2FA 添加更多安全性 - p.6):

htpasswd -c /home/foo/.htpasswd foo               # authentication for the username 'foo'
chmod 400 /home/foo/.htpasswd                     # restricted the permissions
sudo chown www-data:www-data /home/foo/.htpasswd  # change the ownership
Run Code Online (Sandbox Code Playgroud)

根据上面的内容,你的虚拟主机配置文件的相关部分应该是这样的:

<VirtualHost *:80>

    # Other configuration directives

    Alias "/bar" "/home/foo/bar"

    <Directory "/home/foo/bar">
            #Require all granted
            Options +Indexes +MultiViews +FollowSymLinks
            IndexOptions +FancyIndexing

            # Allow using of a .htaccess files
            AllowOverride All

            # This section could be moved in .htaccess file
            <IfModule mod_authz_core.c>

                <IfModule mod_authn_file.c>
                    AuthType Basic
                    AuthName "Type some hints here..."
                    AuthUserFile /home/foo/.htpasswd
                </IfModule>

                Require valid-user

            </IfModule>

    </Directory>


</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

启用相关模块并重新启动 Apache2 以应用新配置:

sudo a2enmod authz_core authz_user authn_file
sudo systemctl restart apache2.service
Run Code Online (Sandbox Code Playgroud)


结果示例

在此输入图像描述


更新:

我假设问题属于文件系统的权限问题。解决这个问题的最简单方法可能是使用此答案bindfs中所述的方法将目标目录挂载到 DocumentRoot 目录中。


工作解决方案:

这是最终的解决方案:放弃让 Alias 在外部安装的文件夹中正常工作的想法,而是采用 @pa4080 的解决方法建议,并将bindfs文件夹安装到 webroot 中的 /blah2。我未能成功让 /etc/fsab 正确初始化我的绑定,因此我决定为该任务编写一个初始化脚本。

首先,安装bindfs:

apt-get update
apt-get install bindfs
mkdir /var/www/example.com/blah2
Run Code Online (Sandbox Code Playgroud)

接下来我创建了一个/var/www/scripts/blahbind.sh在启动时运行的脚本文件:

#!/bin/bash
bindfs -o force-user=www-data,perms=a=rX /blah1/blah2 /var/www/example.com/blah2
Run Code Online (Sandbox Code Playgroud)

然后给它正确的权限:

chmod 750 /var/www/scripts/blahbind.sh
chmod +x /var/www/scripts/blahbind.sh
Run Code Online (Sandbox Code Playgroud)

接下来我创建了一个服务脚本:

vi /etc/systemd/system/blahbind.service 
Run Code Online (Sandbox Code Playgroud)

与内容:

[Unit]
Requires=mydrive.mount
After=mydrive.mount
Description=bind /blah1/blah2 to example.com/blah2 folder

[Service]
ExecStart=/var/www/scripts/blahbind.sh
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

注意,mydrive.mount应替换为/blah1/blah2文件夹的驱动器。获取坐骑列表systemctl list-units --type=mount

通过运行确认服务脚本是否有效

sudo service blahbind start
Run Code Online (Sandbox Code Playgroud)

然后启用该服务在重新启动后仍保留:

sudo systemctl enable blahbind.service
Run Code Online (Sandbox Code Playgroud)

然后,我的简化位置块,Alias没有example.com.conf

   <Location /blah2>
            Options +Indexes +MultiViews +FollowSymLinks
            IndexOptions +FancyIndexing
   </Location>
Run Code Online (Sandbox Code Playgroud)


Kri*_*ves 0

Apache 将需要所有目录、您的案例/blah1/blah2. 如果它遇到一个目录,它不具有该目录的组执行权限,则不会进行列表。

您可能需要修改/blah1为具有公共执行权限或 chownwww-data使其成为具有组执行权限的组,例如。g+x