How to Configure and Use Nginx with PHP8.4-FPM on Ubuntu 24.04


Nginx is a lightweight and very popular HTTP web server that is widely used to serve web requests. Its asynchronous design makes it one of the best choices for powering high traffic websites.

PHP-FPM is a FastCGI Process Manager (FPM) implementation for the PHP programming language. Like Nginx, it is able to process multiple requests simultaneously, boosting its performance.

In this post, we will look at how we can configure Nginx so that it will be able to pass PHP scripts to PHP-FPM for processing. If you already have Nginx, PHP, and PHP-FPM installed, you can skip to the section on configuring Nginx to use php8.4-fpm.

Getting Started

Before getting started, it is expected that you have a Ubuntu system with user privileges to install software packages. Basic knowledge of Linux commands is essential to get along.

To complete the objective of this guide, we will need Nginx, PHP, and PHP-FPM software packages. However, prior installation of these packages is not required. Separate sections have been dedicated for the installation of these packages.

If you already have Nginx, PHP, and PHP-FPM installed, you can ignore the immediate discussions and skip to the section on configuring Nginx to use PHP-FPM. However, if you are missing any of these software packages, you can click on a link from the list below to jump to the appropriate section and install the missing package.

The installation of the required software packages, discussed in the next sections, is based on the order of the package installation sections listed above. Let’s get started.

Step 1: Update Ubuntu System

Before we install any software package, it is important to update the list of available software packages and their versions. This ensures that we have the latest list of available software packages.

Type and execute the following command to update the list of available software packages on the Ubuntu system:

Bash
sudo apt update

Optionally, we can execute the following command to upgrade installed software packages:

Bash
sudo apt upgrade

Step 2: Install Nginx

To install Nginx on Ubuntu system, type and execute the following command:

Bash
sudo apt install nginx

The web server listens on port 80 for HTTP requests and port 443 for HTTPS requests. We will need to adjust the firewall so that the web server can receive web requests through these ports.

To allow the web server to receive web requests through HTTP port 80, type and execute the following command:

Bash
sudo ufw allow 80/tcp;

To allow the web server to receive web requests through HTTPS port 443, type and execute the following command:

Bash
sudo ufw allow 443/tcp

If the ufw firewall isn’t active, we will need to activate it with the following command:

Bash
sudo ufw enable

We can check to verify that the Nginx web server is active and running. The following command will output the status of the web server:

Bash
sudo systemctl status nginx

A sample output from the above command is shown in the following image:

nginx-status

For an extensive coverage of Nginx installation and its configuration on Ubuntu, see the discussion on how to install and configure Nginx on Ubuntu 24.04.

Step 3: Install PHP

By default, Ubuntu 24.04 ships with PHP 8.3. However, we will install the latest version of PHP. The latest PHP release, at the time of writing, is PHP 8.4. To install it, we will add a repository that has PHP 8.4 available before performing the installation.

If you already have an earlier release of PHP installed, such as PHP 8.3, you can still follow along with the instructions in this section. However, you will need to replace occurrences of PHP version number 8.4 with the version number of PHP that you have installed on your Ubuntu system.

PHP 8.4 is available in Ondrej Sury’s repository. We will add this repository so that we will be able to install it on Ubuntu. To do this, type and execute the following command:

Bash
sudo add-apt-repository ppa:ondrej/php

After adding the repository, we will need to update the list of available software packages on the Ubuntu system:

Bash
sudo apt update

We are now ready to install PHP 8.4 on our Ubuntu system. Type and execute the following command to install it:

Bash
sudo apt install php8.4

Observe from the above command that we explicitly specified the version number of PHP that we intend to install. If we omit the version number, the command will install the default PHP version that ships with the Ubuntu system.

For example, since Ubuntu 24.04 ships with PHP 8.3, the following command, without explicit PHP version number, will install PHP 8.3 rather than PHP 8.4:

Bash
sudo apt install php

Since this guide uses PHP 8.4 for the examples, and this PHP version isn’t the default in Ubuntu 24.04, we had to explicitly specify the PHP version number in the php 8.4 installation command.

If you notice that the installation of PHP 8.4 also installed Apache web server and you do not need it, you can remove Apache with the following command:

Bash
sudo apt remove apache2*

With installation of PHP completed, we can run the following command to check its version:

Bash
php -v

A sample output is shown in the following image:

Step 4: Install PHP-FPM

PHP-FPM is a FastCGI Process Manager (FPM) for PHP processing. It is designed to handle multiple requests simultaneously leading to improved performance, especially on high traffic websites.

The installation of PHP does not automatically install PHP-FPM. To use it, we will need to explicitly install it on our Ubuntu system. It is provided as an extension to core PHP.

To install php8.4-fpm, type and execute the following command:

Bash
sudo apt install php8.4-fpm

Recall that we installed PHP 8.4, which isn’t the default PHP that ships with Ubuntu 24.04. Therefore, in the installation command above, we had to explicitly specify the version of PHP for which we are installing PHP-FPM.

PHP-FPM runs as a service. We need to ensure that it is always started on system boot. To do this, we will execute the following command to enable php8.4-fpm service:

Bash
sudo systemctl enable php8.4-fpm

We can run the following command to check its status:

Bash
sudo systemctl status php8.4-fpm

Step 5: Configure Nginx for PHP-FPM

Configuring Nginx to use PHP-FPM is a very simple task that can be completed in just some few minutes. In fact, Nginx has default directives for running PHP-FPM. However, these directives are, by default, commented out and therefore inactive. One thing we can do is to uncomment the lines that we need. That sounds good, isn’t it?

To see the default Nginx directives for running PHP-FPM, we can use a text editor to view the lines. Using nano text editor, type and execute the following command:

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

Scroll through the content until you see the section containing the following text:

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

As can be seen, all lines have been commented out. Rather than uncomment the lines we need in this section of the file, we can think of a better approach.

Suppose we have a server running multiple websites, with each website having its own server block configuration. If we need to use php8.4-fpm in all these websites, then we will have to type the directives above in each website’s server block configuration. That will lead to code duplication, isn’t it? I am sure you have already figured out a solution.

Rather than type the directives directly in the server block configuration, we can write them in a single file and re-use the directives in multiple server block configuration files.

Let’s create the file that will contain the Nginx directives for using PHP-FPM. You can choose any name you like for this file. However, it will be appropriate to have the version number of PHP as part of the file name for easy identification.

In this guide, let’s assume the name php8_4.handler for the file that will contain the directives. The .handler extension is not mandatory. That’s our own making. We will save the file in Nginx root directory: /etc/nginx/.

From the command interface, type and execute the following command:

Bash
sudo nano /etc/nginx/php8_4.handler

Type or paste the following code into the text editor.

Nginx
# pass PHP scripts to FastCGI server
location ~ \.php$ {
    include snippets/fastcgi-php.conf;

	  # With php-fpm (or other unix sockets):
	  fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}

If you are using a different version of PHP, then you will need to update the directive on line 6 to use the version of PHP-FPM that you have installed. Save and exit the editor.

In some Nginx configurations using PHP-FPM, you may encounter other directives that may include the following lines:

Nginx
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;

If you have seen these directives elsewhere, you may be wondering why we did not include them in our configuration. Well, we actually did with the following directive in our configuration:

Nginx
include snippets/fastcgi-php.conf;

With the inclusion of fastcgi-php.conf, we also include those other directives seen earlier.

Since we saved the configuration in a separate file, we can include it in multiple server block configuration files. For practical example, let’s include it in the default Nginx server block configuration so that we can perform a test by running a PHP script from the web browser.

Type and execute the following command to open the web server’s default server block configuration:

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

Scroll down until you see the section on FastCGI directives that have been commented out. We opened and scrolled to this section earlier. Just above the section, type the following line to include the php8_4.handler file that we created:

Nginx
include php8_4.handler;

The following code shows additional lines so that you can see clearly where we have included the php8_4.handler file.

Nginx
include php8_4.handler

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

Save the file and exit the editor.

Step 6: Test Nginx Configuration

It is always a good idea to test for errors after editing web server configuration files. Type and execute the following command to test the web server configuration:

Bash
sudo nginx -t

If there is no configuration error, the web server will report with a positive feedback. A sample output is shown below:

We should reload the web server configuration for the changes to take effect:

Bash
sudo systemctl reload nginx

Our web server configuration with PHP-FPM is complete. We can make a web request from the web browser to process a PHP script.

Step 7: Test PHP from Web Browser

To test and confirm that the web server can pass PHP scripts to PHP-FPM for processing, we can create a PHP script that will output some content. In this example, we will call the phpinfo() function to give us information about the installed PHP version in use. Remember to delete this file from the directory when done.

From the command interface, type and execute the following command:

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

Type or paste the following PHP code in the text editor:

PHP
<?php
    phpinfo();

Save the file and close the editor.

If you have an IP address assigned to your Ubuntu system, then type the following in your browser’s address bar, replacing IP_ADDRESS with your server’s IP address:

http://IP_ADDRESS/info.php

If you do not have an IP address assigned to your Ubuntu system, you can use localhost in place of the IP address. In this case, type the following in the browser’s address bar:

http://localhost/info.php

You should now see some information about the version of PHP that has been configured to process PHP scripts on your Ubuntu system. A sample view is shown below:

In the above image, the value of Server API indicates that we are using PHP-FPM, shown in the marked region as FPM/FastCGI.

Wrapping Up

PHP-FPM is a FastCGI process manager that can handle multiple PHP requests with minimal resources. Combined with Nginx web server, the pair is a choice for high traffic internet websites.

In this guide, we have demonstrated how to configure Nginx web server so that it can pass PHP scripts to PHP-FPM for processing.


Leave a Reply

Your email address will not be published. Required fields are marked *