VestaCP comes prepacked with PHP-FPM (if you select the option when installing) which manages PHP processes. By default it spawns processes that stay alive constantly eating memory. We can further optimise the PHP-FPM settings by tweaking the configuration that’s made when adding a new domain name in an attempt to reduce PHP-FPM memory usage.
VestaCP default PHP-FPM settings
The file /usr/local/vesta/data/templates/web/php-fpm/default.tpl
looks by default like this:
[%backend%] listen = 127.0.0.1:%backend_port% listen.allowed_clients = 127.0.0.1 user = %user% group = %user% pm = dynamic pm.max_children = 50 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 10 env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
And the socket file /usr/local/vesta/data/templates/web/php-fpm/socket.tpl
looks like this:
[%backend%] listen = /var/run/php5-%backend%.sock listen.allowed_clients = 127.0.0.1 user = %user% group = %user% listen.owner = %user% listen.group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 10 env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
Reduce PHP-FPM memory usage
Change the file /usr/local/vesta/data/templates/web/php-fpm/default.tpl
to this:
[%backend%] listen = 127.0.0.1:%backend_port% listen.allowed_clients = 127.0.0.1 user = %user% group = %user% pm = ondemand pm.max_children = 50 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 10 pm.process_idle_timeout = 10s pm.max_requests = 500 env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
Now change the file /usr/local/vesta/data/templates/web/php-fpm/socket.tpl
to this:
[%backend%] listen = /var/run/php5-%backend%.sock listen.allowed_clients = 127.0.0.1 user = %user% group = %user% listen.owner = %user% listen.group = nginx pm = ondemand pm.max_children = 50 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 10 pm.process_idle_timeout = 10s pm.max_requests = 500 env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
Essentially, what this does is make PHP-FPM spawn PHP processes when needed, and let others die. By changing pm = dynamic
to pm = ondemand
we can use PHP-FPM’s feature to reduce PHP-FPM memory usage.
I have also added in pm.process_idle_timeout = 10s
which will kill the PHP process after 10 seconds of inactivity, in an attempt to reduce PHP-FPM memory usage, and set pm.max_requests = 500
. The pm.max_requests
setting is advised as covered here (basically helping to stop memory leaks).
Rebuild all the PHP-FPM configuration files
After changing these files the settings will become active for any new website added. Unfortunately, at this time it’s not possible to use any of VestaCP’s “rebuild” functions to re-create the PHP-FPM configuration settings for each domain name. You have to edit each file individually by adding/changing the new settings:
pm = ondemand pm.process_idle_timeout = 10s pm.max_requests = 500
The location of every PHP-FPM configuration file is here: /etc/php-fpm.d
Restart PHP-FPM
For whatever reason sometimes I find that the linux command doesn’t restart the PHP-FPM processes, so I simply log into Vesta and click the Server link and hit restart on PHP-FPM and NGINX.
All done! Now you have reduced PHP-FPM’s memory usage!
You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>