在 CentOS 系统中,使用 Apache2(通常称为 httpd)设置自定义错误页面非常简单。请按照以下步骤操作:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
/etc/httpd/conf/httpd.conf:sudo vim /etc/httpd/conf/httpd.conf
<Directory> 部分,它通常包含对网站文件和目录的访问权限设置。在此部分中,添加以下代码以启用自定义错误页面:ErrorDocument 404 /custom_404.html
ErrorDocument 500 /custom_500.html
这里,404 和 500 是 HTTP 状态码,分别表示“未找到”和“内部服务器错误”。/custom_404.html 和 /custom_500.html 是自定义错误页面的路径。请确保这些文件存在于您的网站根目录中。
保存并关闭配置文件。
创建自定义错误页面文件。在网站根目录(通常为 /var/www/html)中创建名为 custom_404.html 和 custom_500.html 的文件,并添加适当的 HTML 内容。例如:
sudo vim /var/www/html/custom_404.html
在打开的文件中添加以下内容:
<!DOCTYPE html>
<html>
<head>
<title>404 - 页面未找到</title>
</head>
<body>
<h1>抱歉,您访问的页面不存在。</h1>
<p><a href="/">返回首页</a></p>
</body>
</html>
同样地,为 custom_500.html 创建一个类似的文件。
保存并关闭文件。
重新加载 Apache2 服务以使更改生效:
sudo systemctl reload httpd
现在,当用户遇到 404 或 500 错误时,Apache2 将显示您设置的自定义错误页面。