要将AWS Elastic Beanstalk PHP日志发送到CloudWatch,您需要更新您的Elastic Beanstalk环境配置文件并添加一些代码。
以下是解决方法的步骤:
编辑您的Elastic Beanstalk环境配置文件(.ebextensions文件夹中的文件,或者通过AWS管理控制台的“配置”选项卡)。
添加以下配置部分,用于创建一个新的CloudWatch Logs日志组和日志流,并将PHP错误日志发送到该日志流:
files:
"/etc/awslogs/config/php-error.conf":
mode: "000644"
owner: root
group: root
content: |
[php-error]
log_group_name = /aws/elasticbeanstalk/[environment]/php-error
log_stream_name = {instance_id}
file = /var/log/php-fpm/error.log
datetime_format = %Y-%m-%d %H:%M:%S
initial_position = start_of_file
encoding = utf-8
buffer_duration = 5000
batch_count = 1000
batch_size = 32768
retry_duration = 1000
max_retries = 5
multiline_pattern = ^\[
time_zone = UTC
commands:
"01":
command: "chkconfig awslogs on"
请确保将[environment]
替换为您的环境名称。
保存配置文件并重新部署Elastic Beanstalk环境。
重启环境以使配置更改生效。
该配置将在EC2实例上创建一个名为php-error
的日志组,并为每个EC2实例创建一个日志流。PHP错误日志将被发送到/var/log/php-fpm/error.log
文件中,并通过AWS日志代理(awslogs)发送到CloudWatch Logs中。
您可以在AWS管理控制台的CloudWatch Logs页面中查看和搜索PHP错误日志。