首頁 > 運維 > Nginx > 主體

nginx不解析php檔案如何解決

PHPz
發布: 2023-05-21 10:46:46
轉載
1631 人瀏覽過

  1. PHP未安裝或未配置

#首先,請確保您的伺服器已經安裝了PHP,並且已經正確配置了nginx以使其正常工作。若要檢查PHP是否已正確安裝,請開啟終端並執行下列命令:

php -v
登入後複製

這將顯示您伺服器上目前安裝的PHP版本。如果沒有顯示PHP版本,請考慮安裝PHP。

要確保PHP與nginx一起使用,請編輯nginx設定檔並新增以下行:

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設定檔相符。

  1. index.php檔案未設定

如果您的網路應用程式的主頁為index.php,但是它不會在nginx中自動處理,那麼您需要在nginx設定檔的「index」指令中加入index.php,如下所示:

index index.php index.html;
登入後複製

現在,當您開啟主頁時,nginx將自動尋找index.php並正確處理它。

  1. PHP檔案權限

另一個導致nginx無法解析PHP檔案的主要原因是權限不正確。確保以下內容:

  • PHP檔案的權限為644

  • #PHP檔案所在目錄的權限為755

#### ####還需確保nginx使用者俱有所有PHP檔案的所有權,且PHP檔案所在目錄的所有權也設定為nginx群組。這可以透過使用以下命令來實現:###
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錯誤日誌,並開啟錯誤記錄功能。請確保該資料夾已建立並具有適當的權限。 ###

以上是nginx不解析php檔案如何解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!