Install PHP 8.3, 8.2, 8.1, 8.0 or 7.4 on Ubuntu 22.04 or 20.04

PHP is a cornerstone in web development, offering a scripting language that is versatile and widely supported. Installing PHP can enhance your web development capabilities if you’re running Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa.

Why Choose PHP on Ubuntu?

  • Versatility: PHP is suitable for everything from small websites to complex web applications, supported by extensive libraries and frameworks.
  • Ease of Integration: PHP works seamlessly with various databases and technologies, streamlining the development process.
  • Community Support: A robust global community backs PHP, offering resources, tutorials, and regular updates.
  • Scalability: PHP can easily adapt to growing user demands, making it ideal for scalable applications.

Comparative Advantages

  • User-Friendly: PHP is often easier to pick up and use, especially for those new to web development.
  • Speed: Known for its speed, PHP can offer performance advantages over other server-side languages.
  • Compatibility: PHP works well with various web servers and operating systems, including those commonly used on Ubuntu systems.

This guide will focus on how to install PHP 8.3, 8.2, 8.1, 8.0, or 7.4 on Ubuntu 22.04 or 20.04. We’ll use CLI commands and a trusted PPA by Ondřej Surý, a respected member of the Debian and Ubuntu communities. Whether you’re a seasoned developer or just getting started, this guide aims to simplify the PHP installation process on Ubuntu.

Section 1: Import the PHP PPA on Ubuntu 22.04 or 20.04

We’ll begin our journey by introducing Ondřej Surý’s PHP PPA into our system. This well-regarded PPA provides the most up-to-date PHP versions for Debian and Ubuntu distributions, allowing us to access PHP’s latest features and security updates.

Step 1: Refreshing Ubuntu System Packages Before PHP Installation

First and foremost, it’s good practice to ensure that all your Ubuntu system packages are updated. This not only secures your system but also aids in successfully installing new packages. Use the following commands to refresh your package list and upgrade any outdated packages:

sudo apt update
sudo apt upgrade

With the sudo apt update command, we ask the system to refresh its local package index. This index is a database of available packages from the repositories defined in the /etc/apt/sources.list file and in the /etc/apt/sources.list.d directory.

If there are updates available, the sudo apt upgrade command will handle the upgrade process. This ensures that your system is running the most recent versions of its software, enhancing security and stability.

Step 2: Install Initial Packages for PHP PPA on Ubuntu

Next, we’ll install additional packages to incorporate Ondřej’s PHP PPA into our system. These packages are essential for secure package downloading and management. Execute the following command to install these packages:

sudo apt install ca-certificates apt-transport-https software-properties-common lsb-release -y

The ca-certificates package provides the necessary certificates to verify secure websites, while apt-transport-https allows the Advanced Package Tool (APT) to retrieve packages over ‘https’ protocol. The software-properties-common package offers some practical utilities for managing the software repository sources, and lsb-release provides specific LSB (Linux Standard Base) information about your Linux distribution.

Step 3: Integrating the PHP Repository on Ubuntu

With the necessary packages, we’re ready to integrate Ondřej Surý’s PHP PPA into our system. We’ll use this PHP repository to install the latest PHP versions, often more recent than those available in Ubuntu’s default APT repository.

Let’s go ahead and import the repository with the following command:

sudo add-apt-repository ppa:ondrej/php -y

After adding the PPA, we must update the package cache to recognize this new source. This can be achieved by running the sudo apt update command again:

sudo apt update

Your system should be aware of the newly added PPA, and you’ll likely see some packages that need updating. To bring these packages up to date, execute the sudo apt upgrade command:

sudo apt upgrade

And that’s it! You’ve successfully integrated Ondřej Surý’s PHP PPA into your Ubuntu system, paving the way for installing the latest PHP versions. Next, the following sections will demonstrate how to install PHP with some known web applications.

Section 2: Install PHP 8.3, 8.2, 8.1, 8.0 or 7.4 on Ubuntu 22.04 or 20.04

We’re ready to install PHP since we’ve successfully integrated Ondřej Surý’s PHP PPA into our system. PHP can be used as an Apache module, with PHP-FPM when using an Apache HTTP server, or with Nginx. We’ll go through each of these scenarios in detail.

Option 1: Install PHP as an Apache Module on Ubuntu 22.04 or 20.04

PHP works as a module within the Apache HTTP server in this scenario. Only installing the PHP version you need is recommended to maintain a clean and manageable environment. Below are the commands to install PHP 8.3, 8.2, 8.1, 8.0, and 7.4 as Apache modules.

Note: You only need to install your desired version:

sudo apt install php8.3 libapache2-mod-php8.3
sudo apt install php8.2 libapache2-mod-php8.2
sudo apt install php8.1 libapache2-mod-php8.1
sudo apt install php8.0 libapache2-mod-php8.0
sudo apt install php7.4 libapache2-mod-php7.4

Following the installation, you’ll need to restart your Apache server so that it can load the newly installed PHP module:

sudo systemctl restart apache2

Option 2: Install Apache with PHP-FPM on Ubuntu on Ubuntu 22.04 or 20.04

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with additional features useful for sites of any size, particularly busier ones. Here are the commands to install PHP and PHP-FPM 8.3, 8.2, 8.1, 8.0, and 7.4 with Apache:

sudo apt install php8.3-fpm libapache2-mod-fcgid
sudo apt install php8.2-fpm libapache2-mod-fcgid
sudo apt install php8.1-fpm libapache2-mod-fcgid
sudo apt install php8.0-fpm libapache2-mod-fcgid
sudo apt install php7.4-fpm libapache2-mod-fcgid

However, PHP-FPM isn’t enabled for Apache by default, so you’ll need to enable it with the following command:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php{version}-fpm

Remember to replace {version} with your desired PHP version. For instance, if you’re using PHP 8.2, the command would be:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm

To effect these changes, restart the Apache web server:

sudo systemctl restart apache2

Option 3: Install PHP Support for Nginx on Ubuntu 22.04 or 20.04

Unlike other web servers like Apache, Nginx doesn’t have built-in support for processing PHP files. Therefore, you must install PHP-FPM (FastCGI Process Manager) to handle PHP files with Nginx. Here are the commands to install PHP 8.2, 8.1, 8.0, and 7.4 and PHP-FPM for Nginx support:

sudo apt install php8.3 php8.3-fpm php8.3-cli
sudo apt install php8.2 php8.2-fpm php8.2-cli
sudo apt install php8.1 php8.1-fpm php8.1-cli
sudo apt install php8.0 php8.0-fpm php8.0-cli
sudo apt install php7.4 php7.4-fpm php7.4-cli

After installation, the PHP-FPM service should automatically start running. In case it does not manually initiate the service with the following command:

sudo systemctl start php{version}-fpm

Remember to replace {version} with your desired PHP version.

Configuring Nginx Server Block for PHP-FPM on Ubuntu 22.04 or 20.04

To enable Nginx to process PHP files, it’s necessary to modify your Nginx server block configuration file. You’ll need to add the following segment to all server blocks that handle PHP files, specifically in the location block that matches the ~ .php$ pattern:

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

As before, ensure to replace {version} with your specific PHP version.

You can validate the syntax of your modified Nginx configuration using the following command:

sudo nginx -t

This command verifies the syntax of the configuration files and highlights any errors found. A successful output will look like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Once you’ve confirmed the syntax is correct, restart the Nginx service to apply the changes:

sudo systemctl restart nginx

As a final note, it’s always a good idea to check which version of PHP is currently installed on your system. To do this, use the following command:

php --version

This command displays your system’s current PHP version, allowing you to confirm that your installation or upgrade was successful.

Example output with PHP 8.2 installed with Ubuntu Linux:

Screenshot showing listed PHP versions on Ubuntu 22.04 and 20.04.Pin
Screenshot demonstrating how to list installed PHP versions on Ubuntu 22.04 or 20.04 Linux.

Section 3: Create a PHP Test File on Ubuntu 22.04 or 20.04

We’re now moving on to an important step: verifying the PHP installation. This can be achieved by creating and executing a simple PHP test file. Here’s how to do it in the Ubuntu Linux terminal.

Step 1: Creating the Test File

Initiate the process by creating a new file named test.php in your desired directory. This can be done with the touch command:

touch test.php

Step 2: Editing the Test File

Once the file has been created, adding some PHP coit is time. To do this, open the file in a text editor. For this illustration, we’re using nano, but feel free to substitute this with your preferred text editor, such as vim, gedit:

nano test.php

You will input a simple PHP code to display a message in the text editor. This will confirm if PHP is functioning correctly on your system. Here’s the code:

   echo "PHP is working!";

After entering the code, save the file by pressing CTRL + O. You can then exit the editor by pressing CTRL + X.

Step 3: Executing the Test File

Now, it’s time to see if PHP is working as expected. Run the test file using the following command:

php test.php

If PHP has been installed and configured correctly, the phrase “PHP is working!” will be displayed in your terminal. This verifies that your PHP installation is successful and ready to use. Remember, while we used nano in this example, other text editors can also be used based on your preference.

Section 4: Navigating PHP Alternative Versions on Ubuntu 22.04 or 20.04

In this section, we will explore how to switch between different versions of PHP installed on your Ubuntu system. This process is achieved by using the update-alternatives command in the terminal, which allows you to set a preferred PHP version for your system. This flexibility can be crucial if you work on projects requiring different PHP versions.

Step 1: Checking the Current PHP Version on Ubuntu 22.04 or 20.04

Before switching PHP versions, knowing which PHP version your system is currently utilizing is essential. Run this command to display the active PHP version:

php -v

Step 2: Listing Available PHP Versions on Ubuntu 22.04 or 20.04

To identify the PHP versions available for use on your system, execute the following command:

sudo update-alternatives --list php

Example output:

This command will provide a list of all installed PHP versions, offering a variety of choices to suit your requirements.

Step 3: Switching PHP Versions on Ubuntu 22.04 or 20.04

After determining the available PHP versions, you can set your preferred version using the update-alternatives command. Replace {version} with the PHP version you intend to switch to:

sudo update-alternatives --set php /usr/bin/php{version}

For instance, if you want to switch to PHP 7.4, the command should look like this:

sudo update-alternatives --set php /usr/bin/php7.4

Upon executing this command, your terminal will display a message similar to the following:

update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode

This output confirms that your system is now using PHP 7.4.

Step 4: Verify the PHP Version Switch on Ubuntu 22.04 or 20.04

After switching, verify that the active PHP version has indeed changed. Use the same command as before:

php -v

As per the earlier example, the output should now display PHP version 7.4.

If you’re using PHP-FPM, changes to the PHP version might necessitate adjustments to your Nginx server blocks. Knowing how to switch between different PHP versions is a powerful tool in your programming arsenal, allowing for greater adaptability to project-specific needs.

Concluding Thoughts

Installing PHP on Ubuntu is essential for any web developer or system administrator. By understanding these steps, you are empowered to create dynamic web applications or host powerful Content Management Systems (CMS) like WordPress.

In this guide, we’ve walked through the process of installing PHP and PHP-FPM, creating a simple PHP test file to verify the installation, and managing alternative versions of PHP for your diverse project needs. Each step has equipped you with practical knowledge to handle real-world scenarios effectively.

Share to...