How To Install PHP On Ubuntu 20.04 or 22.04
PHP is a programming language that is used to create web-based applications. It is essential to install the latest PHP on Ubuntu 20.04 to run the software written on it. In general, PHP is used to build e-commerce sites, blogs, API, and other applications. If you’re looking for a simple method on how to install PHP on Ubuntu and beyond, don’t look any further. The following guide will teach you how to install it quickly and effortlessly.
Installing PHP on Ubuntu
As with many other developer tools, PHP has several different release versions. As of the date of writing this article, PHP 7.4. 7.4 and 8.1 are currently supported and are the most popular versions of the software. When you buy a Linux server, it is crucial to install PHP 8 on Ubuntu 22 or any other version that you are using.
This command will install PHP using the apt program manager:
Sudo apt install php
On Ubuntu hosting 20.04, the option installs PHP 7.4, and Ubuntu 22.04 runs PHP Version 8.1.
If you’re trying to select the version you want to use PHP for either of the systems, you can follow the steps below to installing PHP 8 on Ubuntu 20 or any other version.
1. Install PHP using Apache on Ubuntu
If you’re using the Apache Web server, then install PHP using its Apache module. Below are the installation options for the two versions: PHP 7.4 as well as PHP 8.1.
- Verify that you are running the most recent repository updates by typing the following command in the terminal window:
Sudo apt update && sudo apt upgrade
- Install software-properties-common to help you manage distributions and independent software sources:
Sudo apt install software-properties-common
If your package is available, proceed.
- After that, you should include ondrej/php PPA. This package offers several PHP versions for Ubuntu:
Sudo add-apt-repository ppa:ondrej/php
Once the PPA is loaded, hit enter to confirm that you have added the repository. There are a variety of PHP versions that are available in the PPA vary, starting at 5.6 through 8.2.
- Update to be able to include new packages:
Sudo apt update
- You can now install specific PHP versions. For instance:
- For installation of PHP 7.4, start the following command:
Sudo apt -y install php7.4
- For installing PHP 8 on Ubuntu 20, execute these commands:
Sudo apt -y install php 8.1
- Verify the installation using the following:
php -v
2. Install PHP using Nginx on Ubuntu
The Nginx server doesn’t have native PHP processing capabilities. If you’re employing the Nginx server instead of Apache, then follow the instructions below to download PHP 7.4 as well as 8.1.
- Type in the following command in the terminal window to verify you’re using the most up-to-date software:
Sudo apt update && sudo apt upgrade
- Make sure to add to the Ondrej/php PPA to include a variety of PHP versions:
Sudo add-apt-repository ppa:ondrej/php
If the PPA information is loaded, click Enter to continue.
- For installing PHP for Nginx, Use any of these commands:
- For PHP 7.4:
Sudo apt install php7.4-fpm -y
- For PHP 8.1:
Sudo apt install php8.1-fpm -y
- When the installation is completed, you can start the Nginx service to apply the changes. In order to implement the changes, type in the following:
Sudo systemctl restart nginx
- Next, you must enable PHP assistance by editing your server block. Start the server block by typing the command:
Sudo nano /etc/nginx/sites-available/default
- Incorporate the following code into the block file on your server for Nginx to use PHP:
server{
# . . . existing configuration
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php<version>-fpm.sock;
}
}
- Save the file, then close the file.
- Then, restart Nginx using Ubuntu and load PHP again:
Sudo systemctl restart nginx
Sudo systemctl reload php<version_number>-fpm
Installing PHP Modules on Ubuntu
To install further PHP modules, you must use the syntax below:
Sudo apt install php<version>-<package_name>
For instance:
Sudo apt install php8.1-posix
If you prefer to install multiple modules at the same time. For instance, to install the components such as MySQL, zip and Bcmath in PHP 7.4 then, you’d use:
Sudo apt install php7.4-{mysql,zip,bcmath}
To display the loaded PHP modules, use the command:
php -m
How to Uninstall PHP on Ubuntu
To remove PHP from Ubuntu, Follow the steps in the following:
- Enter the following command into the terminal:
Sudo apt-get purge php<version>
For instance, if you have installed PHP 7.4 FPM, you can run the command below:
Sudo apt-get purge php7.4-fpm
- To remove the orphaned packages, use the following command:
Sudo apt-get autoremove
- Then, you should check your PHP version to ensure that the uninstall was successful:
php -v
If the output doesn’t display the version, then the uninstall was not able to remove everything properly.
Conclusion
If you’ve read this article, then you know how to install PHP on Ubuntu hosting. In this article, we discussed how to install the latest PHP on Ubuntu 20.04 and other versions using Apache and Nginx in the Ubuntu 20.04 as well as 22.04 system.
The most important thing to keep in mind is that PHP operates in conjunction with an existing server, which means you must install PHP specifically on the server software. You can also download a stable version of PHP from one of the Ubuntu software repositories.