根据ls 的结果,Apache 无法访问您的文件。
简答:
运行以下两条命令:
find /var/www/html -type f -exec chmod 644 {} \;
find /var/www/html -type d -exec chmod 755 {} \;
更长的解释:
pi@raspberrypi:~ $ ls -l /var/www/html
total 2976
-rw------- 1 pi pi 60146 Jul 23 22:11 bnw.jpg
-rw------- 1 pi pi 202851 Jul 23 22:11 color.jpg
-rw------- 1 pi pi 617185 Jul 23 21:27 dark.jpg
-rw------- 1 pi pi 2028727 Jul 23 22:11 effect.jpg
drwx------ 2 pi pi 4096 Jul 23 21:33 images
-rw------- 1 pi pi 2238 Jul 23 22:11 index.css
-rw-r--r-- 1 pi root 1261 Jul 23 22:11 index.html
-rw------- 1 pi pi 108397 Jul 23 22:11 photo.jpg
-rw------- 1 pi pi 538 Jul 23 22:11 script.js
-rw------- 1 pi pi 2238 Jul 23 21:33 style.css
行首的符号含义如下:
d: indicates a directory
r: means the file/directory is readable
w: means the file/directory is writable
x: means the file/directory is executable
-: means none of the above apply
这些符号按如下方式分组:
drwxrwxrwx
^^^ apply to all users on the system
^^^--- apply to all users in the group that owns the file
^^^------ apply to the user that owns the file
其中pi pi 指的是文件的所有者:pi 组中的用户pi。您的 index.html 文件是一个例外,因为它归组 root 所有。
目前这并不重要,除了您需要意识到 Apache 通常在组 www-data 中以用户 www-data 的身份运行。这因系统而异,但这是最常见的。这意味着,要让 Apache 能够访问文件,它必须可供 www-data 用户或 www-data 组(或两者)使用。
在您的情况下,文件归用户 pi 和组 pi 所有(index.html 除外,它归组 root 拥有。因为 Apache 不是那个用户,而是很可能不在这两个组中,这意味着必须正确设置“系统上的所有用户”的文件权限,Apache 才能访问该文件。
如您所见,index.html 设置为对系统上的所有用户可读:
-rw-r--r-- 1 pi root 1261 Jul 23 22:11 index.html
^^^ all users on the system may read from this file, but may not write and may not execute the file
^^^--- all users in the group "root" may read from this file, but may not write and may not execute the file
^^^------ the user "pi" may read from and write to this file, but may not execute the file
所有其他文件,包括images 目录,只能由“pi”用户访问:
-rw------- 1 pi pi 2028727 Jul 23 22:11 effect.jpg
^^^ all users on the system may not read from, write to or execute this file
^^^--- all users in the group "pi" may not read from, write to or execute this file
^^^------ the user "pi" may read from and write to this file, but may not execute it
因此,要使 Apache 可以使用 effect.jpg,您需要将文件权限更改为:
-rw----r-- 1 pi pi 2028727 Jul 23 22:11 effect.jpg
^^^ all users on the system (including Apache) may read from this file
为此,您可以使用chmod 命令。有两种方法可以翻转该权限:
chmod o+r effect.jpg
chmod 604 effect.jpg
chmod o+r 表示“将'r'权限添加到'其他用户'类别”(您可以使用u+r 或g+r 更改用户或组权限)。
chmod 604 表示“将用户权限设置为 6,组设置为 0,其他设置为 4”——其中数字是权限的二进制和:1(可执行)、2(可写)、4(可读)。
目录需要更多工作:
drwx------ 2 pi pi 4096 Jul 23 21:33 images
为了允许文件系统实际打开一个目录并读取其中的文件,它需要对试图访问其内容的用户是可执行的。因此,要允许 Apache 读取此文件夹中的任何文件,它需要具有以下权限:
drwx---r-x 2 pi pi 4096 Jul 23 21:33 images
^^^ all users on the system may read from this directory and execute it
为此,使用相同的原则:
chmod o+rx images(其他用户,添加可读可执行权限)
chmod 705 images(为所有者设置 read(4)+write(2)+execute(1),为所有其他用户设置 read(4)+execute(1))
现在,虽然所有者和组相同不是绝对必要的,但最佳做法是确保该组具有与“所有其他用户”类别相同的权限。因此,与其给文件604 (-rw----r--) 和文件夹705 (drwx---r-x),不如给它们644 (-rw-r--r--) 和755 (drwxr-xr-x)。如果您在多个开发人员需要能够修改文件的环境中工作,他们应该在同一个用户组中,最佳做法是赋予该组与所有者相同的权限,所以644 (@987654366 @) 和775 (drwxrwxr-x)。
最后,您不想手动更改所有文件权限。这个特定的项目似乎相对较小,但它仍然是令人讨厌的工作。幸运的是,我们可以使用find 命令进行批量更新。
find 将列出给定文件夹的全部内容,包括子文件夹,然后您可以对其进行过滤或执行操作。
find /var/www/html -type f
这将列出/var/www/html 或任何常规文件的子文件夹中的所有条目。
find /var/www/html -type d
这将列出/var/www/html 或任何属于目录的子文件夹中的所有条目。
我们可以使用-exec 告诉find 自动对它找到的每个文件/文件夹执行某个命令:
find /var/www/html -type f -exec chmod 644 {} \;
{} 是一个占位符,find 将放置每个文件名。需要\; 来通知find 不会为-exec 命令提供更多参数,因此我们可以选择为find 本身添加其他参数。
所以上面的命令会修复所有文件的权限,下面的命令会修复所有文件夹的权限:
find /var/www/html -type d -exec chmod 755 {} \;
之后,Apache 应该可以访问/var/www/html 中的所有文件和文件夹。请记住,每次在该文件夹中创建新文件时,都需要检查权限并在必要时进行修复。