How to Install PHP 7.4 on Ubuntu 24.04
PHP 7.4 is a popular PHP release and the last mirror version for PHP 7. It is supported and required by most applications due to its extensible features and compatibility with multiple services such as database backends. You can use PHP 7.4 with all supported extensions on Ubuntu 24.04 and integrate applications such as web servers to deliver dynamic web applications.
This article explains how to install PHP 7.4 on Ubuntu 24.04. You will use PHP, PHP 7.4 FPM, and common extensions to deliver dynamic web applications while integrating supported services such as web servers with PHP.
Prerequisites
Before you begin, you need to:
- Have access to an Ubuntu 24.04 instance as a non-root sudo user.
Add the PHP PPA to the APT Package Manager
PHP 7.4 is discontinued and not available in the APT package sources on Ubuntu 24.04. Adding a Personal Package Archive (PPA) repository source allows you to install PHP 7.4 with all supported PECL (PHP Extension Community Library) extensions required in most applications. Follow the steps below to add the ondrej/ppa source to the APT package sources on Ubuntu 24.04.
Update the APT package information index
$ sudo apt update
Add the ondrej/ppa source to your APT sources
$ sudo add-apt-repository ppa:ondrej/php
Press Enter when prompted to add the PPA to your APT sources.
WARNING: add-apt-repository is broken with non-UTF-8 locales, see https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:
# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
Press [ENTER] to continue or Ctrl-c to cancel adding it.
Update the APT package index to apply the PPA repository information
$ sudo apt update
Install PHP 7.4
You can install PHP 7.4 on Ubuntu 24.04 after adding the main PPA source on your server. Follow the steps below to install PHP 7.4 and all required extensions on Ubuntu 24.04.
Update the APT package index
$ sudo apt update
Install PHP 7.4
$ sudo apt install php7.4 -y
Check the installed PHP version and verify that it’s 7.4
$ php -v
Use the php7.4 -v
command if you have another installed PHP version on your server.
Output:
PHP 7.4.33 (cli) (built: Dec 24 2024 07:12:16) ( 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
Install common PHP 7.4 extensions
$ sudo apt install php7.4-mysql php7.4-gd php7.4-xml php7.4-zip php7.4-mbstring php7.4-curl php7.4-json -y
The command installs the following common PHP extensions on the server:
- php-mysql: Connects PHP to MySQL database sources.
- php-gd: Used for image manipulation.
- php-xml: Enables XML file support in PHP applications.
- php-zip: Enables ZIP compression support in PHP applications.
- php-mbstring: Handles multistring data in PHP applications.
- php-curl: Allows PHP applications to create connections to external APIs.
- php-json: Enables JSON encoding and decoding in PHP applications.
Install PHP 7.4 FPM
PHP-FPM (FastCGI Process Manager) is a high-performance process manager for PHP that efficiently handles connections between PHP and other applications or services. PHP-FPM enables efficient and scalable connections between PHP and other applications using a UNIX socket or port. It’s available as a separate module and is not installed with PHP directly. Follow the steps below to install PHP 7.4 FPM on Ubuntu 24.04 to optimize and handle PHP connections.
Update the APT package information index
$ sudo apt update
Install PHP 7.4 FPM
$ sudo apt install php7.4-fpm -y
Enable the PHP 7.4 FPM service to start at boot
$ sudo systemctl enable php7.4-fpm
Start the PHP 7.4 FPM service
$ sudo systemctl start php7.4-fpm
Check the PHP 7.4 FPM status and verify that it’s active and running
$ sudo systemctl status php7.4-fpm
Output:
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php7.4-fpm.service; enabled; preset: enabled)
Active: active (running) since Wed 2025-03-19 00:30:07 UTC; 1min 41s ago
Docs: man:php-fpm7.4(8)
Main PID: 27348 (php-fpm7.4)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 2265)
Memory: 8.3M (peak: 9.1M)
CPU: 82ms
CGroup: /system.slice/php7.4-fpm.service
├─27348 "php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)"
├─27350 "php-fpm: pool www"
└─27351 "php-fpm: pool www"
Configure PHP 7.4
PHP 7.4 FPM uses pools to configure the PHP process information, enable and optimize PHP connections with other applications. Follow the steps below to configure PHP 7.4 pools and test the connection to PHP on Ubuntu 24.04.
List the files in the /etc/php/7.4/fpm/ directory and verify that the php-fpm.conf file is available
$ ls /etc/php/7.4/fpm
Output:
conf.d php-fpm.conf php.ini pool.d
The /etc/php/7.4/fpm directory is the main configuration directory for PHP 7.4 FPM that includes the pool and configuration information. The php-fpm.conf file includes the main configuration information and links to the pool.d directory which includes pool configurations.
Open the www.conf default configuration using a text editor such as vim
$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf
Verify the [www] pool configuration name
[www]
Verify the user and group configuration the pool runs as
user = www-data
group = www-data
PHP-FPM uses the www-data user and group to run on the server by default. Specifying a different user allows PHP-FPM to run and interact with other applications.
Locate and verify the default listen configuration
listen = /run/php/php7.4-fpm.sock
The default configuration specifies the UNIX socket used to handle PHP connections on the server. Specifying an IP address like 127.0.0.1:9000 enables PHP FPM to listen for connections using a TCP port and address.
Find additional configurations, including pm to modify how PHP FPM handles connections on the server
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
Save the www.conf file and close the text editor to save the pool configuration changes.
Create new configurations in the pool.d directory to set up additional custom pool configurations with the .conf extension to handle PHP connections on the server
$ sudo vim /etc/php/7.4/fpm/pool.d/.conf
Restart PHP 7.4 FPM to apply the pool configuration changes
$ sudo systemctl restart php7.4-fpm
Test PHP 7.4
Follow the steps below to create a sample PHP application and test PHP 7.4 on your Ubuntu 24.04 server.
Test the active PHP version and verify that it’s 7.4
$ php -v
Or, run php7.4 -v if multiple versions are installed.
$ php7.4 -v
Output:
PHP 7.4.33 (cli) (built: Dec 24 2024 07:12:16) ( 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
Create an app.php test application file
$ vim app.php
Enter the following PHP code into the file
Save the app.php file and close the text editor.
The PHP code in the above file displays a Greetings from centron message when it runs.
Run the app.php application using PHP
$ php app.php
Verify that the PHP application runs without any errors in your output
Greetings from centron
Conclusion
You have installed PHP 7.4 on Ubuntu 24.04 and configured it to run using PHP FPM pools. You can use PHP 7.4 with most dynamic applications and install additional modules that enable you to integrate PHP with other services, including Nginx and MySQL, to process requests depending on your project needs.