PHPLinux VPSUbuntu

How to Installing PHP on Ubuntu: A Comprehensive Guide

PHP is a widely-used server-side scripting language that powers many websites and web applications. It’s essential to keep your PHP installation up to date to ensure security, performance, and compatibility with the latest web technologies. Ubuntu is a popular Linux distribution, and in this guide, we will walk you through the process of installing different PHP versions on an Ubuntu system. This comprehensive guide will cover multiple PHP versions, including PHP 7.4, PHP 8.1, and beyond.

Prerequisites

Before we start, make sure you have the following prerequisites:

  1. An Ubuntu server or desktop system (this guide is based on Ubuntu 20.04 LTS, but the process should be similar for other versions).
  2. Access to a terminal with administrative privileges (you can use the terminal emulator or SSH).

Let’s get started!

Step 1: Update Your System

It’s always a good practice to ensure your system is up to date before installing any software. Open your terminal and run the following commands:

sudo apt update

The sudo apt update command is used to refresh the package information on a Debian-based Linux system. It ensures that your system’s package manager (APT, which stands for Advanced Package Tool) has the latest information about available packages and their versions from the configured software repositories.

Then Upgrade:

sudo apt upgrade

The sudo apt upgrade command is used to upgrade the installed packages on a Debian-based Linux system, such as Ubuntu. This command will search for updates to your installed packages and, if any updates are available, it will prompt you to confirm the upgrade. It is a good practice to regularly run this command to keep your system up to date with the latest software updates and security patches.

Step 2: Add a PPA Repository

Before the repository add, we run the software-properties-common package is a useful tool in Ubuntu that provides a set of software management utilities, including the add-apt-repository command. This command is often required when you want to add external repositories, such as PPA (Personal Package Archive) repositories, to your system.

sudo apt install software-properties-common

And Then, To manage multiple PHP versions, we’ll use the Ondřej Surý PPA repository, which provides a range of PHP versions. First, you’ll need to add this repository:

sudo add-apt-repository ppa:ondrej/php

After adding the repository, update your package list:

sudo apt update

Step 3: Install PHP Version

Now that you have access to various PHP versions, you can install them as needed. To view the available PHP versions, use the following command:

apt search php | grep php8.

You’ll see a list of available PHP versions with names like php8.0, php7.4, etc.

You will need some knowledge of PHP to install the PHP version. In your case you may need different PHP versions. But from our previous post we get the idea that PHP vs PHP-FPM. If you haven’t seen my previous post, click here

We are installing PHP-FPM for our server but you can install only PHP variant if you want. To install a specific version, replace phpX.Y-fpm with your desired version in the following command:

For PHP-FPM:

sudo apt install phpX.Y-fpm

For only PHP:

sudo apt install phpX.Y

For example, to install PHP 8.1 FPM, you’d run:

sudo apt install php8.1-fpm

Or if you don’t want to install PHP-FMP version then follow this command:

sudo apt install php8.1

You can install as many PHP versions as needed for your projects.

Step 4: Install PHP Extensions

PHP extensions enhance the functionality of your PHP installation. To install extensions, you can use the apt package manager as well. Here’s an example of how to install the MySQL extension for PHP:

PHP extensions enhance the functionality of your PHP installation. To install extensions, you can use the apt package manager as well. To install a extensions, replace X.Y to your php version and [extension name] with your desired extension name in the following command:

sudo apt install phpX.Y-[extension name]

You can replace [extension name] with the name of the extension you need. Some common PHP extensions include:

  • php8.1-gd for GD graphics library
  • php8.1-curl for cURL support
  • php8.1-xml for XML support
  • php8.1-json for JSON support

You can install multiple extensions in one command by listing them with spaces. When you install a new PHP version, it doesn’t come with the same extensions as the default PHP version. You’ll need to install the extensions you require for your projects.

Commonly, the most useful extensions you need include common, curl, mbstring, opcache, readline, xml, zip, mysql, gd. You can install all these extensions using the following command:

sudo apt install php8.1-cli php8.1-common php8.1-curl php8.1-mbstring php8.1-opcache php8.1-readline php8.1-xml php8.1-zip php8.1-mysql php8.1-gd

Step 4: Switching Between PHP Versions

To switch between PHP versions, you can use the update-alternatives command. For instance, to switch to PHP 7.4, run:

sudo update-alternatives --set php /usr/bin/php7.4

To confirm the active PHP version, you can use the php -v command:

php -v

Step 6: Configuring Web Servers

If you’re running web applications, you’ll also need to configure your web server to use the desired PHP version. For Apache, you can enable PHP for a specific version with:

The command sudo a2dismod phpX.Y is used to disable a specific version of PHP (X.Y) as a module for the Apache web server.

sudo a2dismod phpX.Y
The command sudo a2enmod phpX.Y is used to enable a specific version of PHP (X.Y) as a module for the Apache web server
sudo a2enmod phpX.Y

Conclusion

You’ve successfully installed and managed multiple PHP versions on your Ubuntu system. This flexibility allows you to work on a variety of PHP projects with different version requirements and stay up-to-date with the latest PHP releases.

With this setup, you can easily switch between PHP versions, install the necessary extensions, and configure your web server to run web applications on your chosen PHP version. This ability to manage multiple PHP versions will be invaluable as you tackle different projects and adapt to evolving PHP technology in your development journey. Enjoy the convenience and flexibility that multiple PHP versions bring to your Ubuntu system!

Happy Coding 😉

IQBAL HASAN

I love to talk on trending ⚡ technology ⚡, I have huge attraction on 😁 Linux 😅 and I am a bit 😄 mad 😄 when I code.

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button