HTTP 500 是「內部伺服器錯誤」,表示伺服器執行 PHP 程式碼時遇到了問題
Apache 日誌:
使用以下指令檢視錯誤日誌:
tail -f /var/log/apache2/error.log
檢查是否有 PHP 或伺服器相關的錯誤訊息(如語法錯誤、權限問題)。
PHP 日誌:
檢查 PHP 的錯誤日誌位置(預設可能在 /var/log/php8.1-fpm.log
或 /var/log/apache2/error.log
中)。
如未啟用 PHP 錯誤日誌,可在 /etc/php/8.1/apache2/php.ini
中開啟:
log_errors = On
error_log = /var/log/php_errors.log
重新啟動 Apache:
systemctl restart apache2
語法錯誤:檢查 .php
檔案是否有語法錯誤。
使用 PHP CLI 驗證:
php -l your_file.php
未處理的例外:程式可能有未捕獲的錯誤(如資料庫連線失敗、變數未定義等)。
測試基本檔案:
建立一個簡單的 test.php
進行測試:
<?php
phpinfo();
確認是否能正確顯示。
目錄權限:
確認 PHP 檔案所在目錄的權限正確:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
URL 重寫問題:
.htaccess
,檢查檔案中是否有錯誤指令。.htaccess
看是否正常:
暫時移除 .htaccess
:
mv /var/www/html/.htaccess /var/www/html/.htaccess.bak