温馨提示×

怎样调试debian apache

小樊
35
2026-09-15 16:34:24
栏目: 智能运维

下面按常见场景给你一份「Debian 上调试 Apache」的实用清单,从最基础到进阶,基本能覆盖 90% 的问题。


一、基础服务状态检查

1. 查看 Apache 是否运行

systemctl status apache2

或

service apache2 status

启动 / 重启 / 停止:

sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl stop apache2

二、配置语法检查(最重要)

改完配置先检查语法:

sudo apache2ctl configtest

或

apache2 -t

✅ 输出 Syntax OK 才安全


三、查看 Apache 错误日志(第一现场)

默认日志位置

/var/log/apache2/error.log

实时查看:

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

访问日志:

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

四、确认端口与监听

查看监听端口

sudo ss -tulnp | grep apache
# 或
sudo netstat -tulnp | grep apache

默认端口:

  • 80(http)
  • 443(https)

如果端口被占用:

sudo lsof -i :80

五、虚拟主机(VirtualHost)调试

列出已启用站点

ls /etc/apache2/sites-enabled/

测试某个 vhost

apache2ctl -S

常见错误:

  • DocumentRoot 不存在
  • 权限不足(见下)

六、权限问题(非常常见)

目录权限

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

Apache 配置中必须有:

<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

修改后:

sudo systemctl reload apache2

七、开启详细错误显示(开发环境)

修改虚拟主机或全局配置

LogLevel alert trace8

或

LogLevel debug

重启 Apache 后看 error.log

⚠️ 生产环境不要长期开 debug


八、PHP / 模块调试

查看已加载模块

apache2ctl -M

启用模块

sudo a2enmod rewrite
sudo a2enmod ssl
sudo systemctl restart apache2

PHP 不解析?

sudo apt install libapache2-mod-php
sudo a2enmod php
sudo systemctl restart apache2

九、防火墙 / SELinux 类问题(Debian 少)

UFW

sudo ufw allow 80
sudo ufw allow 443

十、快速自检清单 ✅

  • [ ] configtest 通过
  • [ ] error.log 无致命错误
  • [ ] 端口在监听
  • [ ] 文件权限正确
  • [ ] vhost 已 enable
  • [ ] 浏览器清缓存测试

如果你愿意,可以告诉我:

  • Debian 版本(11 / 12?)
  • Apache 版本
  • 具体报错信息(贴 error.log)

我可以直接帮你针对性排错。

0 踩