How To Install PHP 7.4 on Ubuntu 22.04

This tutorial intends to teach you How To Install PHP 7.4 on Ubuntu 22.04.

There are multiple uses of PHP that make it a strong scripting language option for web development. It operates over the web server and then processes all client requests into HTML files. Additionally, PHP is a general-purpose language; developers can use it to code for different applications.

Another good use of PHP is that it is compatible with different operating systems, like macOS, Windows, and Linux. Furthermore, it is possible to use PHP for varying web servers, e.g., OpenBSD, Nginx, and Apache. PHP is compatible with different cloud environments like Amazon AWS and Microsoft Azure.

Overall, PHP has a flexible structure and can generalize different types of formats, like PNG, JPEG, GIF, and PDFs.

Essentially, PHP can help with two applications:

  • Command-line scripting- You can use PHP script for the command-line scripting to conduct administrative work like PDF generation and email transfer.
  • Server-side scripting– Web developers can use PHP to develop dynamic web apps and websites.

Steps To To Install PHP 7.4 on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.

By default, PHP 8.1 is available in the default Ubuntu 22.04 repository. If you plan to use an older version like PHP 7.4, follow the steps below.

Install PHP Dependencies on Ubuntu 22.04

First, you need to update and upgrade your local package index with the following command:

sudo apt update && sudo apt upgrade -y

Then, you need to install the required packages on your server with the command below:

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

At this point, you need to add the  Ondřej Surý PHP repository on Ubuntu 22.04.

This is a home for packaging various software into Debian and Ubuntu.

To add the Sury PHP repo to your server, you can use 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 with Apache and Nginx options.

Installing PHP 7.4 on Ubuntu 22.04

If you run 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

When your installation is completed, restart Apache to apply the changes:

sudo systemctl restart apache2

To install PHP-FPM, you can 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 by the following command:

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

Then, restart Apache again:

sudo systemctl restart apache2

Verify your PHP-FPM service is active and running on your server with the command below:

sudo systemctl status php7.4-fpm
Output
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor pr>
     Active: active (running) since Tue 2022-10-04 11:15:55 UTC; 26s ago
       Docs: man:php-fpm7.4(8)
    Process: 12359 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
   Main PID: 12356 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 4575)
     Memory: 6.9M
        CPU: 59ms
     CGroup: /system.slice/php7.4-fpm.service
...

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

php --version
Output
PHP 7.4.32 (cli) (built: Sep 29 2022 22:25:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.32, Copyright (c), by Zend Technologies

Installing PHP 7.4 with Nginx Module

As you know, Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM to handle the PHP files.

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

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

PHP-FPM will start automatically when your installation is completed.

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

sudo systemctl status php7.4-fpm
Output
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor pr>
     Active: active (running) since Tue 2022-10-04 11:15:55 UTC; 26s ago
       Docs: man:php-fpm7.4(8)
    Process: 12359 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
   Main PID: 12356 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 4575)
     Memory: 6.9M
        CPU: 59ms
     CGroup: /system.slice/php7.4-fpm.service
...

At this point, you need to edit your Nginx server block and add the example below for Nginx to process the PHP files on Ubuntu 22.04.

This is an example for all server blocks that process PHP files that need the location ~ .php$ added.

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

Now you can check that you have no error syntax for Nginx:

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

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

php --version
Output
PHP 7.4.32 (cli) (built: Sep 29 2022 22:25:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.32, Copyright (c), by Zend Technologies

That’s it. You are done.

Conclusion

At this point, you learn to Install PHP 7.4 on Ubuntu 22.04 with Apache and Nginx options.

Hope you enjoy it.

You may be interested in these articles:

Install and Configure LibreNMS on Ubuntu 22.04

How To Set up Zenmap on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!