在 Linux 安裝 Apache 及 PHP 後, 可以按需要設定個別 VirtualHost 或目錄停用 VirtualHost, 例如一個只用作儲存上載圖片的目錄, 不需要執行 PHP 的權限, 以下的設定方法。
VirtualHost 停用 PHP:
首先開啟儲存 VirtualHost 的檔案, RHEL 及 CentOS 是 /etc/httpd/conf/httpd.conf, 而 Debian 及 Ubuntu 則需要視乎主機名, 以下會以 myweb.com 作為例子:
RHEL / CentOS:
# vi /etc/httpd/conf/httpd.conf
Debian / Ubuntu:
# vi /etc/apache2/sites-abailable/myweb.com.conf
找到相關 VirtualHost 的段落, 加上 “php_admin_value engine off” 一行, 例如:
1 2 3 4 5 6 |
<VirtualHost *:80> ServerName myweb.com ServerAlias www.myweb.com DocumentRoot /var/www/myweb.com php_admin_value engine off </VirtualHost> |
儲存檔案後, 重新啟動 Apache:
# systemctl restart httpd
之後如果 myweb.com 有 PHP 檔案, 系統不會進行編譯, 會直接顯示源始檔。
目錄停用 PHP:
以下會停用 /var/www/myweb.com/images 目錄的 PHP 功能, 開啟 httpd.conf:
RHEL / CentOS:
# vi /etc/httpd/conf/httpd.conf
Debian / Ubuntu:
# vi /etc/apache2/httpd.conf
在檔案最後加上以下幾行:
1 2 3 |
<Directory /var/www/myweb.com/images php_admin_value engine off </Directory> |
儲存檔案後, 重新啟動 Apache:
# systemctl restart httpd