要自定义 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 版本。
现在,当发生错误时,你的自定义错误页面应该会显示。请注意,这些步骤可能因你的具体配置和需求而略有不同。