Ubuntu 上 Apache2 报错的原因非常多,需要先看具体错误信息才能准确判断。下面我按最常见情况给你一个排查清单,你可以对照看看是哪一种。
报错示例:
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
原因:
解决:
sudo netstat -tulnp | grep :80
或
sudo lsof -i :80
关闭占用程序或改端口:
sudo systemctl stop nginx
报错示例:
AH00526: Syntax error on line 10 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
解决:
sudo a2enmod rewrite
sudo systemctl restart apache2
报错示例:
403 Forbidden
You don't have permission to access this resource.
原因:
/var/www 目录权限不对www-data)无权限解决:
sudo chown -R www-data:www-data /var/www
sudo chmod -R 755 /var/www
报错:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name
或
DocumentRoot does not exist
解决: 确认:
ls /var/www/html
不存在就创建或改配置。
sudo systemctl status apache2
sudo journalctl -xe
sudo apache2ctl configtest
sudo systemctl status apache2
sudo journalctl -xe | tail -n 50
请把以下信息贴出来:
lsb_release -a)apache2 -v)我可以直接告诉你是哪一行配置或哪个服务的问题。