How to setup PHP-FPM for Apache or Nginx on Ubuntu Linux

|

,

|

The content is a tutorial on how to speed up PHP-based websites by installing and configuring PHP-FPM (FastCGI Process Manager) on Apache or Nginx web servers. It provides step-by-step instructions on how to install PHP-FPM, configure Apache or Nginx to use it, and test if PHP is loaded correctly. It assumes the user already has…

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. So, 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 Apache or Nginx web servers to use PHP-FPM. Most WordPress setups will have the PHP-FPM module installed and configured. There are not many on Apache, but enabling it 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 Apache / Nginx to route PHP requests through PHP-FPM to handle.

Install PHP-FPM and Apache2 Module

To get PHP-FPM installed and enabled on Apache, 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 Apache to use it, continue below.

Configure Apache2 to use PHP-FPM

After installing the module, open the default configuration file 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

Configure Apache2 Default Site

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

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

Then, add the highlighted lines between the 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: http://192.168.41.2/info.php

Enable PHP-FPM on Nginx

For Nginx, run the commands below to install PHP-FPM. PHP-FPM is set up by default to communicate over a Unix socket, so there are no changes. Nginx will handle the communications over the Unix socket.

sudo apt-get install php-fpm

Then open the 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:


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



4 responses to “How to setup PHP-FPM for Apache or Nginx on Ubuntu Linux”

  1. Patrick Avatar
    Patrick

    Hi !
    Thanks for this article.
    Just a typo for Apache part : You wrote two times sudo systemctl restart apache2 at the end.
    One of those should restart php-fpm

    1. !robot Avatar
      !robot

      Thanks, updated

  2. me Avatar
    me

    Doesn’t work/Is outdated

  3. Marco van Steen Avatar
    Marco van Steen

    In some places like php-fpm must be replaced with phpX.Y-fpm where you must replace X.Y with the php version that must be ran by the virtual host.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading