Catatan Pribadi untuk optimasi PHP-FPM

README.md Update README.md 10 days ago
README.md

Optimasi-PHP-FPM

Catatan Pribadi untuk optimasi PHP-FPM

Menyesuaikan child processes pada PHP-FPM (Nginx)

Saat mengatur opsi ini pertimbangkan hal berikut:

  • Berapa lama permintaan rata-rata Anda ?
  • Berapa jumlah maksimum pengunjung simultan yang didapat situs ?
  • Berapa banyak memori yang dikonsumsi rata-rata setiap child proses ?

Tentukan apakah batas max_children telah tercapai.

  • $ sudo grep max_children /var/log/php-fpm/error.log

Contoh Output :

[22-Mar-2020 12:34:09] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it
[22-Mar-2020 12:36:03] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it
[22-Mar-2020 12:37:35] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it
[22-Mar-2020 12:39:46] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it
[22-Mar-2020 12:53:40] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it
[22-Mar-2020 12:54:49] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it
[22-Mar-2020 13:01:19] WARNING: [pool www] server reached pm.max_children setting (20), consider raising it
[22-Mar-2020 13:02:50] WARNING: [pool www] server reached pm.max_children setting (20), consider raising it
[22-Mar-2020 13:03:03] WARNING: [pool www] server reached pm.max_children setting (20), consider raising it
[22-Mar-2020 13:10:03] WARNING: [pool www] server reached pm.max_children setting (20), consider raising it
[22-Mar-2020 13:12:56] WARNING: [pool www] server reached pm.max_children setting (14), consider raising it
[22-Mar-2020 13:14:04] WARNING: [pool www] server reached pm.max_children setting (14), consider raising it
[22-Mar-2020 13:15:05] WARNING: [pool www] server reached pm.max_children setting (14), consider raising it
[22-Mar-2020 13:16:22] WARNING: [pool www] server reached pm.max_children setting (14), consider raising it
[22-Mar-2020 13:17:39] WARNING: [pool www] server reached pm.max_children setting (45), consider raising it

Tentukan RAM dan rata - rata pool memory.

  • free -h
  • All fpm processes: ps -ylC php-fpm --sort:rss
  • Average memory: ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
  • All fpm processes memory: ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm

Output :

85.66 Mb php-fpm: pool www
83.43 Mb php-fpm: pool www
83.38 Mb php-fpm: pool www
6.54 Mb php-fpm: master process (/etc/php-fpm.conf)
0.32 Mb grep --color=auto php-fpm

Hitung max_children

Based on RAM

  • pm.max_children = Total RAM yang didedikasikan untuk server web / Ukuran maksimal child process

  • System RAM: 2GB

  • Average Pool size: 83Mb
  • pm.max_children = 1500MB / 83MB = 18

Based on average script execution time

  • max_children = (waktu eksekusi skrip PHP rata-rata) * (PHP requests per second)
  • visitors = max_children * (detik antara tampilan halaman) / (rata-rata waktu eksekusi)

Configure

sudo vim /etc/php-fpm.d/www.conf

pm.max_children = 18
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_request = 1000
; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives:
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;
;             pm.start_servers     - the number of children created on startup.
;                                    this value must not be less than min_spare_servers 
;                                    and not greater than max_spare_servers.
;
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
; Note: This value is mandatory.