在 CentOS 上配置 Apache 服務器時,遇到 404 錯誤通常是由于以下幾個原因之一:
文檔根目錄配置錯誤:
/etc/httpd/conf/httpd.conf
或 /etc/apache2/apache2.conf
文件中的 DocumentRoot
指令。權限問題:
chmod
和 chown
命令來設置正確的權限和所有權。虛擬主機配置錯誤:
DocumentRoot
。/etc/httpd/conf.d/
或 /etc/apache2/sites-available/
目錄下的虛擬主機配置文件。SELinux 配置:
getenforce
命令檢查 SELinux 的狀態,如果啟用,可以嘗試臨時禁用它來測試是否是 SELinux 導致的問題。setenforce 0
臨時禁用 SELinux,或者配置 SELinux 策略以允許 Apache 訪問所需的文件和目錄。防火墻配置:
firewall-cmd
命令來檢查和配置防火墻規則。重啟 Apache 服務:
systemctl restart httpd
或 systemctl restart apache2
命令來重啟 Apache。以下是一個基本的檢查步驟示例:
檢查文檔根目錄:
grep -r "DocumentRoot" /etc/httpd/conf/httpd.conf
檢查權限:
ls -ld /var/www/html
chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html
檢查 SELinux 狀態:
getenforce
setenforce 0 # 臨時禁用 SELinux
檢查防火墻規則:
firewall-cmd --list-all
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
重啟 Apache 服務:
systemctl restart httpd
通過以上步驟,你應該能夠找到并解決導致 404 錯誤的原因。如果問題仍然存在,請檢查 Apache 的錯誤日志文件(通常位于 /var/log/httpd/error_log
或 /var/log/apache2/error.log
)以獲取更多詳細信息。