gpt4 book ai didi

html - Apache不允许加载除index.html之外的其他文件

转载 作者:可可西里 更新时间:2023-11-01 17:27:01 25 4
gpt4 key购买 nike

在你问之前,我试着寻找类似的问题,没有一个是我的问题。
我是新手,我在运行raspbian的raspi上安装了apache2、php5和mysql5。
根据需要,我将index.html放在/var/www/html中,当我单独键入pi的ip或后跟/index.html时,index.html会正确运行其代码,并且所有元素都会出现。
问题:此外,我把我的js和css文件放在同一个文件夹中,还有一些照片要显示。
浏览器完全读取html,但不加载由此链接的js和css文件,也不加载图像。把文件放在子文件夹中也没有帮助。
当我输入ip,然后输入/style.css(链接css文件的名称)时,我得到一个403禁止。错误日志还显示:

 file permissions deny server access: /var/www/html/photo.jpg, referer: http://192.168.178.120/
access to /images/dark.jpg denied because search permissions are missing on a component of the path, referer: http://192.168.178.120/

我试着研究这个,但没有任何帮助。当我的js和css在我的html中时,除了图像之外,一切都很好
结论:apache不允许浏览器加载 index.html以外的文件。
你知道问题是什么吗?有没有一个特定的文件夹可以放我的其他文件?提前谢谢。

最佳答案

根据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的身份运行。这在不同的系统中有所不同,但这是最常见的。这意味着为了让apache能够访问一个文件,它必须对 www-data用户或 www-data组(或两者)可用。
在您的案例中,这些文件归用户 www-data和组 pi所有(index.html除外,它归组 pi所有)。由于apache不是这个用户,而且很可能不在这两个组中的任何一个组中,这意味着必须正确设置“系统上所有用户”的文件权限,以便apache能够访问该文件。
如您所见, root设置为对系统上的所有用户都可读:
-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

所有其他文件,包括 index.html目录,只能由“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

因此,要使 images对apache可用,您需要将文件权限更改为:
-rw----r-- 1 pi pi   2028727 Jul 23 22:11 effect.jpg
^^^ all users on the system (including Apache) may read from this file

为此,您可以使用 effect.jpg命令。有两种方法可以取消该权限:
chmod
chmod o+r effect.jpg
chmod 604 effect.jpg表示“将'r'权限添加到'other users'类别”(您可以使用 chmod o+ru+r更改用户或组权限)。
g+r表示“将用户的权限设置为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 604(其他用户,添加可读和可执行权限)
chmod o+rx images(设置读取(4)+写入(2)+所有者执行(1)和读取(4)+所有其他用户执行(1)
现在,虽然严格来说,如果所有者和组是相同的,则没有必要这样做,但最好确保组具有与“所有其他用户”类别相同的权限。因此,与其提供文件 chmod 705 images604)和文件夹 -rw----r--705),不如提供它们 drwx---r-x644)和 -rw-r--r--755)。如果您工作的环境中需要多个开发人员能够修改文件,那么他们应该在同一个用户组中,最佳做法是授予该组与所有者相同的权限,因此 drwxr-xr-x644)和 -rw-rw-r--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
之后,apache应该可以访问 find /var/www/html -type d -exec chmod 755 {} \;中的所有文件和文件夹。请记住,每次在该文件夹中创建新文件时,都需要检查权限并在必要时修复它。

关于html - Apache不允许加载除index.html之外的其他文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45269674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com