How To Install PHP 7.4 on Ubuntu 20.04 with Easy Steps

Posted on

How To Install PHP 7.4 on Ubuntu 20.04 with Easy Steps

How To Install PHP 7.4 on Ubuntu 20.04 with Easy Steps

This guide provides a step-by-step walkthrough on How To Install PHP 7.4 on Ubuntu 20.04. PHP, which stands for PHP: Hypertext Preprocessor, is a widely used server-side scripting language specifically designed for web development. Its open-source nature means it’s free to use and distribute, making it a popular choice among developers. PHP is known for its ease of use and relatively simple syntax. PHP files typically have the ".php" extension.

The following guide, originally presented on the Orcacore website, details the process of setting up How To Install PHP 7.4 on Ubuntu 20.04.

Important Note: It is crucial to understand that PHP 7.4 has reached its end-of-life and is no longer actively supported. Similarly, Ubuntu 20.04 will reach the end of its standard support in April 2025. It is strongly recommended to consider using more recent, actively supported PHP versions and upgrading your Ubuntu installation to 22.04 LTS or a newer release for security and performance reasons. However, if you have legacy applications requiring PHP 7.4, this guide remains relevant.

Before proceeding, ensure you are logged into your Ubuntu 20.04 server as a non-root user with sudo privileges. If you haven’t already, you can refer to a guide on initial server setup with Ubuntu 20.04 for assistance.

Let’s proceed with the steps to How To Install PHP 7.4 on Ubuntu 20.04.

1. Install Required Packages for PHP 7.4

First, update and upgrade your local package index to ensure you have the latest package information:

sudo apt update && sudo apt upgrade -y

Next, install the necessary packages using the following command:

sudo apt install software-properties-common apt-transport-https -y

Now, add the Ondřej Surý PHP repository to your Ubuntu 20.04 system. This repository is a valuable resource for packaging various software, including PHP versions, for Debian and Ubuntu distributions.

To add the Surý PHP repository, execute the following command:

sudo add-apt-repository ppa:ondrej/php -y

After adding the repository, update and upgrade your APT repository list once again:

sudo apt update && sudo apt upgrade -y

With the repository added and updated, we can now proceed to install PHP 7.4 with options for both Apache and Nginx web servers.

2. Set up PHP 7.4 on Ubuntu 20.04

Depending on whether you are using the Apache HTTP server or Nginx, the installation process differs slightly.

Installing PHP 7.4 with Apache module

If you are running Apache, you can configure PHP to run as either an Apache module or using PHP-FPM (FastCGI Process Manager).

To install PHP 7.4 as an Apache module, execute the following command:

sudo apt install php7.4 libapache2-mod-php7.4 -y

Once the installation is complete, restart Apache to apply the changes:

sudo systemctl restart apache2

To install PHP-FPM, use the following command:

sudo apt install php7.4-fpm libapache2-mod-fcgid

Note: By default, PHP-FPM is not enabled for Apache. You must enable it using the following commands:

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php7.4-fpm

Then, restart Apache again:

sudo systemctl restart apache2

Verify that the PHP-FPM service is active and running on your server:

sudo systemctl status php7.4-fpm
Sample Output:
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-10-23 10:00:00 UTC; 10s ago
       Docs: man:php-fpm7.4(8)
    Process: 12345 ExecStart=/usr/sbin/php-fpm7.4 --nodaemonize --fpm-config /etc/php/7.4/fpm/php-fpm.conf (code=exited, status=0/SUCCESS)
   Main PID: 67890 (php-fpm7.4)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 1144)
     Memory: 10.2M
     CGroup: /system.slice/php7.4-fpm.service
             └─67890 php-fpm7.4: master process (/etc/php/7.4/fpm/php-fpm.conf)

You can also verify that PHP 7.4 is installed on your Ubuntu 20.04 system by checking its version:

php --version
Sample Output:
PHP 7.4.33 (cli) (built: Oct 13 2023 14:23:01) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c) Zend Technologies

Installing PHP 7.4 with Nginx Module

Nginx, unlike Apache, does not have native PHP processing capabilities. Therefore, you must install PHP-FPM to handle PHP files.

To install PHP 7.4 and PHP 7.4-FPM, run the following command:

sudo apt install php7.4 php7.4-fpm php7.4-cli -y

PHP-FPM should start automatically upon completion of the installation. To confirm that it is active and running, execute the following command:

sudo systemctl status php7.4-fpm
Sample Output:
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-10-23 10:05:00 UTC; 5s ago
       Docs: man:php-fpm7.4(8)
    Process: 23456 ExecStart=/usr/sbin/php-fpm7.4 --nodaemonize --fpm-config /etc/php/7.4/fpm/php-fpm.conf (code=exited, status=0/SUCCESS)
   Main PID: 78901 (php-fpm7.4)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 1144)
     Memory: 11.1M
     CGroup: /system.slice/php7.4-fpm.service
             └─78901 php-fpm7.4: master process (/etc/php/7.4/fpm/php-fpm.conf)

Next, you need to configure your Nginx server block to properly process PHP files. Add the following example to your Nginx server block configuration file. This configuration tells Nginx to pass PHP file requests to PHP-FPM.

server {
 location ~ .php$ {
   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php7.4-fpm.sock;
 }
}

Verify that your Nginx configuration has no syntax errors:

sudo nginx -t
Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Finally, verify that PHP 7.4 is installed by checking its version:

php --version
Sample Output:
PHP 7.4.33 (cli) (built: Oct 13 2023 14:23:01) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c) Zend Technologies

That concludes the process of installing PHP 7.4 on Ubuntu 20.04 with Nginx.

Conclusion

How To Install PHP 7.4 on Ubuntu 20.04 using either Apache or Nginx provides the foundation for running PHP-based applications. While this setup might be necessary for maintaining legacy projects, it’s strongly advised to upgrade to a newer, supported PHP version for enhanced security and performance. Furthermore, upgrading Ubuntu itself is highly recommended.


Alternative Solutions

While the method described above is a standard approach, here are two alternative ways to install PHP (focusing on a more recent version due to the end-of-life of PHP 7.4, but the principles apply):

1. Using Docker

Docker provides a containerization solution that simplifies application deployment and management. You can use Docker to run PHP applications without directly installing PHP on your host system. This approach offers several advantages, including environment isolation and reproducibility.

Explanation:

Docker allows you to create isolated environments called containers. A Dockerfile defines the instructions for building a Docker image, which is a snapshot of the container’s file system. You can use a pre-built PHP Docker image from Docker Hub or create your own. This eliminates the need to modify the host operating system and avoids potential conflicts with other software.

Example:

Create a Dockerfile in your project directory:

FROM php:8.2-apache  # Or php:8.2-fpm if you prefer FPM with Nginx

WORKDIR /var/www/html

COPY . .  # Copy your PHP application files into the container

# Optionally, install additional PHP extensions
RUN docker-php-ext-install pdo pdo_mysql mysqli

# Expose port 80 for Apache (or 9000 for FPM)
EXPOSE 80

Build the Docker image:

docker build -t my-php-app .

Run the Docker container:

docker run -d -p 80:80 my-php-app

Now your PHP application is running inside a Docker container, accessible on port 80 of your host machine.

2. Using a Package Manager with Specific PHP Version Support

While apt generally installs the default PHP version available in the repository, you can use package managers like apt (with specific repository configurations) or asdf (a version manager) to install a specific PHP version more easily.

Explanation:

This approach involves adding a repository to your system that specifically caters to multiple PHP versions or using a version manager. For apt, this is similar to the original guide but focuses on newer PHP versions available in the Ondřej Surý repository. For asdf, you install the tool, configure it to manage PHP versions, and then install the desired PHP version.

Example (using asdf):

  1. Install asdf: Follow the instructions on the asdf website (https://asdf-vm.com/) to install it on your system.

  2. Add the PHP plugin:

    asdf plugin add php
  3. Install a specific PHP version:

    asdf install php 8.2
  4. Set the PHP version (locally or globally):

    asdf local php 8.2   # For the current directory
    asdf global php 8.2  # System-wide
  5. Verify the PHP version:

    php --version

This ensures you’re using the specified PHP version managed by asdf, without directly interfering with the system’s default PHP installation (if any). This approach is particularly useful for managing multiple PHP versions on the same system for different projects.

Leave a Reply

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