Investigation if php-fpm processes on LightSail can be increased.

Amazon Linux

In below article I upgraded LightSail instance from 3.5USD/512MB to 5USD/1GB.



RAM are added 512MB so I came up the idea to increase processes of php-fpm to work faster.
This article is how I confirmed this idea.

In conclusion I decided to keep current configuration.
Do not increase processes.

Firstly I checked free memory by running script to show free memory.
– Used memory by httpd/mysqld/php-fpm are also visible.
I referred this Japanese site, thanks so much!

メモリの共有分を考慮してApacheのメモリ使用量を割り出す - grep Tips *
ps auxの結果でてくるRSS(プロセスが実際に使用しているRAMの総メモリ量)の値を見て、Apacheのメモリ使用量を計算すると、Apach
#!/bin/bash

min=65535
min_time=''
loop=0
loop_per_day=17280 # 86400 / 5
export TZ="Asia/Tokyo"

declare -a processes=("httpd" "mysqld" "php-fpm")
declare -a max_mem=(0 0 0)

function get_max_mem_usage() {
	process=$1
	ps -ef | grep ${process} | grep -v root | grep -v grep | \
	while read line;
	do
		args=(${line});
		echo -ne ${args[1]}"\t"${args[2]}"\t";sudo cat /proc/${args[1]}/smaps \
		|awk 'BEGIN{rss=0;shared=0;private=0;}/Rss/{rss+=$2;}/Shared/{shared+=$2;}/Private/{private+=$2;}END{printf("%.1f\t%.1f\t%.1f\t%.1f\n",rss/1024,shared/1024,private/1024,(rss-shared)/1024);}';
	done \
	| awk '{sum+=$5}END{print sum}'
}
	
while :
do
	now=`date`
	freemem=$(free -m  | grep ^Mem | tr -s ' ' | cut -d ' ' -f 7)
	if [ ${min} -gt ${freemem} ]; then
		min=${freemem}
		min_time=${now}
	fi
	echo ${now} | tee -a stat.txt
	echo "Current available is ${freemem}MB at ${now}, Minimum available is ${min}MB at ${min_time}" | tee -a stat.txt

	for process in ${processes[@]}
	do
		mem_usage=`get_max_mem_usage ${process}`
		echo "${process} uses"  ${mem_usage} "MB" | tee -a stat.txt
	done

	echo "" >> stat.txt
	if [ ${loop} -ge ${loop_per_day} ]; then
		min=65535
		min_time=''
		loop=0
	fi
	loop=$((loop+1))
	sleep 5
done



Below screen shot was taken when I access this blog page some times.
MemAvailable is 174MB, php-fpm totally uses 152.9MB.
I feel that increasing process of php-fpm may not be safe considering oom killer.



Configuration of php-fpm focusing how many processes are below.

vim /opt/bitnami/php/etc/php-fpm.conf

pm=ondemand
pm.max_children=5
pm.process_idle_timeout=10



Each configuration is as follows.

 ;  ondemand - no children are created at startup. Children will be forked when
 ;             new requests will connect. The following parameter are used:
 ;             pm.max_children           - the maximum number of children that
 ;                                         can be alive at the same time.
 ;             pm.process_idle_timeout   - The number of seconds after which
 ;                                         an idle process will be killed.



To summarize current settings,

No child processes at start up.
New child process will be created by fork() when request comes.
Maximum child process count is pm.max_children.
 - No new child process is created even if additional request comes.
   (New request is queued to backlog and processed when on-going request is processed)
Idle child process will be killed after 10 seconds.
 -> Free memory will be increased.



We can set static/dynamic to pm as well as ondemand.

static means fixed number of processes are running.
No new child processes are created so performance is higher than others.
Also comsuming memory by php-fpm is almost fixed so it is easily understandable.

;   static  - a fixed number (pm.max_children) of child processes;



dynamic intends to keep some idle processes to be ready.
It has many sub parameters than other two so configuring needs more investigation/tests.
– For high skilled engineers.

;   dynamic - the number of child processes are set dynamically based on the
 ;             following directives. With this process management, there will be
 ;             always at least 1 children.
 ;             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.
 ;             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.

Conclusion

How was it?

If you have some good configuration than me, sharing it will be highly appreciated!
– I am also looking for optimal performance and stability…

Comments

  1. Thanks for the post, there isn’t much out there that I could find on this subject. I am having problems with my server also, lightsail bitnami wordpress multisite. Getting this in my logs: [21-Jun-2023 11:03:33] WARNING: [pool www] server reached pm.max_children setting (15), consider raising it

    it looks like its set to dynamic, I guess I’ll try adding the max_children parameter and setting it to something higher than 15. I have a feeling its not going to work though.

    • yatch says:

      Hi, thank you for being interested in my post!

      In default of bitnami wordpress multisite, configuration is likely set as “ondemand” in my case.

      > [21-Jun-2023 11:03:33] WARNING: [pool www] server reached pm.max_children setting (15), consider raising it

      This means your php-fpm is configured to run 15 processes at maximum and used all 15 processes.

      If your server computer has enough free RAM then you can increase number of pm.max_children so your php-fpm can handle more requests.

      In my case my instance doesn’t have much free RAM so I kept default configuration.

      Thanks.

      • There was alot of free memory actually, I started out on a server with 1GB ram but upgraded to one with 2GB, and I used am image of the 1GB ram server as the base for the new one. I ended up moving to a more scaleable configuration though, a 2GB ram database server + a load balancer + 2 1GB app sever instances, and its working out much better than the single 2GB server and isn’t that much more expensive. 15$ for the database server, 13$ for the load balancer, and the app servers are 5$ each.

Copied title and URL