Optimize PHP-FPM on VestaCP
Optimize PHP-FPM on VestaCP
We had seen installing VestaCP in our budget website tutorial series. VestaCP provides an easy to use web interface to manage and maintain your datacenter. VestaCP comes with PHP-FPM and there are a lot of different ways to efficiently configure it for optimal performance. On one of our datacenter which we manage was consuming more than 80% of memory even with SWAP (Find out more on SWAP space calculation here). The average CPU utilization was also well above 10% for most of the time. Hence we started to look at the logs and configurations as to what can we do to optimize the server. We looked at a few options where we stumbled upon an article by Steven Sullivanon reducing PHP-FPM memory usage. The article mentioned is an year old and there are some enhancements in VestaCP which can automate a few process.
Let’s dive into the configuration now. VestaCP has a bunch of templates that are used when you create a new site. One of the configuration template is for PHP-FPM settings. There are 2 template files which you can find in the following locations
/usr/local/vesta/data/templates/web/php-fpm/default.tpl /usr/local/vesta/data/templates/web/php-fpm/socket.tpl
Now the template files are default configurations. You can make the following changes to the template files to reduce the memory usage
In both default.tpl and socket.tpl change the line pm = dymanic to pm = ondemand and add the following lines below pm.max_spare_servers
pm.process_idle_timeout = 10s pm.max_requests = 500
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 its memory usage.
We 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
Now this is where things get a bit tricky. Earlier it was not possible to rebuild all your web domain configurations at once. But you can now do the same with the v-rebuild-web-domains.
Run the following to command to rebuild all the config
Make sure that you backup the nginx.conf and snginx.conf as these will also be rebuilt. In case you have custom rules you can just restore the configs from your backup.
$ /usr/local/vesta/bin $ ./v-rebuild-web-domains [USER]
The argument [USER] for v-rebuild-web-domains will be the username for which the web domains will be regenerated.
Once all the domains are regenerated restart PHP-FPM with
$ service php-fpm restart
After this anytime you create a new web domain it will be created with the optimized configuration and rebuilding again is not required.
That’s it. You have now successfully optimized PHP-FPM.
Conclusion
After we made these changes, the datacenter resource utilization was approximately 40% lower of what it used to be.
We use NodeQuery to monitor our servers and below is the hourly RAM & SWAP utilization graph