Introduction
PHP is a widely-used server-side scripting language designed specifically for web development. To run PHP-based web applications on an Ubuntu system, you need to ensure that the appropriate PHP packages are installed and properly configured.
Installing PHP on Ubuntu 22.04 doesn’t have to be complicated. In this guide, I’ll show you a simple and clear step-by-step process to get PHP up and running quickly.
We will use the Ondrej PPA for installing PHP on Ubuntu 22.04 LTS system. Which contains PHP 8.3, 8.2, 8.1, 8.0, 7.4, 7.3, 7.2. 7.1, 7.0 & PHP 5.6 packages. You can install any of the versions as your requirement. The new application developers are suggested to use the latest PHP version PHP 8.1.
In this tutorial, you will learn how to install PHP on Ubuntu 22.04 LTS system. It is compatible with Ubuntu 20.04, and 18.04 systems too.
Installation Steps
First of all, open a terminal and update the current packages with the following commands:
sudo apt update && sudo apt upgrade
Step2: Installing Multiple PHP Versions
Install a few dependencies required by this tutorial with the below-mentioned command:
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https -y
Ubuntu’s default repositories may not have all PHP versions. So, we will add the ondrej/php
repository which contains all versions of PHP packages for the Ubuntu systems.
sudo add-apt-repository ppa:ondrej/php
Now, update the Apt package manager cache:
sudo apt update
The SURY repository contains PHP 8.3, 8.2, 8.1, 8.0, 7.4, 7.3, 7.2. 7.1, 7.0 & PHP 5.6. You can install any of the required PHP versions on your system. I am installing all PHP versions for a demo purpose.
sudo apt install php8.3
sudo apt install php8.2
sudo apt install php8.1
sudo apt install php8.0
sudo apt install php7.4
sudo apt install php7.3
sudo apt install php7.2
sudo apt install php7.1
sudo apt install php5.6
Step3: Installing PHP Extensions
Install extensions for PHP8.3
sudo apt install php8.3-mysql php8.3-mbstring php8.3-mcrypt php8.3-dom php8.3-bcmath php8.3-intl php8.3-soap php8.3-zip php8.3-gd php8.3-curl php8.3-cli php8.3-xml php8.3-xmlrpc php8.3-gmp php8.3-common -y
Install extensions for PHP8.2
sudo apt install php8.2-mysql php8.2-mbstring php8.2-mcrypt php8.2-dom php8.2-bcmath php8.2-intl php8.2-soap php8.2-zip php8.2-gd php8.2-curl php8.2-cli php8.2-xml php8.2-xmlrpc php8.2-gmp php8.2-common -y
Install extensions for PHP8.1
sudo apt-get install php8.1-dom php8.1-xml php8.1-bcmath php8.1-curl php8.1-gd php8.1-intl php8.1-mbstring php8.1-mcrypt php8.1-zip php8.1-soap php8.1-mysql php8.1-common -y
Install extensions for PHP7.4
sudo apt install php7.4-bcmath php7.4-common php7.4-json php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl php7.4-gettext -y
I have installed PHP7.4 to 8.3 PHP extensions, but you can install extensions as per your requirement.
Step4: Check Active PHP Version
After successful installation, verify that the correct version of PHP is installed by checking the version number by the below-mentioned command:
php -v
Step5: Configure PHP Settings and Reload Apache
1. Configure php settings
php -i | grep "Configuration File"
2. you will get the path to the php.ini file from this command. Open this file to modify some settings
sudo nano <path_of_php.ini_file>
3. In the php.ini file, search and change the following values below
max_execution_time=18000
max_input_time=1800
memory_limit=4G
4. Save the PHP configuration file and reload Apache2.
sudo systemctl reload apache2
Conclusion
You have successfully installed and configured multiple PHP versions on Ubuntu 22.04. This setup allows you to handle multiple projects with varying PHP requirements efficiently. Whether you’re working with legacy applications or modern frameworks, managing PHP versions is now easier than ever.
Stay tuned for more tutorials on managing web servers and optimizing performance!
Thank you for reading this article!!!