简体   繁体   English

如何使apache2用户访问/ root目录中的文件?

[英]How to make apache2 user to access the file in /root directory?

I am using Linux 12.04,apache and php is installed on it.I want to access a text file in /root/ folder.I am really confused with the permissions.The php script im using 我正在使用Linux 12.04,在其上安装了Apache和php。我想访问/ root /文件夹中的文本文件。我真的对权限感到困惑。

<?php
$file = fopen("/root/cr.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached

while(!feof($file))
  {
  echo fgets($file). "<br>";
  }

fclose($file);
?>

This script is able to access the file /var/www folder but not able to access /root/ip.txt file. 该脚本能够访问文件/ var / www文件夹,但不能访问/root/ip.txt文件。 Please help and explain step to step possible. 请帮助并逐步解释。

I will forget about the security implications of doing this and will go down to business: 我会忘记这样做的安全隐患,然后开始做事:

If you have done ls -l /var/www and ls -l /root you would have noticed that both has different permissions: 如果您执行过ls -l / var / www和ls -l / root,则您会注意到两者具有不同的权限:

$ ls -l /root/cr.txt total 2 -rw-r----- 1 root root 0 Jul 9 01:28 cr.txt $ ls -l /var/www total 2 -rw-r--r-- 1 www-data www-data 0 Jul 9 01:28 somefile $ ls -l /root/cr.txt总计2 -rw-r ----- 1根root 0 Jul 9 01:28 cr.txt $ ls -l / var / www总计2 -rw-r--r -1 www-data www-data 0 Jul 9 01:28 somefile

The /root is only readable for root, while /var/www is readable by www-data user. / root仅对root可读,而/ var / www对www-data用户可读。 Now, if you check apache process you will notice that it's running using the www-data user. 现在,如果您检查apache进程,您会注意到它正在使用www-data用户运行。

$ ps aux | $ ps aux | grep apache www-data 5133 0.0 0.2 6512 1208 ? grep apache www-data 5133 0.0 0.2 6512 1208? R+ 10:04 0:00 apache R + 10:04 0:00阿帕奇

Now, you are trying to make apache running with the www-data user read the file. 现在,您正在尝试使www-data用户运行的apache读取该文件。 You can take three courses of action: 您可以采取三种行动方式:

1.Move the file to /var/www and change it's permissions so www-data users can read it. 1.将文件移至/ var / www并更改其权限,以便www-data用户可以读取它。

mv /root/cr.txt /var/www/
chown www-data:www-data /var/www/cr.txt

2.This is the preferable method. 2.这是首选方法。

Create a symlink to the file in the /var/www directory:

ln /root/cr.txt /var/www/

3.This won't ensure that your file is being read, in some cases. 3.在某些情况下,这不能确保文件被读取。

This is dangerous and should not be done! 这很危险,不应该这样做! Add the www-data user to the root group, or change the file ownership so it could be read by www-data users: 将www-data用户添加到根组,或更改文件所有权,以便www-data用户可以读取它:

chown :www-data /root/cr.txt
## Or
sudo adduser www-data root

This shouldn't be done if you don't understand the risks! 如果您不了解风险,则不应这样做!

First, it is generally a bad idea to give apache access to root. 首先,让apache访问root通常是一个坏主意。 If you insist ... 如果你坚持 ...

Install ACL (Access Control List) Installing ACL 安装ACL(访问控制列表) 安装ACL

Then, assuming your apache server runs with 'apache2' for it's user and group, give the apache2 user and group access to directories/files: 然后,假设您的apache服务器的用户和组以“ apache2”运行,请授予apache2用户和组对目录/文件的访问权限:

setfacl -m "group:apache2:r-x" /root/whatever.file
setfacl -m "user:apache2:r-x" /root/whatever.file

# *** only need the next two lines if you plan on writing new files in the specified  directory.  It sets the default file permissions that will be used when the new file is created.
setfacl -d -m "group:apache2:r-x" /root
setfacl -d -m "user:apache2:r-x" /root

Change the rx permissions to whatever you need 将rx权限更改为所需的权限

Edit - potential solution without ACL 编辑-没有ACL的潜在解决方案

The following is untested and may require tweaking but should get you going in the right direction. 以下内容未经测试,可能需要进行调整,但可以使您朝正确的方向前进。 USE AT YOUR OWN RISK! 自行承担使用风险!

Create a new group. 创建一个新组。 The name can be anything but for this example I will call it 'wwwadmins' using groupadd 该名称可以是任何名称,但在本示例中,我将使用groupadd将其命名为“ wwwadmins”

 groupadd wwwadmins

Add the root and apache2 users to this new group using usermod 使用usermod将root和apache2用户添加到该新组中

 usermod -a -G wwwadmins root
 usermod -a -G wwwadmins apache2

Change the group owner on the file to the new group using chown 使用chown将文件上的组所有者更改为新组

 chown :wwwadmins /root/whatever.file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM