Apache-2.2

Apache 在預設情況下不解析 VHOST 上的 PHP

  • March 22, 2013

我設置了一個 Debian 6 VPS 來託管幾個小站點,它在預設站點上執行良好。

接下來,我創建了新使用者並複制/粘貼預設 vhost 文件並使用a2ensite啟用它,並且只為該新使用者相應地調整了路徑,但由於某種原因,PHP 文件沒有被解析,只顯示它們的原始碼。

所以,回顧一下:相同的虛擬主機設置,啟用 PHP 模組,啟用短標籤(但未使用)

說清楚:我在這里和其他地方的網路上搜尋並找到了一堆“解決方案”,但這些似乎都不適合我。

剛剛在日誌中註意到這個字元串:

PHP Fatal error:  Unknown: Failed opening required '/home/tester/public_html/index.php' (include_path='.:/usr/share/php:/usr/share/pear') in Unknown on line 0

[Thu Mar 21 20:28:55 2013] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  Unknown: open_basedir restriction in effect. File(/home/tester/public_html/index.php) is not within the allowed path(s): (/home/user/public_html:/tmp) in Unknown on line 0

這是我的預設虛擬主機文件內容:

<VirtualHost *:80>

       ServerAdmin user@mail.com
       ServerName domain.com
       ServerAlias www.domain.com
       DocumentRoot /home/user/public_html/
       ErrorLog /home/user/logs/error.log
       CustomLog /home/user/logs/access.log combined

       <Directory "/home/user/public_html">
       AllowOverride All
               php_admin_flag engine on
               php_admin_value open_basedir "/home/user/public_html:/tmp"
       </Directory>

</VirtualHost>

PHP 的open_basedir會不會是這個問題的罪魁禍首?

耶!得到它的工作!

在 /etc/apache2/mods-enabled/php5.conf 中檢查了 Apache 的 PHP 模組 conf 設置:

<FilesMatch ".+\.ph(p[345]?|t|tml)$">
   SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
   SetHandler application/x-httpd-php-source
   # Deny access to raw php sources by default
   # To re-enable it's recommended to enable access to the files
   # only in specific virtual host or directory
   Order Deny,Allow
   Deny from all
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[345]?|t|tml|ps)$">
   Order Deny,Allow
   Deny from all
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
   <Directory /home/*/public_html>
       php_admin_value engine Off
   </Directory>
</IfModule>

它清楚地說:

# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.)

所以我做了,然後重新啟動了 Apache,瞧:

http://i.stack.imgur.com/Be7dj.png

是的,open_basedir 可能是這個問題的根源。您必須在路徑中有 /home/tester/public_html/ 並且您有 /home/user。

看起來它跳過了 PHP 解析並且沒有阻止對頁面的訪問,因為我很難 - 我發現類似的問題在這裡解決了:https ://bbs.archlinux.org/viewtopic.php?id=57877 。

/要求澄清的第一個版本被刪除,因為我被告知不符合 ServerFault 政策。/

引用自:https://serverfault.com/questions/490255