折腾:
【未解决】crifan.com网站又又挂了
期间,发现php-fpm.log文件太大,大到51G,导致磁盘空间满了,WordPress网站的mysql数据库无法正常启动。
现在去想办法解决php-fpm的log文件太大的问题
[root@crifan log]# ls -lh total 51G -rw------- 1 root root 51G Oct 9 04:22 php-fpm.log [root@crifan log]# pwd /usr/local/php/var/log
最好是能限制大小
php-fpm.log too large
The request-response cycle in the case of PHP (Image credit: ProinerTech)
How PHP and Nginx work together (Image credit: DataDog)
php-fpm.log
php-fpm log rotation
循环日志 但是是切分出新文件 不是限制大小 不是我要的
php-fpm.log limit
去找php-fpm.conf
[root@crifan etc]# pwd /usr/local/php/etc [root@crifan 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
去看看内容
[root@crifan 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 [root@crifan 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
还是建议用logrotate去实现类似效果:限制log日志da’xiao
[root@crifan 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
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
此处自己这里没有reopenlogs
此处暂时觉得:
不去用lograte了
因为没有合适的
提到的:
postrotate中需要的
/usr/lib/php/php7.0-fpm-reopenlogs
所以估计也不太能起效果
暂时只去:
删除php-fpm.log
[root@crifan log]# rm -rf php-fpm.log [root@crifan log]# ll total 0 [root@crifan 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
[root@crifan log]# service php-fpm stop Redirecting to /bin/systemctl stop php-fpm.service [root@crifan 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
[root@crifan etc]# pwd /usr/local/php/etc [root@crifan 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
[root@crifan log]# service php-fpm start Redirecting to /bin/systemctl start php-fpm.service [root@crifan 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'
看来不是#作为注释,而是逗号
所以改为:
[root@crifan ~]# 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
再去重启
[root@crifan ~]# service php-fpm restart Redirecting to /bin/systemctl restart php-fpm.service [root@crifan ~]# 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:
如果此处有
提到的reopenlogs:
/usr/lib/php/php7.0-fpm-reopenlogs
则可以充分利用:logrotate去实现日志分割,且可以设置对应的size等参数限制大小
具体参数可以参考:
如何设置可以参考:
方案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中参数含义:
转载请注明:在路上 » 【已解决】CentOS7中php-fpm.log日志文件太大