Linux VPSPHPUbuntu

How to Installing PHP 7.4 on Ubuntu: A Comprehensive Guide

In our previous post, “How to Installing PHP on Ubuntu: A Comprehensive Guide,” we covered the fundamentals of installing PHP on an Ubuntu system. If you haven’t had the chance to read that guide, we highly recommend you do so, as it provides essential background information on the installation process. Now, it’s time to dive into a specific version of PHP, PHP 7.4, which offers stability and performance improvements. This comprehensive guide will walk you through the step-by-step process of installing PHP 7.4 on your Ubuntu system.

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 7.4

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

To install PHP 7.4 FPM, you’d run:

sudo apt install php7.4-fpm

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

sudo apt install php7.4

This command will download and install PHP 7.4, along with any necessary dependencies. Confirm the installation by typing “Y” when prompted.

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:

  • php7.4-gd for GD graphics library
  • php7.4-curl for cURL support
  • php7.4-xml for XML support
  • php7.4-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,<code class="language-bash">libapache2 . You can install all these extensions using the following command:

sudo apt install php7.4-cli libapache2-mod-php7.4 php7.4-common php7.4-curl php7.4-mbstring php7.4-opcache php7.4-readline php7.4-xml php7.4-zip php7.4-mysql php7.4-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:

Disable your current PHP version and then enable PHP 7.4 for the Apache server.

Here are the corrected commands:

For example, if your current PHP version is PHP 7.4:

sudo a2dismod php8.1

And then enable PHP 7.4 for the Apache server:

sudo a2enmod php7.4
For Nginx, you’ll need to configure your Nginx virtual host files to use PHP 7.4.

Conclusion

In this guide, we’ve walked you through the process of installing PHP 7.4 on your Ubuntu system. PHP 7.4 offers stability and performance improvements, making it a popular choice for web development projects. By following these steps, you can have PHP 7.4 up and running on your Ubuntu machine, ready to power your web applications.

If you have any questions or encounter any issues during the installation process, please refer to the previous post for a more in-depth understanding of PHP installation on Ubuntu.

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

2 Comments

Leave a Reply

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

Back to top button