The PHP programming language interpreter can work in several modes. It can be integrated into a web server as a special module or used as a separate PHP-FPM service. FPM stands for FastCGI Process Manager. It is a service that starts several processes that can execute PHP scripts.
PHP-FPM can receive scripts to be executed using TCP or Unix sockets. This is the way Nginx scripts are executed. In this article, we will walk you through the installation of Nginx with PHP-FPM on Ubuntu.
How to Install Nginx on Ubuntu 20.04
There are two ways to install Nginx. The first way is to install a package from the official Ubuntu repository. At the time of this writing (August 1, 2021), the current version of Nginx present in the Ubuntu repository was version 1.18.0. This version is considered obsolete. The current version is 1.20.1 (as of August 1, 2021).
1. Official Ubuntu Repositories
If you want to install the version of Nginx from the Ubuntu repositories, you need to follow these steps. First, we update the package lists using the command:
sudo apt update
In order to install Nginx, just run the command:
sudo apt -y install nginx
Then the program can be used. Checking and configuring the program will be described in the sections below.
2. Official repositories of Nginx
The second way is to install the latest version of Nginx from the official repositories provided by the Nginx developers. If you want to use this installation method, first you need to update the package lists using the command:
sudo apt update
Install the required packages:
sudo apt -y install curl gnupg2 ca-certificates lsb-release
Then you have two options to choose from – connect the repository with the stable version of nginx or connect the repository with the main version. The stable version is more tested and workable. This version can be used both in test environments and in production. The main version is not as stable and may contain bugs. This version is not recommended for use in production environments.
To connect the repository with the stable version of nginx, run the following command:
To connect the repository with the main version of nginx, run the following command:
echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
The following steps must be performed regardless of the selected repository. Import the official key used by the package manager to authenticate packages:
curl -o /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key
Check if the correct key was loaded:
gpg --dry-run --quiet --import --import-options import-show /tmp/nginx_signing.key
The command output should contain the complete key fingerprint 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62:
Move the key to your apt trusted keys directory:
sudo mv /tmp/nginx_signing.key /etc/apt/trusted.gpg.d/nginx_signing.asc
To install nginx, run the following commands:
sudo apt update
sudo apt -y install nginx
The Nginx version from the developers is slightly different from the version from the official repositories. All additional configuration files are located here in the /etc/nginx/conf.d folder. If you want to use the sites-available and sites-enabled folders, you need to create them:
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled
Then add the following line to the end of the section http file /etc/nginx.conf in order to from the folder /etc/nginx/sites-enabled the site configuration was loaded:
sudo vi /etc/nginx/nginx.conf
Then restart Nginx:
sudo nginx -s reload
3. Launch Nginx
After installing the package, we check that Nginx has started successfully using the command:
sudo systemctl status nginx
If the status instead of active is inactive (dead), then the service must be started manually using the command:
sudo systemctl start nginx
Also note that you cannot run Apache and Nginx on the same port. In this case, you will receive an error nginx address already in use 80. For Nginx to work correctly, you will need to disable the Apache web server (if you have one) or change its port from 80 (which is the default) to another free port.
4. Configuring the firewall
By default, the firewall closes all unauthorized incoming connections. Therefore, in order to be able to access your web server from the outside, you need to add its port to the exceptions:
sudo ufw allow in 80/tcp
5. Checking Nginx operation
Once Nginx is running, it will be available at the address of the server it was installed on. You can check if everything is working by simply going to the server address by entering it in your browser. For the example, Nginx was installed on localhost:
If you see a welcome message like the screenshot above, it means that Nginx has been successfully installed and running.
Installing PHP-FPM on Ubuntu
The next step is to install the PHP programming language interpreter and all the necessary modules for working with PHP-FPM. To install all the necessary modules, run the command:
sudo apt -y install php7.4 php7.4-cli php7.4-fpm php7.4-json php7.4-pdo php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php-pear php7.4-bcmath
At the time of this writing (August 1, 2021), the current version of PHP in the official Ubuntu repositories was version 7.4. The most recent official version of PHP from the developers is 8.0.9 (as of July 29, 2021). After installing all the necessary packages, check the PHP-FPM status:
systemctl status php7.4-fpm.service
If the status instead of active will inactive (dead), then the service must be started manually using the command:
sudo systemctl start php7.4-fpm.service
After the service has started, the installation will be considered complete.
Connecting Nginx to PHP-FPM
To accept FastCGI requests from Nginx, PHP-FPM can listen on a TCP / IP socket or UNIX socket. UNIX sockets are an interprocess communication tool that enables efficient communication between processes running on the same operating system, while TCP / IP sockets allow processes to communicate over a network.
Unlike TCP / IP socket, which identifies a server by IP address and port (e.g. 127.0.0.1:9000), you can bind the server to a UNIX socket using a file path (e.g. /run/php-fpm/www.sock) that is visible on the file system.
A UNIX socket is a special type of file – file and directory permissions are applied to it (as is the case with any other UNIX file type), and it can be used to restrict which processes on the host can read and write to the file, ( and thus communicate with the back-end server).
Thus, the UNIX socket is secure because only processes on the local host can use it. The TCP / IP socket can be accessed from the Internet and this can pose a security risk unless additional security measures are taken, such as configuring a firewall.
Configuring PHP-FPM to Listen on a UNIX Socket
To configure PHP-FPM to listen on a UNIX socket, open the default PHP-FPM pool configuration file using your favorite text editor using the command:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
Then find the listen directive and set the UNIX socket file path for it as follows – listen = /run/php/php7.4-fpm.sock
If you are using a UNIX socket, you also need to set the appropriate read / write permissions on the file to allow connections from the NGINX web server. By default NGINX runs as user www-data в Ubuntu.
Find the options listen.owner and listen.group and assign them a value www-data… Also set the mode to 0660, for parameter listen.mode.
Configuring PHP-FPM for TCP / IP Socket Listening
Although a UNIX socket is faster than a TCP / IP socket, it is less scalable because it can only support interprocess communication on the same OS. If NGINX and the internal application server (PHP-FPM) are running on different systems, you will have to configure PHP-FPM to listen on TCP / IP sockets for a remote connection.
In the PHP-FPM pool config file, set the listening address, for example: 127.0.0.1:9000… Make sure the port you selected is not in use by another process or service on the same system.
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
Find the parameter listen and enter the address – 127.0.0.1:9000:
Save changes and close the file. Nginx php fpm installation is almost complete.
Configuring NGINX for PHP-FPM
Once you have configured the address that PHP-FPM listens to, you need to configure Nginx to request a proxy to it through this address using the config parameter fastcgi_pass, which is located in the configuration file of the virtual host block.
For example, let’s take the configuration file of the standard nginx site page that opens when nginx is first started, located in the following path – /etc/nginx/conf.d/default.conf, let’s open it for editing:
sudo nano /etc/nginx/conf.d/default.conf
If you have configured PHP-FPM to listen on a UNIX socket, find the location block to handle the .php files and set the following parameters for fastcgi:
If using a TCP / IP socket, replace the value in the parameter fastcgi_pass to the IP address and port of the server running PHP-FPM FastCGI:
After making changes to the Nginx configuration, check that the syntax is correct using the command:
sudo nginx -t
Next, you need to restart the services in order to apply the changes using the commands:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
After restarting the services, the connection is considered successful. To further configure PHP-FPM, use the article on Configuring PHP-FPM on Ubuntu 20.04 – Configuring PHP-FPM
Removing Nginx and PHP-FPM from Ubuntu
To completely remove Nginx and PHP-FPM from the system, just remove all packages that you installed earlier:
sudo apt -y purge nginx php7.4 php7.4-cli php7.4-fpm php7.4-json php7.4-pdo php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php-pear php7.4-bcmath
The purge command allows you to remove not only packages, but also their configuration files. If you want to keep the configuration files, use the remove command.
In this article, we have covered how to install Nginx PHP-FPM on Ubuntu 20.04. As you can see, it is not very difficult and even a beginner can cope with such a task. If you have any questions, leave them in the comments!