Jul 11, 2023 4 min read

How to Install PHP 8.2 on Ubuntu 22.04

Install PHP 8.2 on Ubuntu 22.04 with our step-by-step tutorial. PHP 8.2 is the latest version of the popular web development scripting language.

Install PHP 8.2 on Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install PHP 8.2 on Ubuntu 22.04, let's briefly understand - What is PHP 8.2?

PHP 8.2 is the latest version of the popular scripting language used for web development. Released in 2023, it brings exciting new features and improvements. PHP 8.2 enhances performance, security, and developer productivity. With JIT (Just-In-Time) compilation, code execution is faster, leading to quicker response times.

The introduction of native types allows for stricter type checking, making code more reliable. Additionally, PHP 8.2 introduces new functions and syntax enhancements, facilitating cleaner and more concise code. Upgrading to PHP 8.2 ensures a more efficient and robust web development experience.

In this tutorial, you will install PHP 8.2 and combine it with Nginx and Apache on Ubuntu 22.04. The default Ubuntu 22.04 repositories feature PHP 8.1 at the time of writing. We'll use the ondrej/php PPA repository to install PHP.

Advantages of PHP 8.2

  1. Improved Performance: PHP 8.2 introduces JIT compilation, resulting in faster code execution and quicker response times.
  2. Enhanced Security: With native types and stricter type checking, PHP 8.2 ensures more reliable and secure code.
  3. Increased Developer Productivity: New functions and syntax enhancements make code cleaner and more concise.
  4. Compatibility: PHP 8.2 maintains backward compatibility, allowing easy migration from previous versions.
  5. Community Support: Being a popular language, PHP benefits from a large and active community, providing resources and assistance.

Step 1 - Enable PHP Repository

1) Run the following command to enable the repository:

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

You can install PHP 8.2 after the PPA has been enabled.

Step 2 - Install PHP 8.2 with Apache

You can run PHP as an Apache module or PHP-FPM if you're using Apache as your web server.

Install PHP as Apache Module

The process of installing PHP as an Apache module is simple:

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

Restart Apache after installing the packages to allow the PHP module to load:

sudo systemctl restart apache2

Configure Apache with PHP-FPM

PHP's Php-FPM is a FastCGI process manager. To install the required packages, use the following command:

sudo apt update
sudo apt install php8.2-fpm libapache2-mod-fcgid

PHP-FPM is disabled by default in Apache. Run the following command to enable it:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm

Restart Apache to apply the changes:

sudo systemctl restart apache2

Install PHP with Nginx

Nginx doesn't come with built-in PHP processing capabilities. The PHP files will be handled by PHP-FPM ("fastCGI process manager").

To install PHP with PHP FPM, use the following commands:

sudo apt update
sudo apt install php8.2-fpm

The FPM service will start immediately after the installation is complete. Run this command to see if the service is up and running.

sudo systemctl status php8.2-fpm
Output
● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-12-03 16:10:47 UTC; 6s ago

To allow Nginx to process PHP files, update the Nginx server block and add the following lines:

server {

    # . . . other code

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

Remember to restart the Nginx service for the new settings to take effect:

sudo systemctl restart nginx

Step 3 - Install PHP Extensions

Extensions for PHP are compiled libraries that enhance PHP's fundamental capabilities. Extensions are distributed as packages and can be installed using apt:

sudo apt install php8.2-[extname]

Let's take an example, to install MySQL and GD extensions, use the following command:

sudo apt install php8.2-mysql php8.2-gd

Remember to restart Apache or PHP FPM after installing a new PHP extension, depending on your setup.

Test PHP Processing

Create a new file named info.php in the /var/www/html directory with the following code to see if the webserver is properly set for PHP processing:

<?php

phpinfo();

Save the file, then go to http://your_server_ip/info.php in your browser. You will be able to see the information about your PHP configuration.

FAQs to Install PHP 8.2 on Ubuntu 22.04

Which PHP packages are required for PHP 8.2 installation on Ubuntu 22.04?

You will need to install the php8.2 and php8.2-common packages, along with other necessary modules like php8.2-mysql or php8.2-gd, depending on your project requirements.

Can I upgrade my existing PHP version to 8.2 on Ubuntu 22.04?

Yes, if you have an older PHP version installed, you can upgrade it to PHP 8.2 by following the installation process and updating the necessary packages.

How do I check the installed PHP version after the installation?

You can use the php -v command in the terminal to check the installed PHP version and ensure it's 8.2.

Will installing PHP 8.2 on Ubuntu 22.04 affect my existing PHP configurations?

PHP 8.2 installation should not overwrite your existing configurations, but it's recommended to create a backup of your configuration files to be safe.

Are there any known issues or incompatibilities with PHP 8.2 on Ubuntu 22.04?

PHP 8.2 is generally stable, but some older PHP code may require adjustments due to backward-incompatible changes. Always test your application after the upgrade.

Can I use PHP 8.2 with popular web servers like Apache or Nginx on Ubuntu 22.04?

Yes, PHP 8.2 is compatible with web servers like Apache and Nginx on Ubuntu 22.04. You need to install the respective PHP module for your web server.

Does PHP 8.2 support all the extensions available for older PHP versions?

While most extensions are updated to support PHP 8.2, some third-party extensions may require updates for compatibility. Check the extensions' official documentation for details.

Is it recommended to use PHP 8.2 in a production environment on Ubuntu 22.04?

While PHP 8.2 offers great features and improvements, consider testing your applications thoroughly before deploying in production to ensure compatibility and stability.

Conclusion

We hope this tutorial helped you understand how to install PHP 8.2 on Ubuntu 22.04

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.