AWS Elastic Beanstalk PHP日志不在CloudWatch中



我有一个通过AWS Elastic Beanstalk运行的PHP应用程序。但是PHP错误日志似乎没有与访问日志等一起包含在CloudWatch中。我该如何将它们发送到CloudWatch?

根据一些拼写,php错误日志似乎被发送到/var/logs/php-fpm/www-error.log,由/etc/php-fpm.d/www.conf:中的设置决定

php_admin_value[error_log] = /var/log/php-fpm/www-error.log

根据这里的信息,发送到CloudWatch for PHP的日志只有:

/var/log/eb-engine.log
/var/log/eb-hooks.log
/var/log/httpd/access_log
/var/log/httpd/error_log
/var/log/nginx/access.log
/var/log/nginx/error.log

您可以添加自定义配置,让CloudWatch代理获取正确的文件。或者,您可以将php错误消息添加到已经发送的文件中。这可以通过文件.ebextensions/my.config:中的以下内容来完成

/etc/php-fpm.d/www-my-overrides.conf:
mode: "000644"
owner: root
group: root
# For some reason, EB configures the php errors to go to /var/log/php-fpm/www-error.log,
# but doesn't include that file in the default log files sent to CloudWatch. This directs
# the log files to the error file that is being sent to CloudWatch
content: |
[www]
php_admin_value[error_log] = /var/log/httpd/error_log

我不确定,但我认为同一目录中的www-my-overrides.conf文件名需要按字母顺序排列在www.confg之后。

如果您使用的是nginx,那么您需要使用/var/log/nginx/error.log作为错误日志目标——除非您使用Apache,否则CloudWatch似乎会忽略/var/log/httpd,所以即使您向它写入,更改也不会显示在CloudWatch中。

files:
/etc/php-fpm.d/www-my-overrides.conf:
mode: "000644"
owner: root
group: root
# For some reason, EB configures the php errors to go to /var/log/php-fpm/www-error.log,
# but doesn't include that file in the default log files sent to CloudWatch. This directs
# the log files to the error file that is being sent to CloudWatch
content: |
[www]
php_admin_value[error_log] = /var/log/nginx/error.log

此外,您需要使该文件可由php-fpm进程写入,该进程默认情况下以webapp运行,此外,您还需要确保它存在。。。这在创建新实例时还没有实现,因此执行以下两个命令非常重要:

container_commands:
01-command:
command: echo "-- DEPLOYMENT --" >> /var/log/nginx/error.log
02-command:
command: chmod 666 /var/log/nginx/error.log

最新更新