温馨提示×

如何调试ubuntu apache2

小樊
35
2026-08-01 05:34:38
栏目: 智能运维

在 Ubuntu 上调试 Apache2 可以按下面几个层次来做,从“看日志 → 查配置 → 查端口/权限 → 查模块/环境”逐步排查。


一、先确认 Apache 是否正常运行

sudo systemctl status apache2

常见状态:

  • active (running):服务正常
  • inactive / failed:服务没起来

重启 / 重载:

sudo systemctl restart apache2
sudo systemctl reload apache2

二、查看 Apache 错误日志(最关键)

1️⃣ 主错误日志

sudo tail -n 50 /var/log/apache2/error.log

或实时查看:

sudo tail -f /var/log/apache2/error.log

常见错误:

  • Permission denied
  • File does not exist
  • AH00558: Could not reliably determine server's fully qualified domain name
  • Address already in use

三、检查配置文件是否正确

1️⃣ 测试配置文件语法

sudo apache2ctl configtest

或:

sudo apache2 -t

✅ 返回 Syntax OK 表示配置没问题
❌ 有错误会直接提示哪一行有问题


2️⃣ 检查虚拟主机配置

ls /etc/apache2/sites-enabled/

查看某个站点:

cat /etc/apache2/sites-enabled/000-default.conf

常见错误:

  • DocumentRoot 路径不存在
  • ServerName 写错
  • 端口冲突

四、检查端口是否被占用

Apache 默认使用 80 / 443

sudo netstat -tulnp | grep :80
sudo ss -tulnp | grep :80

如果端口被占用(如 nginx):

sudo systemctl stop nginx

五、检查文件和目录权限(非常常见)

1️⃣ Apache 运行用户

ps aux | grep apache

通常是:

  • www-data

2️⃣ 网站目录权限

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

⚠️ 不要随便 chmod 777


六、检查是否启用必要模块

查看已启用模块:

apache2ctl -M

启用模块示例:

sudo a2enmod rewrite
sudo a2enmod ssl
sudo systemctl restart apache2

禁用模块:

sudo a2dismod rewrite

七、检查虚拟主机是否启用

sudo a2ensite your-site.conf
sudo systemctl reload apache2

禁用:

sudo a2dissite your-site.conf

八、测试访问(本地)

curl http://localhost

或:

curl -I http://localhost

九、常见错误速查表

问题 解决方案
403 Forbidden 目录权限 / <Directory> 配置
404 Not Found DocumentRoot 路径错误
500 Internal Error PHP 错误 / .htaccess
无法访问 ufw 防火墙
端口冲突 nginx / 其他程序占用

防火墙检查:

sudo ufw status
sudo ufw allow 80
sudo ufw allow 443

十、调试 PHP(如果用 PHP)

sudo tail -f /var/log/apache2/error.log

或开启 PHP 错误显示(开发环境):

ini_set('display_errors', 1);
error_reporting(E_ALL);

如果你愿意,我可以:

  • ✅ 根据你 具体报错 帮你定位
  • ✅ 帮你 一步步排查某个网站打不开
  • ✅ 帮你 配置 HTTPS / 虚拟主机

你可以直接把 error.log 报错内容访问现象 发给我。

0