在 Ubuntu 上调试 Apache2 通常涉及查看日志、检查配置、测试服务状态等步骤。下面我按常见排错流程给你一份实用指南。
systemctl status apache2
常见状态:
active (running):正常failed:启动失败(看日志)inactive:未启动启动 / 重启:
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl enable apache2
sudo tail -n 50 /var/log/apache2/error.log
实时查看:
sudo tail -f /var/log/apache2/error.log
sudo tail -n 50 /var/log/apache2/access.log
sudo apache2ctl configtest
✅ 正常输出:
Syntax OK
❌ 有错误会指出具体文件和行号
Ubuntu Apache 配置位置:
/etc/apache2/
├── apache2.conf
├── ports.conf
├── sites-available/
├── sites-enabled/
├── mods-available/
└── mods-enabled/
ls /etc/apache2/sites-enabled/
sudo a2ensite your-site.conf
sudo systemctl reload apache2
sudo a2dissite 000-default.conf
Apache 默认端口:
80(HTTP)443(HTTPS)查看端口:
sudo netstat -tulnp | grep apache
# 或
sudo ss -tulnp | grep :80
端口被占用(如 nginx):
sudo systemctl stop nginx
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www
如果启用:
sudo a2enmod rewrite
sudo systemctl restart apache2
apache2ctl -M
sudo a2enmod ssl
sudo a2enmod rewrite
php -v
sudo apt install libapache2-mod-php
sudo apache2ctl -S
检查证书路径是否正确:
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/key.pem
sudo ufw status
允许 HTTP/HTTPS:
sudo ufw allow 80
sudo ufw allow 443
systemctl status apache2apache2ctl configtesterror.loga2ensite如果你愿意,可以告诉我:
我可以帮你精准定位问题。