要自定義 Ubuntu 上的 PHP-FPM 錯誤頁面,請按照以下步驟操作:
首先,創建一個自定義的錯誤頁面文件。你可以使用任何文本編輯器(如 nano、vim 或 gedit)創建一個新的 HTML 文件。例如,創建一個名為 custom_error_page.html 的文件,并添加以下內容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定義錯誤頁面</title>
</head>
<body>
<h1>抱歉,發生錯誤!</h1>
<p>我們會盡快解決這個問題。</p>
</body>
</html>
將此文件保存到你的 web 服務器文檔根目錄(例如 /var/www/html)或其他適當的目錄。
接下來,你需要修改 PHP-FPM 的配置文件,以便在發生錯誤時使用自定義錯誤頁面。打開 PHP-FPM 配置文件,通常位于 /etc/php/7.x/fpm/pool.d/www.conf(其中 7.x 是你的 PHP 版本)。
在文件中找到以下行:
catch_workers_output = yes
確保此行已取消注釋(沒有分號)并設置為 yes。這將允許 PHP-FPM 捕獲工作進程的輸出并將其重定向到主進程。
根據你使用的 web 服務器(Nginx 或 Apache),你需要修改相應的配置文件以使用自定義錯誤頁面。
對于 Nginx,在你的站點配置文件中(通常位于 /etc/nginx/sites-available/your_domain.conf),添加或修改以下內容:
error_page 500 502 503 504 /custom_error_page.html;
location = /custom_error_page.html {
root /var/www/html;
}
將 /var/www/html 替換為你的實際文檔根目錄。
對于 Apache,在你的站點配置文件中(通常位于 /etc/apache2/sites-available/your_domain.conf),添加或修改以下內容:
ErrorDocument 500 /custom_error_page.html
ErrorDocument 502 /custom_error_page.html
ErrorDocument 503 /custom_error_page.html
ErrorDocument 504 /custom_error_page.html
Alias /custom_error_page.html /var/www/html/custom_error_page.html
<Directory /var/www/html>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
將 /var/www/html 替換為你的實際文檔根目錄。
保存所有更改后,重啟 PHP-FPM 和 web 服務器(Nginx 或 Apache)以使更改生效。
對于 Nginx:
sudo systemctl restart nginx
對于 Apache:
sudo systemctl restart apache2
對于 PHP-FPM:
sudo systemctl restart php7.x-fpm
將 7.x 替換為你的實際 PHP 版本。
現在,當發生錯誤時,你的自定義錯誤頁面應該會顯示。請注意,這些步驟可能因你的具體配置和需求而略有不同。