How To Install PHP 8.1 on Ubuntu 20.04

In this article, we want to teach you How To Install PHP 8.1 on Ubuntu 20.04.

PHP is a server-side and general-purpose scripting language that is especially suited for web development.

It originally stood for Personal Home Page. However, now, it stands for Hypertext Preprocessor. It’s a recursive acronym because the first word itself is also an acronym.

PHP was created by Rasmus Lerdorf in 1994. It’s currently maintained by the PHP Development Team.

How To Install PHP 8.1 on Ubuntu 20.04

Before you start to install PHP 8.1, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 20.04.

Now you can follow the steps below to complete this guide.

Import Ondřej Surý PHP PPA on Ubuntu 20.04

To successfully install PHP 8.1, you will need to import the good renowned PPA from Ondřej Surý, the lead developer on PHP and Debian, and maintain Ubuntu and Debian packages.

To install Ondřej Surý PPA and the required packages on Ubuntu 20.04, run the following command:

sudo apt install software-properties-common && sudo add-apt-repository ppa:ondrej/php -y

Then, update  and upgrade your local package index with the command below:

sudo apt update && sudo apt upgrade

Set up PHP 8.1 on Ubuntu 20.04

At this point, you can install PHP 8.1 with an Apache option and Nginx.

Install PHP 8.1 with Apache

If you run an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.

To install PHP 8 as an Apache module, enter the following command on Ubuntu 20.04:

sudo apt install php8.1 libapache2-mod-php8.1

When your installation is completed, restart Apache for the new PHP module to be loaded:

sudo systemctl restart apache2

Also, you can install Apache with PHP-FPM. To do this, run the following command:

sudo apt install php8.1-fpm libapache2-mod-fcgid

Note: PHP-FPM is not enabled for Apache. You must enable it by the following command:

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.1-fpm

Restart Apache to apply the changes:

sudo systemctl restart apache2

Verify that your PHP-FPM service is active and running on your server:

sudo systemctl status php8.1-fpm
Output
php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor pr>
Active: active (running) since Thu 2022-04-21 13:36:15 CEST; 44s ago
Docs: man:php-fpm8.1(8)
Main PID: 61963 (php-fpm8.1)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
Tasks: 3 (limit: 2282)
Memory: 7.3M
CGroup: /system.slice/php8.1-fpm.service
├─61963 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
├─61970 php-fpm: pool www
└─61971 php-fpm: pool www

You can verify your PHP installation on Ubuntu 20.04, with the command below:

php --version
Output
PHP 8.1.4 (cli) (built: Apr 4 2022 13:30:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.4, Copyright (c) Zend Technologies
with Zend OPcache v8.1.4, Copyright (c), by Zend Technologies

Install PHP 8.1 with Nginx

Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM “fastCGI process manager” to handle the PHP files.

Install PHP 8.1 and PHP 8.1-FPM on Ubuntu 20.04 with the following command:

sudo apt install php8.1 php8.1-fpm php8.1-cli -y

Verify your PHP-FPM service is active and running on your server with the command below:

sudo systemctl status php8.1-fpm
Output
php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor pr>
Active: active (running) since Thu 2022-04-21 13:36:15 CEST; 44s ago
Docs: man:php-fpm8.1(8)
Main PID: 61963 (php-fpm8.1)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
Tasks: 3 (limit: 2282)
Memory: 7.3M
CGroup: /system.slice/php8.1-fpm.service
├─61963 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
├─61970 php-fpm: pool www
└─61971 php-fpm: pool www

Also, you need to edit your Nginx server block and add the example below for Nginx to process the PHP files on Ubuntu 20.04.

server {
# … some other code
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following command:

sudo nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
nginx: configuration file /etc/nginx/nginx.conf test is successful

Then, restart Nginx to apply the changes:

sudo systemctl restart nginx

Also, you can verify your PHP 8.1 installation on Ubuntu 20.04:

php --version

Conclusion

At this point, you learn to Install PHP 8.1 on Ubuntu 20.04.

Hope you enjoy it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular