Setup php-fpm 7 with Apache on Debian 9 Stretch

From ETCwiki
Jump to: navigation, search

These are steps to install php-fpm7.0 on a Debian 9 Stretch server. php-fpm allows for faster processing of PHP documents while using less memory than Apache2 with standard php7.0. This means you can run faster websites with less resource use.


Written: 1-16-2018


  • VPS - Debian 9.2
  • Apache 2.4
  • Memory: 1GB

Set up apache2

apt-get update
apt-get install apache2 php7.0

Test apache2 with phpinfo

rm /var/www/html/index.html
echo "<?php phpinfo(); ?>" > /var/www/html/index.php

Note:Access your server, note the Server API

Install php-fpm

apt-get install php-fpm

Change listen address for php-fpm

vim /etc/php/7.0/fpm/pool.d/www.conf
Line 36 should be changed to this:
listen = 127.0.0.1:9000

Add PHP handler to virtualhost or apache2.conf

<FilesMatch "\.php$">
	SetHandler "proxy:fcgi://127.0.0.1:9000/"
</FilesMatch>

Note: this tells Apache (global) or a specific virtualhost (per host) to use php-fpm depending on where you put it.

Enable php-fpm

a2enmod proxy_fcgi

Restart apache and test

service apache2 restart
service php7.0-fpm restart

Check your server's phpinfo for Server API - it should be "FastCGI" now

Speed and Performance

I tested the speed using Pingdom and Webpagetest before and after php-fpm installation on my test server that was under 0 load. Running my test Wordpress website averaged 2.1 seconds load time before php-fpm, and 1.3 seconds after. With MySQL, 4x php-fpm7.0 processes, and 4x apache2 prefork processes I was running at ~300MB real memory usage, with my buffers and cache filling up 600MB.

More speed

  • Tune php-fpm's pool.d/www.conf file for more or less children depending on your resources
  • Switch to Apache Worker MPM instead of prefork. This only works on some sites, but it allows you to disable mod_php and use php-fpm with threaded Apache2 children.

External Links