PHP (Hypertext Preprocessor) is a widely-used, open-source, server-side scripting language designed for web development. It is especially suited for creating dynamic web pages and applications.
In this tutorial, we will show you how to install PHP 8.4 on a Ubuntu 24.04 OS.
Update Operating System
Update your Ubuntu 22.04 operating system to make sure all existing packages are up to date:
# apt update && apt upgrade
Add PHP Repository
By default, PHP 8.4 is not included in the Ubuntu 24.04 default repository. So you will need to add Ondrej Sury PPA into your system.
First, install the required packages using the following command:
# apt-get install ca-certificates apt-transport-https software-properties-common
Once all the packages are installed, add this PPA using the following command:
# add-apt-repository ppa:ondrej/php
Once you are done, update the repository with the following command:
# apt-get update
Install PHP 8.4
Now, you can install the PHP 8.4 using the following command:
# apt-get install php8.4
Once the PHP is installed, you can check the PHP version on your system with the following command:
# php8.4 --version
Output:
# PHP 8.4.1 (cli) (built: Nov 21 2024 14:54:00) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.1, Copyright (c) Zend Technologies
with Zend OPcache v8.4.1, Copyright (c), by Zend Technologies
Install PHP 8.4 for Apache
To install PHP as an Apache module, execute:
# apt install libapache2-mod-php8.4
Then, restart Apache to integrate the new PHP module:
# systemctl restart apache2
To verify that PHP is working with the Apache web server, you can create a test PHP file:
echo "<?php phpinfo(); ?>" | tee /var/www/html/info.php
Then open your web browser and type http://your-IP-address/info.php and you should see the PHP information page.
Install PHP 8.4 FPM for Nginx
For the Nginx web server, you need to install the FPM service, you can install it using the following command:
# apt install php8.4-fpm
Once the installation has been completed, you can confirm that the PHP-FPM
service has been installed correctly with the following command:
# systemctl status php8.4-fpm
Output:
● php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php8.4-fpm.service; enabled; preset: enabled)
Active: active (running)
Docs: man:php-fpm8.4(8)
Process: 11741 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.4/fpm/pool.d/www.conf 84 (code=exited, status=0/SUCCESS)
Main PID: 11737 (php-fpm8.4)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0.00req/sec"
Tasks: 3 (limit: 2218)
Memory: 8.0M (peak: 9.0M)
CPU: 116ms
CGroup: /system.slice/php8.4-fpm.service
├─11737 "php-fpm: master process (/etc/php/8.4/fpm/php-fpm.conf)"
├─11739 "php-fpm: pool www"
└─11740 "php-fpm: pool www"
Test PHP and PHP-FPM
To configure Nginx to use PHP-FPM, you need to edit the default Nginx configuration file:
# nano /etc/nginx/sites-available/default
Add the following configurations to the file.
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
}
Check Nginx syntax:
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Then create a test PHP file similar to the Apache setup:
echo "<?php phpinfo(); ?>" | tee /var/www/html/info.php
To implement the changes, restart Nginx webserver:
# systemctl restart nginx
Then open your web browser and type http://your-IP-address/info.php and you should see the PHP information page.
Install PHP Extension
Installing PHP extensions are simple with the below-mentioned syntax:
# sudo apt install php8.4-[extension]
Replace [extension] with the extension you want to install, if you want to add multiple extensions then include them in braces:
# apt install php8.4-mysql php8.4-imap php8.4-ldap php8.4-xml php8.4-curl php8.4-mbstring php8.4-zip
To check loaded PHP modules use the command:
# php8.4 -m
Example Output:
[PHP Modules]
..............
imap
json
ldap
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
Phar
posix
..............
[Zend Modules]
Zend OPcache
Running PHP 8.4 with Other Versions
Instead of removing old PHP versions, it is also possible to run multiple PHP versions side-by-side.
The update-alternatives command provides an easy way to switch between PHP versions for PHP CLI.
# update-alternatives --config php
This brings up a prompt to interactively select the alternative PHP binary path that php
points to.
There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php.default 100 auto mode
1 /usr/bin/php.default 100 manual mode
2 /usr/bin/php8.3 83 manual mode
3 /usr/bin/php8.4 84 manual mode
To set the path without the interactive prompt:
# update-alternatives --set php /usr/bin/php8.4
Comments and Conclusion
In the tutorial, you have learned how to install PHP 8.4 on Ubuntu 24.04.
For additional help or useful information, we recommend you to check the official PHP 8.4 documentation.
If you have any questions please leave a comment below.