PHP (Hypertext Preprocessor) is a popular open-source scripting language designed to create dynamic and interactive web applications. To leverage its newest features, you can install PHP 8 on Ubuntu 24.04. This release includes PHP 8.4 performance improvements like JIT compilation, better type safety, and extended syntax options.
This guide explains how to install PHP 8.4 on Ubuntu 24.04 using the ppa:ondrej/php repository. It covers configuring PHP-FPM, validating the setup with both Apache and PHP’s built-in server, and checking that common extensions are active.
Prerequisites
Before starting, ensure the following:
- You have an Ubuntu 24.04 server.
- You can access the server via SSH as a non-root user with sudo privileges.
- Your server is updated.
Matching infrastructure at centron
Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
Add the PHP PPA Repository
Ubuntu’s default repositories may not provide older PHP 8.x versions. To install PHP 8.4, you will use the trusted ppa:ondrej/php Personal Package Archive (PPA), which offers maintained builds and extensions.
First, update the APT package index:
$ sudo apt updateNext, add the ppa:ondrej/php repository:
$ sudo add-apt-repository ppa:ondrej/phpPress Enter when asked to confirm. Then, update the APT package index again:
$ sudo apt updateVerify that PHP 8.4 is available in your package sources:
$ sudo apt policy phpExample output:
php:
Installed: (none)
Candidate: 2:8.4+99+ubuntu24.04.1+deb.sury.org
Version table:
2:8.4+99+ubuntu24.04.1+deb.sury.org 500
500 http://ppa.launchpad.net/ondrej/php/ubuntu noble/main amd64 Packages
2:8.2+93ubuntu1 500
500 http://archive.ubuntu.com/ubuntu noble/main amd64 Packages
This confirms PHP 8.4 is available from the PPA. For official release details, refer to the PHP Releases page.
Install PHP 8 on Ubuntu 24.04
Now that the PPA is added, install PHP 8.4 and essential extensions required for most web applications.
Install PHP 8.4:
$ sudo apt install php8.4 -yInstall common PHP extensions:
$ sudo apt install php8.4-common php8.4-cli php8.4-opcache php8.4-mysql php8.4-xml php8.4-curl php8.4-zip php8.4-mbstring php8.4-gd php8.4-intl php8.4-bcmath -yThese extensions are frequently required by CMS platforms, frameworks, and APIs:
- php8.4-common: Core PHP files
- php8.4-cli: Command-line interface
- php8.4-opcache: Bytecode caching for performance
- php8.4-mysql: MySQL/MariaDB database support
- php8.4-xml: XML parsing
- php8.4-curl: cURL library support
- php8.4-zip: ZIP archive handling
- php8.4-mbstring: Multibyte string encoding
- php8.4-gd: Image processing
- php8.4-intl: Internationalization support
- php8.4-bcmath: Arbitrary precision math
Verify the PHP Installation
Check the installed PHP version:
$ php -vExpected output:
PHP 8.4.6 (cli) (built: Apr 11 2025 02:19:40) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.6, Copyright (c) Zend Technologies
with Zend OPcache v8.4.6, Copyright (c), by Zend Technologies
List installed PHP versions and optionally set PHP 8.4 as the default:
$ ls /etc/phpIf only PHP 8.4 is installed, set it as the system default:
$ sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.4 84Confirm the version again:
$ php -vFinally, list active PHP modules to verify extensions are loaded:
$ php -mThis displays all active PHP modules on your system.
Install PHP 8 FPM
PHP-FPM (FastCGI Process Manager) runs PHP as a separate background service and processes dynamic requests more efficiently than traditional CGI. It improves performance, resource management, and is suitable for high-traffic applications. Follow the steps below to configure PHP-FPM for optimal performance.
Install PHP 8.4 FPM:
$ sudo apt install php8.4-fpm -yStart the PHP-FPM service:
$ sudo systemctl start php8.4-fpmEnable PHP-FPM to start automatically on boot:
$ sudo systemctl enable php8.4-fpmCheck the status of the PHP-FPM service:
$ sudo systemctl status php8.4-fpmExample output:
● php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.4-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2025-04-11 02:19:40 UTC; 1min 30s ago
Main PID: 12345 (php-fpm8.4)
Status: "Processes active: 0, idle: 1, Requests: 1, slow: 0, Traffic: 0req/sec"
Configure PHP-FPM Socket (For Apache Integration)
When using PHP-FPM with Apache, it is recommended to connect through a Unix socket instead of TCP for better security and performance. Follow these steps to configure PHP-FPM to use a socket accessible by Apache.
Open the PHP-FPM pool configuration file:
$ sudo nano /etc/php/8.4/fpm/pool.d/www.confUpdate the following lines if they are not already present:
listen = /run/php/php8.4-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660These settings allow Apache (running as www-data) to send PHP requests through the PHP-FPM socket. PHP-FPM enables Apache to delegate PHP processing to a separate process manager, boosting performance and resource handling during heavy traffic. Integration works seamlessly with modules like proxy_fcgi and configuration snippets enabled through a2enconf.
For advanced tuning such as process limits and slow logs, refer to the official PHP-FPM documentation.
Test PHP with the Built-in Development Server
PHP provides a built-in development server for lightweight testing. This is useful to validate PHP functionality before configuring Apache or Nginx.
Create a test directory and PHP file:
$ mkdir ~/phptest && cd ~/phptest$ echo "<!--?php echo 'Greetings from centron'; ?-->" > index.phpStart the PHP development server:
$ php8.4 -S 0.0.0.0:8000Open http://SERVER-IP:8000 in your browser. You should see:
Greetings from centron
This method is suitable for basic testing but not for production environments.
Test and Use PHP 8
This section demonstrates integrating Apache with PHP-FPM and verifying the setup using a sample PHP script. By using proxy_fcgi, Apache delegates PHP processing to PHP-FPM, improving performance and scalability.
If Apache is not installed, install it:
$ sudo apt install apache2Verify the Apache service is running:
$ sudo systemctl status apache2Example output:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2025-04-13 02:19:40 UTC; 8h ago
...
Enable the required Apache modules for PHP-FPM:
$ sudo a2enmod proxy_fcgi setenvifEnable the PHP-FPM configuration for Apache:
$ sudo a2enconf php8.4-fpmRestart Apache to apply changes:
$ sudo systemctl restart apache2Allow HTTP traffic through the firewall:
$ sudo ufw allow 80Reload the firewall configuration:
$ sudo ufw reloadCreate a test info.php file in the Apache web root directory:
$ sudo nano /var/www/html/info.phpAdd the following code to the file:
<!--?php phpinfo(); ?-->Save and exit the editor. Access your server’s IP address in a web browser and open the /info.php URL path to view the PHP information page. Replace SERVER-IP with your server's actual IP address:
http://SERVER-IP/info.php
Warning: After verifying the PHP info page, delete the file to prevent exposing server configuration details:
$ sudo rm /var/www/html/info.phpConclusion
In this guide, you installed PHP 8.4 on Ubuntu 24.04, added the necessary PPA repository, installed essential PHP extensions, and configured PHP-FPM. You also verified the installation by creating a test script served via Apache. With PHP 8.4 running, you are ready to build and deploy modern web applications using the latest PHP features and improvements. For further information, consult the official PHP documentation.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.