最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】CentOS7中php-fpm.log日志文件太大

PHP crifan 260浏览 0评论
折腾:
【未解决】crifan.com网站又又挂了
期间,发现php-fpm.log文件太大,大到51G,导致磁盘空间满了,WordPress网站的mysql数据库无法正常启动。
现在去想办法解决php-fpm的log文件太大的问题
[[email protected] log]# ls -lh
total 51G
-rw------- 1 root root 51G Oct  9 04:22 php-fpm.log
[[email protected] log]# pwd
/usr/local/php/var/log
最好是能限制大小
php-fpm.log too large
nginx – php-fpm writing huge log file – Stack Overflow
plesk-php56-fpm Error log is huge | Plesk Forum
macos catalina – How to limit php-fpm.log size on Mac? – Stack Overflow
php-fpm error.log using up 100% disk space | cPanel Forums
Optimizing PHP-FPM for High Performance – Geekflare
The request-response cycle in the case of PHP (Image credit: ProinerTech)
How PHP and Nginx work together (Image credit: DataDog)
Php-errors.log – Is There Anyway I Can Restrict The File Size? – PHP – SitePoint Forums | Web Development & Design Community
php-fpm.log
php-fpm log rotation
Using Logrotate to Rotate PHP5-FPM Logs – Obstance
循环日志 但是是切分出新文件 不是限制大小 不是我要的
linux – How to Configure logrotate of php5-fpm.log? – Stack Overflow
linux – How to configure logrotate with php logs – Stack Overflow
php fpm – logrotate causes php5-fpm downtime – Server Fault
php-fpm.log limit
PHP: 配置 – Manual
去找php-fpm.conf
[[email protected] etc]# pwd
/usr/local/php/etc
[[email protected] etc]# ll
total 96
-rw-r--r-- 1 root root  1228 May  9  2019 pear.conf
drwxr-xr-x 2 root root  4096 May  9  2019 php.d
-rw-r--r-- 1 root root  1022 May  9  2019 php-fpm.conf
-rw-r--r-- 1 root root  5377 May  9  2019 php-fpm.conf.default
drwxr-xr-x 2 root root  4096 May  9  2019 php-fpm.d
-rw-r--r-- 1 root root 72217 May  9  2019 php.ini
去看看内容
[[email protected] etc]# cat php-fpm.conf
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;


[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = warning


emergency_restart_threshold = 30
emergency_restart_interval = 60s
process_control_timeout = 5s
daemonize = yes


;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;


[www]
listen = /dev/shm/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www

pm = dynamic
pm.max_children = 50
pm.start_servers = 30
pm.min_spare_servers = 20
pm.max_spare_servers = 50
pm.max_requests = 2048
pm.process_idle_timeout = 10s
request_terminate_timeout = 120
request_slowlog_timeout = 0

pm.status_path = /php-fpm_status
slowlog = var/log/slow.log
rlimit_files = 51200
rlimit_core = 0

catch_workers_output = yes
;env[HOSTNAME] = 
crifan.com
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
[[email protected] etc]# 
位置的确是:
error_log = log/php-fpm.log
level是
log_level = warning
去看看参数:rlimit_files
rlimit_files int
设置 master 进程的打开文件描述符 rlimit 数。
不是我以为的 rotate方面的设置
log_level string
错误级别。可用级别为:alert(必须立即处理),error(错误情况),warning(警告情况),notice(一般重要信息),debug(调试信息)。默认:notice。
可以考虑改为error
以便于减少log内容
php-fpm.log max size
permissions – log_errors_max_len = 1024 in php.ini, but php log keeps growing – Stack Overflow
还是建议用logrotate去实现类似效果:限制log日志da’xiao
Server runs out of the space because of PHP-FPM logs – Plesk Help Center
logrotate(8) – Linux man page
[[email protected] etc]# logrotate --help
Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
  -l, --log=STRING          Log file
  --version                 Display version information


Help options:
  -?, --help                Show this help message
  --usage                   Display brief usage message
logrotate(8) – Linux man page
rotate count
Log files are rotated count times before being removed or mailed to the address specified in a maildirective. If count is 0, old versions are removed rather than rotated.
size size
Log files are rotated only if they grow bigger then size bytes. If size is followed by k, the size is assumed to be in kilobytes. If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100Gare all valid.
看到参数有说 size可以限制大小决定是否rotate
目前看起来好像可以借用logrotate实现限定log日志太大的问题
不过先要去看看reopenlogs
centos7 reopenlogs
Setting up logrotate for php error log | Linode Questions
External log rotation and reopen logs doesn’t work · Issue #189 · karlheyes/icecast-kh
Rotating Logs on Unix and Linux – MariaDB Knowledge Base
此处自己这里没有reopenlogs
此处暂时觉得:
不去用lograte了
因为没有合适的
php fpm – logrotate causes php5-fpm downtime – Server Fault
提到的:
postrotate中需要的
/usr/lib/php/php7.0-fpm-reopenlogs
所以估计也不太能起效果
暂时只去:
删除php-fpm.log
[[email protected] log]# rm -rf php-fpm.log 
[[email protected] log]# ll
total 0
[[email protected] log]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        79G   25G   51G  33% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           1.9G   17M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs           379M     0  379M   0% /run/user/0
空间省出来51G了。
以及先去停止php-fpm
[[email protected] log]# service php-fpm stop
Redirecting to /bin/systemctl stop php-fpm.service
[[email protected] log]# service php-fpm status
Redirecting to /bin/systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Fri 2020-10-09 10:48:48 CST; 9s ago
     Docs: http://php.net/docs.php
  Process: 2987 ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf (code=exited, status=0/SUCCESS)
 Main PID: 2987 (code=exited, status=0/SUCCESS)


Oct 09 09:42:41 crifan.com systemd[1]: Started The PHP FastCGI Process Manager.
Oct 09 10:48:48 crifan.com systemd[1]: Stopping The PHP FastCGI Process Manager...
Oct 09 10:48:48 crifan.com systemd[1]: Stopped The PHP FastCGI Process Manager.
再去更改设置为
/usr/local/php/etc/php-fpm.conf
中的
log_level = warning
log_level = error
[[email protected] etc]# pwd
/usr/local/php/etc
[[email protected] etc]# cat php-fpm.conf
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;


[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
#log_level = warning
log_level = error
重启php-fpm
[[email protected] log]# service php-fpm start
Redirecting to /bin/systemctl start php-fpm.service
[[email protected] log]# service php-fpm status
Redirecting to /bin/systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2020-10-09 10:50:34 CST; 5s ago
     Docs: http://php.net/docs.php
  Process: 10145 ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf (code=exited, status=78)
 Main PID: 10145 (code=exited, status=78)


Oct 09 10:50:34 crifan.com systemd[1]: Started The PHP FastCGI Process Manager.
Oct 09 10:50:34 crifan.com php-fpm[10145]: [09-Oct-2020 10:50:34] ERROR: [/usr/local/php/etc/php-fpm.conf:12] unknown entry '#log_level'
Oct 09 10:50:34 crifan.com php-fpm[10145]: [09-Oct-2020 10:50:34] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
Oct 09 10:50:34 crifan.com php-fpm[10145]: [09-Oct-2020 10:50:34] ERROR: FPM initialization failed
Oct 09 10:50:34 crifan.com systemd[1]: php-fpm.service: main process exited, code=exited, status=78/n/a
Oct 09 10:50:34 crifan.com systemd[1]: Unit php-fpm.service entered failed state.
Oct 09 10:50:34 crifan.com systemd[1]: php-fpm.service failed.
算了。重启VPS后看看php是否正常启动
问题依旧
发现原因了:
 unknown entry '#log_level'
看来不是#作为注释,而是逗号
所以改为:
[[email protected] ~]# cat /usr/local/php/etc/php-fpm.conf
。。。
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
;log_level = warning
log_level = error
再去重启
[[email protected] ~]# service php-fpm restart            
Redirecting to /bin/systemctl restart php-fpm.service
[[email protected] ~]# service php-fpm status             
Redirecting to /bin/systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-10-09 10:53:49 CST; 2s ago
     Docs: http://php.net/docs.php
 Main PID: 4449 (php-fpm)
   CGroup: /system.slice/php-fpm.service
           ├─4449 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
           ├─4459 php-fpm: pool www
           ├─4460 php-fpm: pool www
           ├─4461 php-fpm: pool www
           ├─4462 php-fpm: pool www
           ├─4463 php-fpm: pool www
           ├─4464 php-fpm: pool www
           ├─4465 php-fpm: pool www
           ├─4466 php-fpm: pool www
           ├─4467 php-fpm: pool www
           ├─4468 php-fpm: pool www
           ├─4469 php-fpm: pool www
           ├─4470 php-fpm: pool www
           ├─4471 php-fpm: pool www
           ├─4472 php-fpm: pool www
           ├─4473 php-fpm: pool www
           ├─4474 php-fpm: pool www
           ├─4475 php-fpm: pool www
           ├─4476 php-fpm: pool www
           ├─4477 php-fpm: pool www
           ├─4478 php-fpm: pool www
           ├─4479 php-fpm: pool www
           ├─4480 php-fpm: pool www
           ├─4481 php-fpm: pool www
           ├─4482 php-fpm: pool www
           ├─4483 php-fpm: pool www
           ├─4484 php-fpm: pool www
           ├─4485 php-fpm: pool www
           ├─4486 php-fpm: pool www
           ├─4487 php-fpm: pool www
           └─4488 php-fpm: pool www


Oct 09 10:53:49 crifan.com systemd[1]: Started The PHP FastCGI Process Manager.
就正常了。
【总结】
此处,发现
/usr/local/php/var/log/php-fpm.log
太大,有:51G
需要解决此php-fpm.log太大的问题
除了删除了事,希望:
限制大小
最终是:
方案1:
如果此处有
php fpm – logrotate causes php5-fpm downtime – Server Fault
提到的reopenlogs:
/usr/lib/php/php7.0-fpm-reopenlogs
则可以充分利用:logrotate去实现日志分割,且可以设置对应的size等参数限制大小
具体参数可以参考:
logrotate(8) – Linux man page
如何设置可以参考:
php fpm – logrotate causes php5-fpm downtime – Server Fault
Using Logrotate to Rotate PHP5-FPM Logs – Obstance
linux – How to configure logrotate with php logs – Stack Overflow
Setting up logrotate for php error log | Linode Questions
Rotating Logs on Unix and Linux – MariaDB Knowledge Base
方案2:
此处没有上述的reopenlogs,也就不方便用logrotate了
所以只能简单粗暴的删除了php-fpm.log
rm -rf /usr/local/php/var/log/php-fpm.log
另外进一步的,把log的level从warning改为error:
vi /usr/local/php/etc/php-fpm.conf

。。。
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
;log_level = warning
log_level = error
-》希望以后php-fpm能少产生一些log
附录:
php-fpm.conf中参数含义:
PHP: 配置 – Manual

转载请注明:在路上 » 【已解决】CentOS7中php-fpm.log日志文件太大

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.096 seconds, using 20.73MB memory