Install PHP 7.4 on Ubuntu 22.04 | Best Setup – OrcaCore

Posted on

Install PHP 7.4 on Ubuntu 22.04 | Best Setup – OrcaCore

This tutorial on the Orcacore website will guide you through How To Install PHP 7.4 on Ubuntu 22.04. PHP, a widely used scripting language, is a powerful option for web development. It interacts with the web server, processing client requests and generating HTML files. Beyond web development, PHP is a general-purpose language, allowing developers to create various applications.

PHP’s versatility extends to its compatibility with different operating systems, including macOS, Windows, and Linux. It also supports various web servers like OpenBSD, Nginx, and Apache. Furthermore, PHP is compatible with cloud environments such as Amazon AWS and Microsoft Azure.

PHP’s flexible structure allows it to handle various data formats like PNG, JPEG, GIF, and PDFs.

Essentially, PHP excels in the following application areas:

  • Server-side scripting: This is the primary and most traditional target field for PHP. You need three things to make this work: the PHP parser (CGI or server module), a web server, and a web browser. You need to run the web server, with a connected PHP installation. You can access the PHP program output with a web browser, viewing the PHP page through the server.
  • Command-line scripting: You can make it run without any server or browser. This type of usage is ideal for PHP scripting, which automates specific tasks. You can also use it for simple text processing tasks.

To follow this guide, you need to be logged into your server as a non-root user with sudo privileges. Refer to our guide on Initial Server Setup with Ubuntu 22.04 on Orcacore if you need assistance.

By default, Ubuntu 22.04 comes with PHP 8.1. If you want to use an older version like PHP 7.4, follow the steps below. This detailed guide will walk you through the process to Install PHP 7.4 on Ubuntu 22.04.

1. Install Dependencies for PHP 7.4

First, update and upgrade your local package index using the following command:

sudo apt update && sudo apt upgrade -y

Then, install the necessary dependencies for PHP 7.4 on your server with this command:

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

Now, add the Ondřej Surý PHP repository to Ubuntu 22.04. This repository contains various software packages for Debian and Ubuntu.

Add the Sury PHP repo using the following command:

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

Update and upgrade your APT repository list again:

sudo apt update && sudo apt upgrade

Now, let’s see how to Install PHP 7.4 on Ubuntu 22.04 with Apache and Nginx options.

2. Installing PHP 7.4 on Ubuntu 22.04

If you are using an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.

Installing PHP 7.4 with Apache module

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

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

After 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: PHP-FPM is not enabled for Apache by default. Enable it using the following command:

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

Then, restart Apache again:

sudo systemctl restart apache2

Verify that your PHP-FPM service is active and running on your server using this command:

sudo systemctl status php7.4-fpm
[Example 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 Tue 2023-10-27 10:00:00 UTC; 10s ago
       Docs: man:php-fpm7.4(8)
   Main PID: 1234 (php-fpm7.4)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 2289)
     Memory: 10.3M
        CPU: 60ms
     CGroup: /system.slice/php7.4-fpm.service

Also, verify that PHP 7.4 is installed on your Ubuntu 22.04 system by checking its version:

php --version
[Example Output]
PHP 7.4.33 (cli) (built: Oct  6 2023 14:23:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

Installing PHP 7.4 with Nginx Module

Nginx does not natively process PHP like Apache. You need to install PHP-FPM to handle PHP files.

To Install PHP 7.4 on Ubuntu 22.04 and PHP 7.4-FPM, run the following command:

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

PHP-FPM will start automatically after the installation is complete.

To verify that it is active and running on your server, run the command below:

sudo systemctl status php7.4-fpm
[Example 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 Tue 2023-10-27 10:05:00 UTC; 15s ago
       Docs: man:php-fpm7.4(8)
   Main PID: 5678 (php-fpm7.4)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 2289)
     Memory: 9.8M
        CPU: 55ms
     CGroup: /system.slice/php7.4-fpm.service

Now, edit your Nginx server block and add the example below for Nginx to process the PHP files on Ubuntu 22.04.

This example shows the necessary configuration within the server block to process PHP files, requiring the location ~ .php$ block:

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

Now, check that you have no syntax errors for Nginx:

sudo nginx -t
[Example 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

Also, verify that PHP 7.4 is installed on your server by checking its version:

php --version
[Example Output]
PHP 7.4.33 (cli) (built: Oct  6 2023 14:23:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

That’s it. You are done!

Conclusion

At this point, you have learned to Install PHP 7.4 on Ubuntu 22.04 with Apache and Nginx options. Installing PHP 7.4 on Ubuntu 22.04 involves adding a third-party repository because it’s not included by default. You can use Ondřej’s PPA for this purpose. After installation, configure it for Apache or Nginx as required. This setup ensures compatibility with older applications while operating on a modern system.

Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and YouTube.

You may be interested in these articles:

Install and Configure LibreNMS on Ubuntu 22.04

How To Set up Zenmap on Ubuntu 22.04

Ubuntu 24.04 PHP Setup Guide

Run PHP 8.3 on Linux Mint 21

Alternative Solutions for Installing PHP 7.4 on Ubuntu 22.04

While the Ondřej Surý PPA is the most common and generally recommended approach, here are two alternative methods for installing PHP 7.4 on Ubuntu 22.04.

1. Using Docker

Docker provides a containerization solution, allowing you to run applications in isolated environments. This approach is particularly useful if you want to avoid modifying your host system’s PHP configuration or if you need to run multiple PHP versions simultaneously.

Explanation:

Instead of directly installing PHP 7.4 on your Ubuntu 22.04 system, you can create a Docker container with PHP 7.4 pre-installed. This container will have its own isolated file system and dependencies, preventing conflicts with other software on your host machine.

Steps:

  1. Install Docker: If you don’t have Docker installed, follow the official Docker documentation for Ubuntu: https://docs.docker.com/engine/install/ubuntu/

  2. Create a Dockerfile: Create a new file named Dockerfile (without any extension) in your project directory. Add the following content:

FROM php:7.4-apache

# Install any additional extensions you need
RUN apt-get update && apt-get install -y 
    libpng-dev 
    libjpeg62-turbo-dev 
    libfreetype6-dev 
    zip 
    unzip 
    && docker-php-ext-configure gd --with-freetype --with-jpeg 
    && docker-php-ext-install -j $(nproc) gd mysqli pdo_mysql zip

# Set working directory
WORKDIR /var/www/html

# Copy your application code
COPY . /var/www/html

Explanation of Dockerfile:

  • FROM php:7.4-apache: This line specifies the base image for your container. It uses the official PHP 7.4 image with Apache pre-configured. You could also use php:7.4-fpm if you prefer using PHP-FPM.
  • RUN apt-get update && apt-get install -y ...: This line installs additional PHP extensions and libraries that your application might require. Adjust this to include all extensions your application needs.
  • WORKDIR /var/www/html: This line sets the working directory inside the container to /var/www/html, which is the default web root for Apache.
  • COPY . /var/www/html: This line copies all files from your current directory to the web root inside the container.
  1. Build the Docker image: In your terminal, navigate to the directory containing the Dockerfile and run the following command:
docker build -t my-php74-app .

This command builds a Docker image named my-php74-app based on the instructions in the Dockerfile.

  1. Run the Docker container: Once the image is built, run the container using the following command:
docker run -d -p 80:80 my-php74-app
  • -d: Runs the container in detached mode (background).
  • -p 80:80: Maps port 80 on your host machine to port 80 inside the container. This allows you to access your application through your web browser.

Now, you should be able to access your PHP 7.4 application by navigating to http://localhost (or your server’s IP address) in your web browser.

2. Compiling from Source

This method involves downloading the PHP 7.4 source code and compiling it manually. This approach provides the greatest control over the installation process and allows you to customize the build options. However, it’s more complex and time-consuming than using a package manager.

Explanation:

Compiling from source gives you fine-grained control over every aspect of the PHP installation. You can choose specific extensions, configure optimization flags, and ensure compatibility with your specific hardware and software environment. However, this approach requires a deeper understanding of the build process and dependency management.

Steps:

  1. Install Build Dependencies: Before you can compile PHP from source, you need to install the necessary build tools and libraries.
sudo apt-get update
sudo apt-get install -y build-essential autoconf libtool bison re2c libxml2-dev libxslt-dev libbz2-dev libpng-dev libjpeg-dev libfreetype6-dev libgmp-dev libcurl4-openssl-dev libssl-dev
  1. Download PHP 7.4 Source Code: Download the PHP 7.4 source code from the official PHP website archive: https://www.php.net/releases/. Replace 7.4.x with the specific version you want to download.
wget https://www.php.net/distributions/php-7.4.33.tar.gz
tar -xvf php-7.4.33.tar.gz
cd php-7.4.33
  1. Configure the Build: Use the ./configure script to prepare the build environment. This is where you specify which extensions you want to include. The following is a basic example. You will likely need to add more options to include the extensions your applications require.
./configure --prefix=/usr/local/php74 --with-apxs2=/usr/bin/apxs2 --with-mysqli --with-pdo-mysql --with-openssl --enable-mbstring --with-gd --with-jpeg --with-png --with-freetype

Explanation of configure options:

  • --prefix=/usr/local/php74: Specifies the installation directory. Adjust this to your preference.
  • --with-apxs2=/usr/bin/apxs2: Enables Apache module support. This is required if you want to run PHP as an Apache module. If using Nginx, omit this and use PHP-FPM.
  • --with-mysqli: Enables MySQLi extension.
  • --with-pdo-mysql: Enables PDO MySQL extension.
  • --with-openssl: Enables OpenSSL support.
  • --enable-mbstring: Enables multi-byte string support.
  • --with-gd: Enables GD graphics library support
  • --with-jpeg: Enables JPEG support within the GD library
  • --with-png: Enables PNG support within the GD library
  • --with-freetype: Enables FreeType support within the GD library

Important: Review the ./configure --help output for all available options. The options shown are a minimum and you will almost certainly need to add others based on your application’s requirements. Make sure to install the necessary -dev packages using apt-get for any extensions you plan to include.

  1. Compile and Install:
make
sudo make install
  1. Configure Apache (if applicable): If you configured PHP as an Apache module, you’ll need to configure Apache to use the new PHP version. Edit your Apache configuration (usually in /etc/apache2/mods-available/php.conf) and update the paths to point to the new PHP 7.4 module (e.g., /usr/local/php74/lib/libphp7.so). You may need to disable the default PHP version first using sudo a2dismod php8.1 (or whatever the default version is) and then enable the new version using sudo a2enmod php74. The exact commands may vary depending on your system.

  2. Configure PHP-FPM (if applicable): If you did not build PHP as an Apache module, then you will need to download, compile and configure PHP-FPM separately. This is outside the scope of this example.

  3. Update PATH (optional): You may want to add /usr/local/php74/bin to your system’s PATH so you can run php commands from the command line without specifying the full path. Edit your ~/.bashrc file and add the following line:

export PATH="/usr/local/php74/bin:$PATH"

Then, source the file:

source ~/.bashrc
  1. Verify Installation:
/usr/local/php74/bin/php --version

This should display the PHP 7.4 version you just compiled.

Caution: Compiling from source is more complex and requires careful attention to detail. It’s essential to understand the build process and dependencies. Also, manually compiled software does not get automatically updated through apt, so you will need to repeat this process to update PHP in the future.

Both alternative methods provide ways to Install PHP 7.4 on Ubuntu 22.04 without relying on the Ondřej Surý PPA. Choose the method that best suits your needs and technical expertise.