How to Install PHP 8.x on Ubuntu 22.04

How to Install PHP 8.x on Ubuntu 22.04

Install and configure PHP 8.x on ubuntu 22.04; Through this tutorial, we will learn how to install and configure PHP 8.x on ubuntu 22.04 system.

PHP 8.1 is the current latest PHP version released on 2021. In this guide we are going to learn how to install the latest PHP version which is currently 8.1 on your Ubuntu 22.04 system or server and configure it with Apache. By default Ubuntu 22.04 ships with PHP 8.1, you can install it easily.

How to Install PHP 8.x on Ubuntu 22.04

Use the following steps to install and configure PHP 8.x on ubuntu 22.04 system:

Step 1 – Update System Dependencies
Step 2 – Add PHP PPA
Step 3 – Install PHP 8.x for Apache
Step 4 – Install PHP 8.x Extensions
Step 5 – Verify PHP Version
Step 6 – Configure PHP 8.x

Step 1 – Update System Dependencies

Open command prompt and execute the following command on command prompt to update latest packages or dependencies on ubuntu 22.04 system:

sudo apt update
sudo apt upgrade

Step 2 – Add PHP PPA

This is an optional setup if you wish to install PHP 8.1. But you are about to install any lower versions of PHP versions like 7.4 you need to add this PPA.

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

Only add this if we are about to install a PHP version other than version 8.1.

Step 3 – Install PHP 8.x for Apache

Execute the following command on command prompt to install PHP 8.1:

sudo apt install php8.1

Once the installation has been completed, we can confirm the installation using the following command:

php -v

Step 4 – Install PHP 8.x Extensions

Using the following command, we can install PHP extensions:

sudo apt install php8.1-extension_name

Now, install some commonly used php-extensions with the following command.

sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-redis php8.1-intl -y

Step 5 – Verify PHP Version

Using the following command, we can easily verify PHP version:

php -v

Output
PHP 8.1.5 (cli) (built: Apr 21 2022 10:32:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies

Step 6 – Configure PHP 8.x

Once the php installation has been completed, Now we configure PHP for Web Applications by modifying some values in php.ini file.

So, execute the following command on command prompt to edit php.ini:

sudo nano /etc/php/8.1/apache2/php.ini

Hit F6 for 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

Then execute the following command on command prompt to restart apache web server:

sudo service apache2 restart

Conclusion

Through this tutorial, we have learned how to install PHP 8.1 on your Ubuntu 22.04 server for Apache.

Leave a Reply

Your email address will not be published. Required fields are marked *