Setup PHP-FPM for Apache2 / Nginx on Ubuntu 17.04 | 17.10

PHP-FPM (FastCGI Process Manager) is an alternative to FastCGI developed to help PHP-based websites run faster. It’s designed to speed up PHP processors and scripts. If you’re running a PHP-based website or blog, like WordPress, Joomla or others, you may want to install and enable PHP-FPM to speed it up.

This brief tutorial shows students and new users how to install and configure Apache2 or Nginx webserver to use PHP-FPM. Most WordPress setups will have PHP-FPM module installed and configure. Not many on Apache2, but enabling it for Apache2 may also provide benefits

So, to speed up your PHP-based websites, follow the steps below:

This tutorial assumes that you have already installed Apache2 or Nginx webserver and they’re functioning okay. The steps below enable Apache2 / Nginx to route PHP requests through PHP-FPM to handle.

Step 1: Install PHP-FPM and Apache2 Module

To get PHP-FPM installed and enabled on Apache2, run the commands below:

sudo apt-get update
sudo apt-get install php-fpm

After running the commands above, the module should be installed and ready to use. To configure Apache2 to use it continue below.

Step 2: Configure Apache2 to use PHP-FPM

Now that the module is installed, open the default configuration file for the module and configure the highlighted settings below.

sudo nano /etc/php/7.0/fpm/pool.d/www.conf

Then change the highlighted settings below.

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www-data
group = www-data

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = /run/php/php7.0-fpm.sock

listen = 127.0.0.1:9000

; Set listen(2) backlog.
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
;listen.backlog = 511

Step 3: Configure Apache2 Default Site

Now that PHP-FPM is installed and configured, open Apache2 default site by running the commands below

sudo nano /etc/apache2/sites-enabled/000-default.conf

Then add the highlighted lines between VirtualHost block and save the file.


<VirtualHost *:80>
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

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

# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noe

Save the file and continue below

Finally, enable proxy_fcfi by running the commands below

sudo a2enmod proxy_fcgi

After that, restart Apache2 and PHP-FPM to load the settings.

sudo systemctl restart apache2.service
sudo systemctl restart php-fpm.service

Test if PHP is loaded by running the commands below.

sudo echo '<?php phpinfo(); ?>' > /var/www/html/info.php

Then browse to the server hostname or IP address followed by /info.php.

ex:

php-fpm on apache2

Step 4: Enable PHP-FPM on Nginx

For Nginx, run the commands below to install PHP-FPM. By default PHP-FPM is setup to communicate over unix socket so no changes there. Nginx will handle the communications over unix socket.

sudo apt-get install php-fpm

Then open Nginx default site configuration file by running the commands below:

sudo nano /etc/nginx/sites-available/default

When the file opens, add the highlighted line to the php block and save.

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
}
###

Save your settings.

Restart Nginx and PHP-FPM by running the commands below

sudo systemctl restart php7.0-fpm nginx

That’s it!

Test PHP by running the commands below.

sudo echo '<?php phpinfo(); ?>' > /var/www/html/info.php

You may also like the post below: