Websites are expanding every day and reaching a larger audience. So far, more than 125000000 domains have been registered on the Internet, which indicates the rapid expansion of the Internet. As the website audience grew, HTML no longer met the needs of webmasters and needed a programming language to act like Desktop software and be easily scalable. Languages such as PHP, Asp, Java and etc were created to serve websites. In this article, we are going to teach you how to install and configure PHP on Ubuntu 22.04.
Table of Contents
What is PHP?
PHP is the most popular programming language for designing dynamic websites and is a free and open-source language. This scripting language can be integrated with HTML and its syntax is similar to C and Perl language. PHP is a server-side language, meaning that PHP code is interpreted on the server and generates HTML output or another output that is visible to the user. PHP was written in the C programming language by Rasmus Lerdorf in 1994 for use in monitoring online resumes and related personal information.
- Easy to learn
- High service speed compared to other similar programming languages such as asp
- Optimal use of resources and memory
- Ability to access multiple free resources
- Specialized web programming language
- Run-on different platforms
- Ability to run on Linux VPS or Linux Host that are cheaper and have more features
- Speed and ease of troubleshooting
How to Install PHP on Ubuntu 22.04
Before doing anything, login to the Ubuntu 22.04 via console.
At first, you should update the packages of the system by entering the following command:
sudo apt update && sudo apt upgrade
Then you will be asked to press Y to confirm the installation.
Before installing PHP, you need to install the required dependencies. To do this run the following command:
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
Ondrej PPA is a PHP and Debian developer and maintains all versions of PHP packages for Ubuntu systems. You can add Ondrej PPA to your system with the following command:
sudo add-apt-repository ppa:ondrej/php
Next, update the system again by entering the following command:
sudo apt update
The SURY repository has PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 and 5.6. You can use the following commands to install any of the required PHP versions.
Install PHP 8.1:
sudo apt install php8.1
Install PHP 7.4:
sudo apt install php7.4
Install PHP 5.6:
sudo apt install php5.6
If an application needs other PHP extensions, you can add the following syntax:
sudo apt install php8.1-[extension]
Note that you should replace your desired extension instead of [extension]. If you want to add multiple extensions, you can do like the following:
sudo apt install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl
How to Configure PHP on Ubuntu 22.04
You can verify the PHP installation by entering the following command:
The output should be as follows:
PHP configuration files are stored in the /etc/php directory under a directory with the version number. Main PHP configuration files are located as follows:
PHP CLI: /etc/php/version/cli/php.ini
PHP FPM: /etc/php/version/fpm/php.ini
PHP modules are stored in the /etc/php/version/mods-available directory. PHP active modules configuration directory are located as follows:
PHP CLI: /etc/php/version/cli/conf.d/
PHP FPM: /etc/php/version/fpm/conf.d/
If you want to check files for any versions of PHP, just put the PHP version number instead of the version in the path of the files and directory.
You can list all PHP extensions by running the following command:
To set the default version of PHP, run the following command. You can read more about switching the PHP version for CLI and Apache.
sudo update-alternatives --config php
You will have 4 choices for the alternative PHP, which are:
There are 4 choices for the alternative php (providing /usr/bin/php)
Press to keep the current choice[*], or type selection number: 2
The output you receive should show all versions of PHP installed on your system. PHP 7.4 is set as the default version of PHP by Selection number 2 for the command line.
How to Uninstall PHP on Ubuntu 22.04
You can remove any PHP version from your system by entering the following command:
sudo apt remove php-version
This frees up disk space and system security.
To uninstall all the modules of the desired version by running the following command:
sudo apt remove php-version-*
Remember to replace your desired version with -version in command.
In this article, we taught you how to install and configure PHP on ubuntu 22.04. Also how to uninstall PHP was explained. We use Ondrej PPA which allows you to install multiple PHP versions on a single system and also install PHP quickly. If you have any questions, you can contact us in the Comments.