在 CentOS 上自定义 Apache2 错误页面,您需要按照以下步骤操作:
- 确保已经安装了 Apache2 服务。如果尚未安装,请使用以下命令安装:
sudo yum install httpd
- 创建自定义错误页面文件。首先,在您的网站根目录(例如
/var/www/html
)中创建一个名为.htaccess
的文件。然后,使用文本编辑器打开该文件并添加以下内容:
ErrorDocument 404 /custom-404.html ErrorDocument 500 /custom-500.html
这里,/custom-404.html
和 /custom-500.html
是您要显示的自定义错误页面的路径。确保这些文件存在于您的网站根目录中。
- 创建自定义错误页面文件。使用文本编辑器创建两个 HTML 文件,分别命名为
custom-404.html
和custom-500.html
。在这些文件中编写您的自定义错误页面内容。例如:
custom-404.html
:
404 - 页面未找到 抱歉,您访问的页面不存在!
custom-500.html
:
500 - 服务器内部错误 抱歉,服务器遇到了错误!
- 重启 Apache2 服务以应用更改。使用以下命令重启 Apache2 服务:
sudo systemctl restart httpd
现在,当用户访问不存在的页面或服务器遇到内部错误时,Apache2 将显示您的自定义错误页面。