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…

Author
yatch

I am software engineer at foreign company in Japan.
My experience of embedded software is over 15 years.
I was in charge of designing of software which has embedded linux.
I love Raspberry Pi.
I earn 10000USD per year on the stock market.
I married Hong Konger.

Please follow me
Amazon LinuxAWSLightsail
Please follow me
The World's Linux Journal

Comments

タイトルとURLをコピーしました