How To Install PHP On Ubuntu 20.04 or 22.04

How To Install PHP On Ubuntu 20.04 or 22.04. In this post, we  introduce PHP , its advantages. After that we show you how to install different PHP versions on Ubuntu 20.04 or 22.04.

What is PHP?

Today, many organizations prefer building their websites and server side applications using PHP. Well, PHP is one of the popular open source scripting languages used specifically for web development and modifying several elements within a database. Also, an HTML embedded server side language that runs on the server to produce dynamic online content. Developers even use the open-source scripting language for creating web applications, managing dynamic websites, and customer relationship management systems.

Being an open source language, it is available for free use and is modified to meet the needs of the user. Earlier, PHP stood for Personal Homepage, but now it is referred to as Hypertext Pre-processor. Over time, it got transformed into a scripting language that made it easier for developers to create dynamic web pages and applications.

It’s been around 26 years since PHP’s initial release. Its latest version 8 was published in November 2020, but version 7 is still the most popular. The most widely used version of PHP is the Zend engine. There are a few additional versions as well, including Hip Hop, HPVM, and Parrot.

The best part about PHP is before a document or website is sent to the user’s browser, this serverside language is run on the server. As a result, dynamic web pages that change depending on user input or database content are made using PHP. The PHP framework makes it easier to reuse existing code and eliminates the need to create lengthy, intricate code for web apps

Features of PHP

  • ServerSide Language: Before sending the webpage to the user’s browser, PHP is run on the server, i.e., the user can view only the HTML output.
  • Scalability:  PHP has the ability to handle multiple requests at once. If you planning to design a website like an e-commerce store that may receive a lot of traffic, you must choose PHP as an ideal choice.
  • Object Oriented Programming: Another key feature of PHP is it supports object oriented programming (OOP). Using this language, developers generate reusable codes and develop complex applications without any hassle.
  • Large Library: Developers make proper use of supported libraries of functions to perform multiple tasks without any hassle. Additionally, developers even take help from these libraries to write code quickly and efficiently.
  • Dynamically Typed Programming Language: PHP are dynamically typed programming language, i.e., the data type or variable is determined only during the runtime. As a result, the variables are modified, and code is written without having to declare data types. Also, if any error exists in the data type, it may not be caught until runtime. Using this feature, developers build applications faster and more efficiently.

Advantages of Using PHP

  • Free and Open Source Scripting Language: Since PHP is a free and open source scripting language anyone use it. With PHP, web developers no longer need to invest in or purchase expensive software as it is easily helps to create dynamic websites. It is readily accessible for use with events or web applications and is downloaded from any location.
  • Fast and Efficient: Applications built with PHP and connected to a database are loaded quickly with this powerful language. All in all, PHP is fast and loads more quickly than other computer languages do on slow internet connections as well as manages multiple requests at once.
  • Compatibility with Operating systems: Another benefit of PHP is it works well with Windows, Linux, macOS, and other popular operating systems. This feature makes it a versatile language that is used on any device.
  • Large Community Support: PHP has a large community for developers to locate solutions to issues and obtain assistance when required. It also has a wide range of libraries available that care used for various function modules for data representation and creating web applications. These built-in libraries help save time as well as the efforts of developers.
  • Easy to Learn: PHP is easy to learn and has a simple syntax that makes it easy for programmers to create code. Basically, anyone who is acquainted with C programming works on PHP with ease.

We have arrived to the main part of this article blog How To Install PHP On Ubuntu 20.04 or 22.04. 

How To Install PHP On Ubuntu 20.04 or 22.04

This section explores how to install PHP on Ubuntu. We also explain how to enable PHP support in Apache and Nginx web servers.


  • A server running Ubuntu 20.04 or 22.04
  • A root user or a user with sudo privileges.

Install Required Dependencies

Before starting, update all the system packages to the updated version by running the following command.

					apt update -y
apt upgrade -y

Once all the system packages are updated, you need to install some required packages on your server. Well, install all of them by running the following command.

					apt install software-properties-common ca-certificates lsb-release apt-transport-https gnupg2 curl unzip -y

After installing all the dependencies, proceed to install PHP.

Install PHP on Ubuntu

By default, the latest version of PHP is included in the Ubuntu default repository. However, if you want to install multiple PHP versions then you will need to add the PHP Ondrej repository to your server. You install the PHP repository with the following command.

					add-apt-repository ppa:ondrej/php

Next, update the repository cache using the following command.

					apt update -y

Now, use the following syntax to install your required PHP versions on your server.

					apt install php(versions-number)

For example, if you want to install the PHP 8.2 version run the following command.

					apt install php8.2 -y

Next, verify the installed PHP version with the following command.

					php --version


You see the PHP version in the following screen.

If you want to install PHP 8.1, run the following command.

					apt install php8.1 -y


To install PHP 7.4 version, run the following command.

					apt install php7.4 -y


If you want to install an older PHP version 5.6, run the following command.

					apt install php5.6 -y


Install PHP Extensions

After installing PHP, you also need to install various PHP extensions to extend their features. The basic syntax to install the PHP extension is shown below.

apt install php8.2-(extension)


For example, if you want to install MySQL, Curl, GD, and XML extensions, run the following command.

					apt install php8.2-mysql php8.2-curl php8.2-gd php8.2-mysql php8.2-xml -y

Replace 8.2 with your desired PHP version, if you want to install the extension for a specific PHP version.

PHP Configuration Files

By default, all PHP configuration files are stored in the /etc/php directory. Here is the list of all main configuration files for the PHP 8.2 version.

  • PHP Apache: /etc/php/8.2/apache2/php.ini
  • PHP-FPM: /etc/php/8.2/fpm/php.ini
  • PHP CLI: /etc/php/8.2/cli/php.ini

All PHP module’s configuration directory locations are shown below:

  • PHP Apache: /etc/php/8.2/apache2/conf.d/
  • PHP-FPM: /etc/php/8.2/fpm/conf.d/
  • PHP CLI: /etc/php/8.2/cli/conf.d/

Change Default PHP Versions

If you are using multiple PHP versions on the same server. Then, you easily switch between your desired PHP version using the update alternatives command.

Run the following command to set the default PHP versions:

					update-alternatives --config php

You should see a list of all installed PHP versions on the following screen.

Type your desired PHP version number from the above list and press the Enter key. Next, verify your new PHP version using the following command.

					php --version

See your default PHP version on the following screen.

Enable PHP Support in Apache and Nginx Web Server

To enable the PHP support in the Apache web server, you need to install the PHP extension for Apache. Simply  install it with the following command.

					apt install libapache2-mod-php8.2 -y

If you want to enable PHP support in the Nginx web server, you need to install the PHP-FPM package on your server.

					apt install php8.2-fpm -y

After installing the PHP-FPM package, also edit your Nginx virtual host configuration file and define your PHP-FPM socket file.

Edit the Nginx main configuration file.

					nano /etc/nginx/sites-available/default

Add the following lines between your existing configuration:

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


Save and close the file then restart the Nginx and PHP-FPM service to apply the configuration changes.

					systemctl restart nginx php8.2-fpm

Remove or Uninstall PHP

In addition, use the apt command to uninstall any PHP version from your system. For example, to uninstall PHP 8.2 version, run the following command.

					apt remove php8.2 -y

After removing PHP uninstall all PHP extensions with the following command.

					apt remove php8.2-*

Once all your PHP extensions are removed, remove all unwanted packages and cache with the following command.

					apt autoremove
apt clean

Thank you for reading How To Install PHP On Ubuntu 20.04 or 22.04. We shall conclude this article now. 

How To Install PHP On Ubuntu 20.04 or 22.04 Conclusion

In this guide, we learned how to install PHP on Ubuntu 20.04 or 22.04. We also learned how to switch between multiple PHP versions and use it with Apache and Nginx web servers. We hope you now easily host your PHP application on the server.

Finally, many web applications demand a strong programming language that comes with a reliable database management system; PHP is one of them. Lastly, PHP is the fastest Programming language that is easy to use and makes the connection securely with databases.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x