2013-08-06 46 views
1

我使用Linux 12.04,Apache和PHP安裝它。我在想訪問/根的文本文件/ folder.I是使用如何讓apache2用戶訪問/ 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); 
?> 

這個permissions.The PHP腳本我真的糊塗腳本能夠訪問文件/ var/www文件夾,但無法訪問/root/ip.txt文件。 請幫忙解釋一下步驟。

+0

文件權限。 '/ root /'只能由服務器的root用戶訪問。 – ciruvan

+0

嘗試更改file.txt的組作爲apache「chown root:apache file.txt」,然後爲組提供讀寫權限。 「chmod g + rw file.txt」。現在再試一次。讓我知道這是否有助於你。 –

回答

1

我會忘記這樣做的安全影響,並會下降到企業內斯:

如果你已經做了ls -l命令的/ var/www和ls -l命令/根,你會注意到,這兩個具有不同的權限:

$ ls -l命令/root/cr.txt共有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

/root只能讀取根目錄,而/ var/www可以被www-data用戶讀取。現在,如果您檢查apache進程,您會注意到它正在使用www-data用戶運行。

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

現在,您正在嘗試使apache運行時使用www-data用戶讀取文件。你可以採取三種行動方式:

1.將文件移動到/ var/www並更改它的權限,以便www-data用戶可以讀取它。

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

2.這是最好的方法。

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

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

3.在某些情況下,這不會確保您的文件正在被讀取。

這很危險,不應該這樣做!在www數據用戶添加到root組,或更改文件的所有權,以便它可以通過www數據的用戶閱讀:

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

這不應該做的,如果你不理解的風險!

0

首先,讓apache訪問根目錄通常是一個壞主意。如果你堅持......

安裝ACL(訪問控制列表)Installing ACL

然後,假設你的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 

更改RX權限,任何你需要

編輯 - 無需ACL

潛在的解決方案

以下是未經測試的,可能需要調整,但應該讓你朝着正確的方向前進。自擔風險使用!

創建一個新組。名稱可以是任何東西,但在這個例子中,我將使用groupadd

groupadd wwwadmins 

稱之爲「wwwadmins」使用usermod

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

更改羣主根和Apache2的用戶加入到這個新組的文件到新組使用chown

chown :wwwadmins /root/whatever.file 
+0

沒有任何其他方式沒有安裝ACL? – acekapila

+0

也許但我一直使用ACL。我編輯了我的答案以包含一個潛在的解決方案 – Drew

相關問題