Jun 24, 2023 5 min read

How to Install PHP on Ubuntu 22.04

Install PHP on Ubuntu 22.04 with our step-by-step tutorial. PHP is a popular server-side scripting language used for web development.

Install PHP On Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

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

PHP is a popular server-side scripting language used for web development. It stands for "Hypertext Preprocessor" and is widely known for its simplicity and versatility. PHP enables the creation of dynamic and interactive websites by embedding code within HTML.

With its extensive library of functions and compatibility with various databases, PHP allows developers to build robust web applications efficiently. Whether you're a beginner or an experienced programmer, PHP offers a user-friendly environment to craft feature-rich websites.

In this tutorial, you will learn how to install PHP On Ubuntu 22.04. We will also address a few FAQs on how to install PHP on Ubuntu 22.04.

Advantages of PHP

  1. Versatility: PHP is compatible with various platforms and databases, making it flexible for web development.
  2. Easy to Learn: Its straightforward syntax and vast community support make PHP a beginner-friendly language.
  3. Rapid Development: PHP's extensive library of functions and frameworks allows for faster development of web applications.
  4. Cost-effective: Being an open-source language, PHP eliminates the need for expensive software licenses.
  5. Large Community: The vast PHP community provides extensive resources, tutorials, and support for developers worldwide.

Prerequisites to Install PHP on Ubuntu 22.04

  • Ubuntu 20.04 or 22.04 installed on a Linux system.
  • Access to an Ubuntu user account with sudo privileges.
  • Access to a terminal window or command line Ctrl+Alt+T.
  • An operating web server (Apache or Nginx).

Installing PHP on Ubuntu

PHP has a number of release versions, just like many developer tools. The most popular and currently supported software versions at the time of writing are PHP 7.4 and 8.1.

The apt package manager is used to install PHP using the command below:

sudo apt install php

The command installs PHP 7.4 on Ubuntu 20.04 and PHP 8.1 on Ubuntu 22.04.

Use one of the installation procedures below to select a specific version of PHP on either system.

1. Install PHP with Apache on Ubuntu

Install PHP with the Apache module if you are using an Apache web server. Installations for both PHP 7.4 and PHP 8.1 can be found below.

01. Verify that your repository is up-to-date by typing the following command into a terminal window:

sudo apt update && sudo apt upgrade -y

02. Install software-properties-common to assist you in managing distributions and independent sources:

sudo apt install software-properties-common

Go on to the following step if the package is already available.

03. After that, add the ondrej/php PPA, which offers many PHP versions for Ubuntu:

sudo add-apt-repository ppa:ondrej/php

Press Enter to confirm adding the repository after the PPA has loaded. The PHP versions that are accessible in the PPA range from 5.6 to 8.2.

04. Modify apt to add the new packages:

sudo apt update

05. You can now set up specific PHP versions. For instance:

  • Execute the following command to install PHP 7.4:
sudo apt -y install php7.4
  • Execute the following command to install PHP 8.1:
sudo apt -y install php8.1

Note: The PHP module for Apache 2 is automatically installed by the command that installs PHP. Use the following command instead if it fails to accomplish that:

sudo apt -y install libapache2-mod-php<version>;

06. Check the installation with:

php -v

The version of PHP that you installed is shown in the output.

The output reads as follows if PHP 8.1 was installed.

2. Install PHP with Nginx on Ubuntu

PHP processing is not native to the Nginx server. Follow the instructions below to install PHP 7.4 or 8.1 to work with Nginx if you are using the Nginx server instead of Apache.

01. Run the following command in a terminal window to make sure you are running the most recent software available:

sudo apt update && sudo apt upgrade -y

02. Include the ondrej/php PPA to add various PHP versions:

sudo add-apt-repository ppa:ondrej/php

Click Enter to continue once the PPA information loads.

03. Use any one of the below commands to install PHP for Nginx:

  • For PHP 7.4:
sudo apt install php7.4-fpm -y
  • For PHP 8.1:
sudo apt install php8.1-fpm -y

The software and its dependencies will be downloaded and installed by the system.

04. Restart the Nginx service when the installation is complete to apply the modifications by typing:

sudo systemctl restart nginx

05. Next, by modifying the server block, enable PHP support. The following command will open the server block:

sudo nano /etc/nginx/sites-available/default

06. To use PHP with Nginx, add the following code to your server's block file:

server{
  # . . . existing configuration
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php<version>-fpm.sock;
  }
}

Replace the <version> with the PHP version, you are using.

07. Save the file, then close it.

08. Lastly, reboot Nginx on Ubuntu and reload PHP:

sudo systemctl restart nginx
sudo systemctl reload php<version>-fpm

Installing PHP Modules on Ubuntu

Use the following syntax to add more PHP modules to your installation:

sudo apt install php<version>-<package_name>

For example:

sudo apt install php8.1-posix

Alternatively, install several modules at once. For instance, in order to install the mysql, zip, and bcmath modules in PHP 7.4, you would run:

sudo apt install php7.4-{mysql,zip,bcmath}

Execute the following command to list all loaded PHP modules:

php -m

How to Uninstall PHP on Ubuntu

Follow the steps below to uninstall PHP from Ubuntu.

01. In your terminal, execute the below command:

sudo apt-get purge php<version>

As an example, run the following command if you installed the PHP 7.4 FPM version:

sudo apt-get purge php7.4-fpm

02. You can delete the orphaned packages:

sudo apt-get autoremove

03. Finally, verify the PHP version to confirm the uninstallation:

php -v

If the output does not return the version, the uninstallation was successful and everything is removed correctly.

FAQs to Install PHP On Ubuntu 22.04

Which PHP version is available for Ubuntu 22.04?

The default PHP version available for Ubuntu 22.04 is PHP 7.4. You can check the installed version by running the command php -vin the terminal.

Can I install a different PHP version on Ubuntu 22.04?

Yes, you can install a different PHP version on Ubuntu 22.04 by using third-party repositories or compiling from source. However, it's recommended to use the version provided by the official Ubuntu repositories.

Are all PHP extensions included in the default installation?

No, the default PHP installation on Ubuntu 22.04 includes only the essential extensions. You can install additional extensions using the command sudo apt install php-extension-name.

Do I need to restart Apache after installing PHP?

Yes, after installing PHP, it's recommended to restart Apache for the changes to take effect. You can do this by running the command sudo systemctl restart apache2.

How can I test if PHP is working on Ubuntu 22.04?

You can create a PHP test file (e.g., info.php) in the Apache web root directory (/var/www/html) with the content <?php phpinfo(); ?>. Accessing this file in a web browser should display PHP information if it's working correctly.

Can I install PHP alongside other web servers like Nginx?

Yes, you can install PHP alongside other web servers like Nginx on Ubuntu 22.04. The process may vary slightly depending on the server, but you'll need to install the PHP-FPM package and configure it accordingly.

How can I update PHP to a newer version on Ubuntu 22.04?

To update PHP to a newer version on Ubuntu 22.04, you can use the package manager apt to update all system packages, including PHP, by running sudo apt update followed by sudo apt upgrade.

Conclusion

Hope this detailed tutorial helped you understand how to install PHP on Ubuntu 22.04.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - 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.