Nginx with PHP8.1 FPM on Ubuntu 20.04

  • avatar
  • 9.8K Views
  • 15 Likes
  • 6 mins read

PHP is a general-purpose scripting language geared towards web development. PHP code is usually processed on a web server by a PHP interpreter implemented as a module, a daemon or as a Common Gateway Interface (CGI) executable.

By default Nginx is not capable of processing dynamic web pages with PHP so it requires the use of specific additional plugins for this purpose. One of these plugins is FPM (FastCGI Process Manager).  FPM is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites. It is the preferred method of processing PHP pages with Nginx and is faster than traditional CGI based methods.

Prerequisites

Before you begin, you will need a working Nginx setup. If you do not already have it, take a look to our Nginx installation and configuration tutorial.

Uninstalling old versions

If you already have installed a previous version of PHP we recommend to uninstall it executing the following command:

sudo apt-get purge php7.*

The example shows how to uninstall PHP7 and related packages, adjust it the version you have installed.

Installing PHP8.1 FPM

At the time of writing this tutorial PHP 7.4 is the default PHP version in Ubuntu 20.04 repositories. To install the latest version of PHP, we are going to use the Ondrej PPA repositories. This repository contains multiple PHP versions and PHP extensions.

Let’s update Ubuntu system packages and install some dependencies as shown:

sudo apt-get update && sudo apt-get install ca-certificates apt-transport-https software-properties-common

Next, add the Ondrej PPA:

sudo add-apt-repository ppa:ondrej/php

When prompted, press ENTER to proceed with adding the repository.

With the PPA enabled, you can install PHP 8.1:

sudo apt-get update && sudo apt-get install php8.1-fpm

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

Check the successful PHP installation with the command:

php -v

The output will show the installed version of PHP:

php8_version.png

Installing PHP8.1 extensions

To list all loaded extensions run the following command:

php --modules

The command to install PHP 8.1 extensions on Ubuntu is:

sudo apt install php8.1-<extension>

You just need to replace extension with the one you want to install, e.g., mysql, curl, gd, xml, etc... Extensions available by default are the listed below:

php8_extensions.png

Configuring Nginx

Now that you have PHP-FPM installed, you will need to update the Nginx configuration file (with nano or any other editor):

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

Uncomment or add the following lines for PHP-FPM to run on your server:

nginx_php8fpm.png

Don't forget to add the index.php to the indexes list:

nginx_php_index.png

Save the configuration file and restart Nginx server to apply the changes:

sudo systemctl restart nginx.service

We have successfully configured our Nginx server with PHP-FPM. Now we can host our website on it.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.