Running both PHP 5 and PHP 7 without wasting resources
Installing both PHP 5 and PHP 7 next to each other seems to be fairly simple. In this blog post, I will be showing you how you can free up some resources while you're transitioning to PHP 7. Here's what I did.
On this Debian web server, I installed php7.0-fpm
from the Dotdeb repository without removing the old Debian php5-fpm
package. Turns out you can install both of these without any issues. Great!
After installing and configuring PHP 7, the only thing I had to do to switch a website to PHP 7 was changing this single line in my Nginx site configuration from:
fastcgi_pass unix:/var/run/php5-fpm.sock;
To:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
I was able to gradually roll out PHP 7 to each website and test for compatibility issues. Everything went great, except for this old CodeIgniter website that I'm still hosting for some friends.
When using the default configuration, both PHP version will start their FPM resource pool:
# php5
root php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data php-fpm: pool www
www-data php-fpm: pool www
# php7
root php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data php-fpm: pool www
www-data php-fpm: pool www
Having PHP 5 running as a fallback for older frameworks is great, but it is a shame that the resource pool is reserving precious memory while it is only handling a couple of requests. Time to fix this!
In your PHP 5 FPM pool configuration file, you can change the process management setting to ondemand
rather than the default dynamic
setting. In this mode, the FPM pool will only start the master process, and fork child processes when new requests come in.
# /etc/php5/fpm/pool.d/www.conf
pm = ondemand
This change might not save you hundreds of megabytes, but it feels like a great optimisation until you can completely kiss PHP 5 goodbye!
Graphic by Fabricio Rosa Marques