Ubuntu 下 Apache2 报错的原因非常多,不能一概而论。下面我按常见报错类型 + 原因 + 排查方法给你一个系统性的说明,你可以对照自己的情况。
报错示例
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
原因
apache2 和 httpd排查
sudo netstat -tulnp | grep :80
# 或
sudo ss -tulnp | grep :80
解决
sudo nano /etc/apache2/ports.conf
报错示例
AH00526: Syntax error on line xx of /etc/apache2/sites-enabled/xxx.conf
原因
</VirtualHost>排查
sudo apache2ctl configtest
解决
报错示例
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
原因
rewrite)解决
sudo a2enmod rewrite
sudo systemctl restart apache2
报错示例
AH01276: Permission denied
原因
DocumentRoot 不存在www-data 无权访问解决
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www
报错示例
php module not found
原因
解决
sudo apt install php libapache2-mod-php
sudo a2enmod php
sudo systemctl restart apache2
查看真实错误
sudo systemctl status apache2
sudo journalctl -xeu apache2
sudo apache2ctl configtest
sudo systemctl status apache2
sudo journalctl -xeu apache2
sudo ss -tulnp | grep :80
如果你愿意,可以直接贴出:
sudo apache2ctl configtest
sudo systemctl status apache2
我可以一步一步帮你修好 ✅