在 Ubuntu 上,Apache 日志默认就是开启的,一般是访问日志(access log)和错误日志(error log)。如果你发现没有日志或想确认/自定义开启方式,可以按下面步骤操作。
sudo systemctl status apache2
如果没运行:
sudo systemctl start apache2
Ubuntu 中 Apache 默认日志路径:
/var/log/apache2/error.log/var/log/apache2/access.log查看日志:
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
Apache 使用 mod_log_config:
apache2ctl -M | grep log_config
应有:
log_config_module (shared)
如未启用:
sudo a2enmod log_config
sudo systemctl restart apache2
打开站点配置(以默认站点为例):
sudo nano /etc/apache2/sites-available/000-default.conf
确认有以下内容:
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
如果缺失,可手动添加,然后:
sudo systemctl restart apache2
主配置文件:
sudo nano /etc/apache2/apache2.conf
常见日志设置:
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error.log
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog ${APACHE_LOG_DIR}/access.log common
修改后重启:
sudo systemctl restart apache2
sudo ls -l /var/log/apache2/
不存在可手动创建并授权:
sudo touch /var/log/apache2/access.log
sudo chown www-data:adm /var/log/apache2/access.log
Apache 用户通常是 www-data
LogLevel debug
⚠️ 生产环境不建议长期开启
如果你是想按虚拟主机分开日志、关闭某个日志、或配置 JSON 日志,可以告诉我你的具体需求。