要自定义 CentOS 上 Apache 的错误页面,请按照以下步骤操作:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
/etc/httpd/conf/httpd.conf
:sudo vim /etc/httpd/conf/httpd.conf
ErrorDocument
指令。如果没有找到,请在文件的末尾添加以下内容:ErrorDocument 404 /custom_404.html
ErrorDocument 500 /custom_500.html
这里,我们将自定义 404 和 500 错误页面。你可以根据需要更改错误代码和自定义页面的路径。
/var/www/html
目录下创建 custom_404.html
和 custom_500.html
文件。使用文本编辑器创建这些文件:sudo vim /var/www/html/custom_404.html
sudo vim /var/www/html/custom_500.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 -->
<!DOCTYPE html>
<html>
<head>
<title>自定义 500 页面</title>
</head>
<body>
<h1>抱歉,服务器内部错误。</h1>
<p>请稍后再试,或联系网站管理员。</p>
</body>
</html>
保存并关闭文件。
重新启动 Apache 服务以使更改生效:
sudo systemctl restart httpd
现在,当用户访问不存在的页面时,将显示自定义的 404 页面;当服务器遇到内部错误时,将显示自定义的 500 页面。你可以根据需要为其他错误代码创建自定义页面,并在 ErrorDocument
指令中指定它们。