How to add PHP-FPM support for NGINX sites

If you're building NGINX sites, you'll probably need PHP-FPM support enabled. Jack Wallen shows you how.

nginxhero.jpg

Image: NGINX

If you've decided to make the switch from Apache to NGINX, one of the questions you might find yourself asking is how to add support for the PHP Fast Process Manager (PHP-FPM). For those that might not know, PHP-FPM is a FastCGI handler for PHP scripts and apps, which makes it possible for a website to handle higher loads. PHP-FPM is much faster than traditional CGI-based multi-user PHP environments and also allows for the hosting of multiple applications using different versions of PHP.

I want to walk you through the process of installing PHP-FPM and then enabling it in your NGINX sites. It's not quite as simple as it is with Apache, but it shouldn't be much of a challenge for any IT pro.

SEE: Linux service control commands (TechRepublic Premium)

What you'll need

The only things you'll need for this are a running instance of NGINX and a user with sudo privileges. I'll be demonstrating on my server of choice, Ubuntu 20.04, but this process can be modified to work on any platform that supports PHP and PHP-FPM.

How to install PHP-FPM

The first thing we must do is install PHP-FPM, and a few extras. Log in to your Ubuntu Server instance and, from the terminal window, issue the command:

sudo apt install php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath -y

The above command will download and install all of the necessary dependencies.

Once PHP-FPM is installed, you need to then find out which version of PHP is installed. Do that with the command:

php --version

You can then check on the status of PHP-FPM with the command:

sudo systemctl status php7.X-fpm

Where X is the version number of PHP.

If you see that PHP-FPM isn't running (it should be), start it with the command:

sudo systemctl start php7.X-fpm

Where X is the version number of PHP.

Finally, enable PHP-FPM with the command:

sudo systemctl enable php7.x-fpm

Again, where X is the version number of PHP.

How to configure PHP-FPM

Now we need to make a few configuration changes, so NGINX knows about PHP-FPM. The first is to edit the default NGINX configuration file. Open that file with the command:

sudo nano /etc/nginx/nginx.conf

In that file, look for the line:

#server_tokens off;

Uncomment that line so it looks like:

server_tokens off;

Save and close the file. 

Next, we need to edit the default virtualhost configuration. Open that file with the command:

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

In that file, look for the section under:

# pass PHP scripts to FastCGI server

That section needs to look like:

location ~ \.php$ {

                include snippets/fastcgi-php.conf;

        #

        #       # With php-fpm (or other unix sockets):

                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

        #       # With php-cgi (or other tcp sockets):

        #       fastcgi_pass 127.0.0.1:9000;

        }

In other words, uncomment (remove the # character) the lines:

  • location ~ \.php$ {
  • include snippets/fastcgi-php.conf;
  • fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  • }

Save and close the file.

Restart NGINX with the command:

sudo systemctl restart nginx

How to test the installation

Create a test file with the command:

sudo nano /var/www/html/info.php

In that file, paste the following contents:

<?php
phpinfo();
?>

Save and close the file.

Open a web browser and point it to http://SERVER/info.php (where SERVER is the IP address of the hosting server). You should see, near the top of the PHP information page, FPM/FastCGI listed for the Server API and, under cgi-fcgi, that php-fpm is active (Figure A).

Figure A

nginxphpa.jpg

PHP-FPM is installed and active for NGINX.

And that's it, you've got NGINX up and running with PHP-FPM support. Remember, when you build your virtualhost configuration files, you'll need to make sure to include PHP support in those. For that, you can use the /etc/nginx/sites-available/default file as an example.

Congratulations, enjoy that PHP/NGINX goodness.

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see