0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

nginx+php-fpmのパフォーマンスチューニング格闘記

Last updated at Posted at 2024-07-12

環境

  • ubuntu 22
  • nginx
  • php-fpm 8.3
  • さくらのVPS 1G スワップは2G

事象発生

> nginxが504エラー投げてます!
> なにーっ

とりあえず復旧

nginx のエラーログをみてみる

Ubuntu
tail -n 10 /var/log/nginx/error.log
/var/log/nginx/error.log
2024/07/12 14:48:46 [error] 2900603#2900603: *8358 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 122.210.79.154, server: hoge.jp, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock", host: "hoge.jp"

php-fpmがまた落ちてるなー

とりあえず再起動で復活するからなおしちゃお

Ubuntu
systemctl restart php8.3-fpm
systemctl restart nginx

原因調査

php-fpmのエラーログってどこにあるんだっけな

Ubuntu
vi /etc/php/8.3/fpm/php-fpm.conf
/etc/php/8.3/fpm/php-fpm.conf
error_log = /var/log/php8.3-fpm.log

あったあった

のぞいてみる

Ubuntu
tail -n 100 /var/log/php8.3-fpm.log
/var/log/php8.3-fpm.log
[08-Jul-2024 11:12:00] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[10-Jul-2024 13:16:08] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[11-Jul-2024 09:08:50] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[12-Jul-2024 15:03:46] NOTICE: Terminating ...
[12-Jul-2024 15:03:46] NOTICE: exiting, bye-bye!
[12-Jul-2024 15:03:46] NOTICE: fpm is running, pid 3170323
[12-Jul-2024 15:03:46] NOTICE: ready to handle connections
[12-Jul-2024 15:03:46] NOTICE: systemd monitor interval set to 10000ms

server reached pm.max_children setting (5), consider raising it

これかなあ。

とりあえず和訳してみる

サーバーが pm.max_children 設定 (5) に達したため、値を上げることを検討してください

わかったようなわからないような

恒久対策

php-fpmが確保している平均メモリ量を調べる

Ubuntu
ps -ylC php-fpm8.3 --sort:rss
# 出力結果
# RSSがメモリ使用率でKBで表示されているらしい
# 36156は36156KB
S   UID     PID    PPID  C PRI  NI   RSS    SZ WCHAN  TTY          TIME CMD
S     0 3536299       1  0  80   0 36156 66629 -      ?        00:00:00 php-fpm8.3
S   114 3536318 3536299  0  80   0 49800 67068 -      ?        00:00:00 php-fpm8.3
S   114 3536307 3536299  0  80   0 50316 67155 -      ?        00:00:00 php-fpm8.3
# ...以下省略

平均を算出

Ubuntu
ps --no-headers -o "rss,cmd" -C php-fpm8.3 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
# 出力結果
52M

私の場合は、1子プロセスにつき平均して52MBを使っているらしい

で、全体で使える1G(物理メモリ)+2GB(swap領域)の3GBなので値をこんなかんじにしてみた

pm.max_children = 40
pm.start_servers = 15
pm.min_spare_servers = 5
pm.max_spare_servers = 25
pm.max_requests = 1500

再起動して反映

systemctl restart php8.3-fpm
systemctl restart nginx

とりあえずこれで様子見

また繰り返すようであれば更新します~

参考

ありがとうございます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?