尽管使用 PHP 具有适当的权限,但权限仍被拒绝

发布于 2024-11-28 01:15:59 字数 1426 浏览 2 评论 0原文

我正在尝试读取 PHP 中的文件,但收到权限被拒绝错误,尽管每个人都具有该文件的读取权限。

PHP 代码:

$config=file_get_contents('/opt/jenkins/home/config.xml');

错误:

Warning: file_get_contents(/opt/jenkins/home/config.xml): failed to open stream: Permission denied in [...]

文件系统权限:

有一个符号链接指向 /opt/jenkins/home//var/lib/jenkins 并且每个人都拥有符号链接、实际文件夹和文件的读取权限。

$ ls -lh /opt/jenkins/
lrwxrwxrwx 1 sysadmin sysadmin   16 2011-08-04 08:12 home -> /var/lib/jenkins

$ ls -lh /var/lib/ | grep jenkins
drwxr-xr-- 6 jenkins adm     4.0K 2011-08-04 10:04 jenkins

$ ls -lh /var/lib/jenkins/config.xml
-rwxr-xr-- 1 jenkins adm 3.9K 2011-08-04 10:05 /var/lib/jenkins/config.xml

Apache 配置

配置为遵循符号链接(Options All)。为 /var/lib/jenkins/ 添加 Directory 指令没有什么区别。

<Directory /opt/jenkins/home/>
        Options All
        AllowOverride All
        Order Allow,Deny
        Allow from All
</Directory>

其他信息

我是否使用符号链接的路径(“/opt/jenkins/home/config.xml”)或真实路径(“/var/ lib/jenkins/config.xml")我也有同样的问题。

apache2 version=2.2.14-5ubuntu8.4
php version=5.3.2-1ubuntu4.9

知道为什么我会收到错误吗?

I'm trying to read a file in PHP and I'm getting a permission denied error although everybody has read access to the file.

The PHP code:

$config=file_get_contents('/opt/jenkins/home/config.xml');

The error:

Warning: file_get_contents(/opt/jenkins/home/config.xml): failed to open stream: Permission denied in [...]

The filesystem permission:

There is a symlink pointing /opt/jenkins/home/ to /var/lib/jenkins and everybody has read permission on the symlink, actual folder, and file.

$ ls -lh /opt/jenkins/
lrwxrwxrwx 1 sysadmin sysadmin   16 2011-08-04 08:12 home -> /var/lib/jenkins

$ ls -lh /var/lib/ | grep jenkins
drwxr-xr-- 6 jenkins adm     4.0K 2011-08-04 10:04 jenkins

$ ls -lh /var/lib/jenkins/config.xml
-rwxr-xr-- 1 jenkins adm 3.9K 2011-08-04 10:05 /var/lib/jenkins/config.xml

Apache configuration

Configured to folllow symlinks (Options All). Adding a Directory directive for /var/lib/jenkins/ makes no difference.

<Directory /opt/jenkins/home/>
        Options All
        AllowOverride All
        Order Allow,Deny
        Allow from All
</Directory>

Additional info

Whether I use the path through the symlink ("/opt/jenkins/home/config.xml") or the real path ("/var/lib/jenkins/config.xml") I have the same problem.

apache2 version=2.2.14-5ubuntu8.4
php version=5.3.2-1ubuntu4.9

Any idea as to why I'm getting the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

杯别 2024-12-05 01:15:59

您的目录需要执行权限才能正常工作。它似乎没有世界执行,并且由于 jenkins 可能不是 apache 用户,并且 apache 用户不在 adm 组中,因此它不起作用:

$ ls -lh /var/lib/ | grep jenkins
drwxr-xr-- 6 jenkins adm     4.0K 2011-08-04 10:04 jenkins

例如:

netcoder@netcoder:~$ mkdir foo
netcoder@netcoder:~$ echo hello > foo/bar
netcoder@netcoder:~$ chmod 777 foo/bar
netcoder@netcoder:~$ ls -lsah foo/bar 
4.0K -rwxrwxrwx 1 netcoder netcoder 6 2011-08-04 08:22 foo/bar
netcoder@netcoder:~$ chmod 444 foo/
netcoder@netcoder:~$ ls -lsah | grep foo
4.0K dr--r--r--  2 netcoder netcoder 4.0K 2011-08-04 08:22 foo
netcoder@netcoder:~$ cat foo/bar 
cat: foo/bar: Permission denied

即使 foo/bar 具有 0777 权限,如果该目录没有执行权限,则读取其内容将被拒绝。

您需要为目标目录和符号链接设置权限。

Your directory needs execute permission for this to work. It does not seem to have world execute, and since jenkins is probably not the apache user, and the apache user is not in the adm group, it wouldn't work:

$ ls -lh /var/lib/ | grep jenkins
drwxr-xr-- 6 jenkins adm     4.0K 2011-08-04 10:04 jenkins

Per example:

netcoder@netcoder:~$ mkdir foo
netcoder@netcoder:~$ echo hello > foo/bar
netcoder@netcoder:~$ chmod 777 foo/bar
netcoder@netcoder:~$ ls -lsah foo/bar 
4.0K -rwxrwxrwx 1 netcoder netcoder 6 2011-08-04 08:22 foo/bar
netcoder@netcoder:~$ chmod 444 foo/
netcoder@netcoder:~$ ls -lsah | grep foo
4.0K dr--r--r--  2 netcoder netcoder 4.0K 2011-08-04 08:22 foo
netcoder@netcoder:~$ cat foo/bar 
cat: foo/bar: Permission denied

Even though foo/bar has 0777 permission, if the directory does not have the execute permission, reading its contents is denied.

You'll need the permission to be set for both the target directory and the symbolic link.

苦行僧 2024-12-05 01:15:59

您需要在层次结构中直到该文件的所有目录上设置执行位。

chmod o+x /var/lib/jenkins

应该可以解决问题。

(注意:ls -lhd /var/lib/jenkinsls -lh ...|grep jenkins 好一点)

You need the execute bit set on all directories in the hierarchy up to that file.

chmod o+x /var/lib/jenkins

should do the trick.

(Note: ls -lhd /var/lib/jenkins is a bit better than ls -lh ...|grep jenkins)

傾旎 2024-12-05 01:15:59

许多现代盒子(数字海洋、机架空间等)都附带了适用于 RedHat 兼容操作系统(如 CentOS)的 SELinux(安全增强型 Linux)。这给你的工作带来了另一个麻烦,你需要记住这一点。您可以完美设置您的权限,但它仍然会显示权限被拒绝。您需要为 SELinux 定义一个可写上下文:

sudo chcon -t httpd_sys_rw_content_t /data/www/html/sites/mysite -R

Lots of modern boxes (digital ocean, rackspace etc) ship with SELinux (Security Enhanced Linux) for RedHat compatible OSs (like CentOS). This throws another wrench into the works which you need to keep in mind. You can have your permissions perfectly set and it will still say permission denied. You need to define a writable context for SELinux:

sudo chcon -t httpd_sys_rw_content_t /data/www/html/sites/mysite -R

独留℉清风醉 2024-12-05 01:15:59

很可能您的 apache 用户不允许读取或访问 Web 文件

  1. 检查 apache 运行的用户身份:

    <块引用>
    <前><代码>$ ps aux | grep [a]pache
    根 40283 0.0 0.2 472548 21116 ? SS 14:38 0:00 /usr/sbin/apache2 -k 启动
    www-数据 40287 0.0 0.1 472760 8800 ? S 14:38 0:00 /usr/sbin/apache2 -k 启动
    www-数据 40288 0.0 0.1 472760 8540 ? S 14:38 0:00 /usr/sbin/apache2 -k 启动
    www-数据 40289 0.0 0.1 472776 8540 ? S 14:38 0:00 /usr/sbin/apache2 -k 启动

  2. 检查网络文件的路径所有权:

    <块引用>

    $ namei -mol /home/john/app2/
    f:/home/john/app2/
    drwxr-xr-x根根/
    drwxr-xr-x root 根目录
    drwx------ john john john # <== 啊哈,apache 用户无法访问!
    drwxr-xr-x 约翰 约翰 约翰 app2
    
  3. 相应地调整权限:

在这一步中,我将留给您,您可以(a) 本例中的 make apache 用户为“john”。或者您可以(b) 将网络文件夹移动到家之外的地方。在不违反安全良好实践的情况下,可以将执行访问权限授予组甚至其他人。

一个。让 apache 用户为 john(仅适用于开发站点或者如果您知道自己在做什么

sudo vi /etc/apache2/envars 
# replace 
export APACHE_RUN_USER=www-data 
export APACHE_RUN_GROUP=www-data
# with
export APACHE_RUN_USER=john 
export APACHE_RUN_GROUP=john

)将该文件夹移出家...它在那里做什么呢?

sudo mv /home/john/app2 /var/www/

请记住更改站点以匹配此目录并重新启动 apache 服务器。

以下是一些参考:

https://wiki.apache.org/httpd/13PermissionDenied

http://wiki.apache.org/httpd/FileSystemPermissions

Most likely your apache user is not allowed to read or access the web files

  1. Check what user is apache running as:

    $ ps aux | grep [a]pache
    root     40283  0.0  0.2 472548 21116 ?        Ss   14:38   0:00 /usr/sbin/apache2 -k start
    www-data 40287  0.0  0.1 472760  8800 ?        S    14:38   0:00 /usr/sbin/apache2 -k start
    www-data 40288  0.0  0.1 472760  8540 ?        S    14:38   0:00 /usr/sbin/apache2 -k start
    www-data 40289  0.0  0.1 472776  8540 ?        S    14:38   0:00 /usr/sbin/apache2 -k start
    
  2. Check the path ownership of your web files:

    $ namei -mol /home/john/app2/
    f: /home/john/app2/
    drwxr-xr-x root root  /
    drwxr-xr-x root root  home
    drwx------ john john  john         # <== Ahaa, no access for apache user!
    drwxr-xr-x john john  john app2
    
  3. Adjust permissions accordingly:

Well in this step I will leave it up to you, you can either (a) the make apache user 'john' in this example. Or you could (b) move the web folder to a place outside home. Where the execute access can be given to the group or to even others without breaking security good practices.

a. Make apache user john (ONLY FOR DEV SITES or if you know what you are doing)

sudo vi /etc/apache2/envars 
# replace 
export APACHE_RUN_USER=www-data 
export APACHE_RUN_GROUP=www-data
# with
export APACHE_RUN_USER=john 
export APACHE_RUN_GROUP=john

b. Move that folder out of home... what is it doing there anyways?

sudo mv /home/john/app2 /var/www/

Remember to change the site to match this directory and to restart the apache server.

Here are some references:

https://wiki.apache.org/httpd/13PermissionDenied

http://wiki.apache.org/httpd/FileSystemPermissions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文