How to Install PHP 8.1 for Nginx On Ubuntu 20.04

How to Install PHP 8.1 for Nginx On Ubuntu 20.04

 

Introduction

PHP is arguably one of the most widely used server-side programming languages. It’s the language of choice when developing dynamic and responsive websites. Popular CMS platforms such as WordPress, Drupal, and Magento are based on PHP.
 
PHP 8.1 is a major update of the PHP language. It contains many new features, including enums, read-only properties, first-class callable syntax, fibers, intersection types, performance improvements, and more.
 
This article will show you how to install PHP 8.1 on Ubuntu 20.04 and integrate it with Nginx.

1   Add the Ondrej PPA Repository 

PHP 7.4 is the default PHP version in Ubuntu 20.04 repositories at the time of writing this tutorial. We will use the Ondrej PPA repositories to install the latest version of PHP. This repository contains multiple PHP versions and PHP extensions.
 
First, let’s update your Ubuntu system packages and install some dependencies as shown below.
$ sudo apt update

$ sudo apt upgrade

# Allow apt to use the repository via HTTPS 

$ sudo apt install ca-certificates apt-transport-https

$ sudo apt install 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.

2   Install PHP 8.1 with Nginx

If you'd like to use PHP 8.1 with Nginx installation, the most recommended step is to install PHP-FPM to process PHP files. You can install PHP and PHP-FPM using the following command:    
$ sudo apt install php8.1 php8.1-fpm
The PHP-FPM service should start automatically. You can verify this as shown:
$ sudo systemctl status php8.1-fpm
 
For Nginx to process PHP files, edit the default Nginx config file by using a text editor, like nano.
 
$ sudo nano /etc/nginx/sites-available/default
 
Configure your Nginx server block by updating the server section as shown:
server {

# ... some other code

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
 
Finally, restart the Nginx web server for the changes to take effect.
$ sudo systemctl restart nginx   

3   Install PHP 8.1 Extensions

PHP extensions are libraries that extend the functionality of PHP. These extensions exist as packages and can be installed as follows:
$ sudo apt install php8.1-[extension-name]
For instance, the example below installs the MySQL extensions.
$ sudo apt install php8.1-mysql

4   Configure PHP 8.1 (Optional)

Now we configure PHP for web applications by changing some values in the php.ini file. For PHP 8.1 FPM with Nginx, the php.ini location will be in the following directory.
sudo nano /etc/php/8.1/fpm/php.ini
Hit F6 to search inside the editor and update the following values for better performance. 
upload_max_filesize = 32M
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000
Once you have modified your PHP settings you need to restart your PHP-FPM for the changes to take effect. 
sudo service php8.1-fpm restart

 

5   Testing PHP Processing

To confirm the version of PHP installed, run the command:
$ php -v
 
Additionally, you can create a sample PHP file at /var/www/html as shown:
$ sudo vim /var/www/html/info.php
Paste the following lines and save the file.
<?php
phpinfo();
?>
 
Finally, go to your browser and browse the server’s IP address as shown.
http://server-ip/info.php
You should get the webpage shown.

6  Other operations

1.1 Install multiple versions of PHP

$ sudo apt install php7.4 php7.4-fpm

$ sudo apt install php8.0 php8.0-fpm

1.2 Check installed versions of PHP and set the default PHP version

sudo update-alternatives --config php
 
Now, we have installed multiple versions of PHP. And we can set one version as the default PHP version we'd like to use. Enter the number, and you have successfully changed the version of PHP.

1.3 Check the version of PHP

$ php -v

1.4 Uninstall PHP

To uninstall a PHP version, (for example, PHP 7.4) from your device, use the following command:
$ sudo apt remove --autoremove php7.4
After you issue the command above, PHP 7.4 will be deleted from your device. To remove the repository, use the following command:
$ sudo add-apt-repository --remove ppa:ondrej/php

Conclusion and further readings

We have tested this tutorial on VPS Mart, so it works fine on our Ubuntu VPS Hosting. We hope this tutorial helps regarding how to install PHP 8.1 and comfortably integrate it with the Nginx web server. Your feedback is most welcome.
 
Read More:

Add Feedback