尽管使用 PHP 具有适当的权限,但权限仍被拒绝
我正在尝试读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的目录需要
执行
权限才能正常工作。它似乎没有世界执行,并且由于 jenkins 可能不是 apache 用户,并且 apache 用户不在 adm 组中,因此它不起作用:例如:
即使
foo/bar
具有0777
权限,如果该目录没有执行权限,则读取其内容将被拒绝。您需要为目标目录和符号链接设置权限。
Your directory needs
execute
permission for this to work. It does not seem to have world execute, and sincejenkins
is probably not the apache user, and the apache user is not in theadm
group, it wouldn't work:Per example:
Even though
foo/bar
has0777
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.
您需要在层次结构中直到该文件的所有目录上设置执行位。
应该可以解决问题。
(注意:
ls -lhd /var/lib/jenkins
比ls -lh ...|grep jenkins
好一点)You need the execute bit set on all directories in the hierarchy up to that file.
should do the trick.
(Note:
ls -lhd /var/lib/jenkins
is a bit better thanls -lh ...|grep jenkins
)许多现代盒子(数字海洋、机架空间等)都附带了适用于 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
很可能您的 apache 用户不允许读取或访问 Web 文件
检查 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 启动
检查网络文件的路径所有权:
<块引用>
相应地调整权限:
在这一步中,我将留给您,您可以(a) 本例中的 make apache 用户为“john”。或者您可以(b) 将网络文件夹移动到家之外的地方。在不违反安全良好实践的情况下,可以将执行访问权限授予组甚至其他人。
一个。让 apache 用户为 john(仅适用于开发站点或者如果您知道自己在做什么
)将该文件夹移出家...它在那里做什么呢?
请记住更改站点以匹配此目录并重新启动 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
Check what user is apache running as:
Check the path ownership of your web files:
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)
b. Move that folder out of home... what is it doing there anyways?
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