-
php未安裝或未配置
首先,請確保您的服務器已經安裝了PHP,并已經正確配置了nginx以使其正常工作。要檢查PHP是否已正確安裝,請打開終端并運行以下命令:
php -v
這將顯示您服務器上當前安裝的PHP版本。如果沒有顯示PHP版本,請考慮安裝PHP。
要確保PHP與nginx一起使用,請編輯nginx配置文件并添加以下行:
立即學習“PHP免費學習筆記(深入)”;
location ~ .php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
在這里,我們要指定nginx的PHP文件處理位置和其他參數。請確認該代碼塊已添加至您的nginx配置文件中,并且sock文件的路徑與您的PHP配置文件相符。
-
index.php文件未設置
如果您的Web應用程序的主頁為index.php,但是它不會在nginx中自動處理,那么您需要在nginx配置文件的“index”指令中添加index.php,如下所示:
index index.php index.html;
現(xiàn)在,當您打開主頁時,nginx將自動查找index.php并正確處理它。
-
PHP文件權限
另一個導致nginx無法解析PHP文件的主要原因是權限不正確。確保以下內容:
-
PHP文件的權限為644
-
PHP文件所在目錄的權限為755
還需確保nginx用戶具有所有PHP文件的所有權,并且PHP文件所在目錄的所有權也設置為nginx組。這可以通過使用以下命令來實現(xiàn):
sudo chown -R nginx:nginx /var/www/html/
在這里,我們將/var/www/html/目錄的所有權分配給nginx用戶和組。
-
PHP模塊未啟用
如果您的nginx無法解析PHP文件且沒有顯示任何錯誤信息,請確保已經啟用了PHP模塊。要啟用它,請編輯nginx的編譯選項,添加以下行:
--with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_auth_request_module --with-http_image_filter_module --with-http_geoip_module --with-http_degradation_module --with-http_xslt_module --with-http_stub_status_module --with-http_spdy_module --with-http_auth_request_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-ipv6 --with-pcre --with-stream --with-stream_ssl_module --with-threads --with-debug --add-module=/path/to/php-src/sapi/nginx/
在這里,我們添加了–add-module=/path/to/php-src/sapi/nginx/來啟用PHP模塊。
-
PHP錯誤記錄
如果nginx無法解析PHP文件,但未顯示任何錯誤消息,則可以在PHP錯誤日志中查找有關錯誤的更多信息。打開php.ini文件,并取消注釋以下行,以啟用PHP錯誤記錄
Error_log = /var/log/php/error.log log_errors = On
我們指定/var/log/php/error.log作為PHP錯誤日志,并開啟錯誤記錄功能。請確保該文件夾已創(chuàng)建并具有適當的權限。