Running PHP8.3 with Nginx on Ubuntu

  • avatar

PHP is a scripting language widely used for web development, with code processed on a web server through a PHP interpreter, implemented as a module, daemon, or CGI executable. Nginx lacks the default ability to handle dynamic web pages with PHP and requires specific additional plugins for this purpose. One such plugin is FPM (FastCGI Process Manager), an alternative PHP FastCGI implementation offering additional features, particularly advantageous for high-traffic sites. FPM stands out as the preferred method for PHP page processing with Nginx, surpassing traditional CGI-based methods in terms of speed.

Prerequisites

You will need a working Nginx setup. If you do not already have one, take a look to our Nginx installation and configuration tutorial to get your server ready.

Uninstalling old PHP versions

If you have installed another version of PHP we recommend to uninstall it executing the command:

sudo apt-get purge php8.*

The example command shows how to uninstall PHP8 and related packages. Adjust the version number to the one you have installed on your computer.

Installing PHP8.3 FPM

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. Add the Ondrej PPA with the command below:

sudo add-apt-repository ppa:ondrej/php

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

Then, update Ubuntu system packages list and install dependencies as shown:

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

Now, with the PPA included, you can install PHP 8.3 along with its necessary dependencies.:

sudo apt-get install php8.3-fpm

Finally, to check the correct installation ask for the version of PHP:

php -v

The output will be similar to the following image and show the installed version on your machine.

PHP version

Installing PHP8.3 extensions

To list all loaded extensions run the following command:

php --modules

The command to install a PHP 8.3 extension is:

sudo apt install php8.3-<extension>

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

PHP 8.3 extensions

You can install frequently used extensions using the command provided below:

apt-get install -y php8.3-common php8.3-fpm php8.3-mysql php8.3-redis php8.3-mongodb php8.3-zip php8.3-gd php8.3-mbstring php8.3-cli php8.3-curl php8.3-xml php8.3-bcmath

Enabling PHP8.3 on Nginx server

Now that you have PHP-FPM installed, you will need to enable it updating Nginx configuration file:

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

Note: we are using nano editor but feel free to use your preferred one.

Following lines, as shown, enable PHP-FPM on your Nginx server.

NGINX with PHP 8.4 FPM configuration

Usually these directives are included but commented in the default configuration file. You will need to locate and uncomment them. Don't forget to add the index.php to the indexes list in the same file:

NGINX index configuration for PHP

Save everything and restart Nginx server to apply changes:

sudo systemctl restart nginx.service

We have successfully configured an Nginx server with PHP-FPM. Enjoy hosting your project 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

2 Comments

avatar

Anderson Torres Reply

thanks

avatar

Jan Bakalar Reply

Thank you, very useful to understand the differences in configuration vs apache2!

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.