Install PHP 8.2 on Ubuntu 20.04 with Efficient Steps

In this tutorial on the Orcacore website, we want to teach you How To Install PHP 8.2 on Ubuntu 20.04. PHP is an open-source server-side scripting language that many devs use for web development. It is also a general-purpose language that you can use to make lots of projects, including Graphical User Interfaces (GUIs).

PHP 8.2 brings type-system improvements for expressive and fine-grained type-safety, readonly classes, sensitive parameter redaction support, new random extension, and many new features along with several changes to streamline and modernize PHP language.

Steps To Install PHP 8.2 on Ubuntu 20.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 20.04.

In this guide, you will learn to install PHP 8.2 with Apache and Nginx Modules.

1. Import Ondřej Surý PHP PPA on Ubuntu 20.04

To successfully install PHP 8.2, 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 -y && 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

2. Set up PHP 8.2 on Ubuntu 20.04

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

Installing PHP 8.2 for Apache on Ubuntu 20.04

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

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

sudo apt install php8.2 libapache2-mod-php8.2 -y

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.2-fpm libapache2-mod-fcgid
Enable PHP-FPM for Apache on Ubuntu 20.04

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

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.2-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.2-fpm
Enable PHP-FPM for Apache on Ubuntu 20.04

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

php --version
PHP 8.2 with Apache Module on Ubuntu 20.04

Installing PHP 8.2 for Nginx on Ubuntu 20.04

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.2 and PHP 8.2-FPM on Ubuntu 20.04 with the following command:

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

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

sudo systemctl status php8.2-fpm
Installing PHP 8.2 for Nginx on Ubuntu 20.04

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.2-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.2 installation on Ubuntu 20.04:

php --version
Output
PHP 8.2.0 (cli) (built: Dec 10 2022 10:52:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.0, Copyright (c), by Zend Technologies

Conclusion

At this point, you learn to Install PHP 8.2 on Ubuntu 20.04 by using Apache and Nginx Modules. Hope you enjoy it. Please subscribe to us on Facebook, YouTube, and Twitter.

You may also like these articles:

How To Install Sysdig on Ubuntu 20.04

How To List Users and Groups in Linux

How To Find a spam script on cPanel

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!