How to Install PHP 8 on Ubuntu 24.04

Updated on 08 May, 2025
How to Install PHP 8 on Ubuntu 24.04 header image

PHP (Hypertext Preprocessor) is a widely used, open-source scripting language for building dynamic and interactive web applications. PHP 8.4 introduces performance enhancements such as JIT compilation, improved type safety, and expanded syntax features.

This article shows you how to install PHP 8.4 on Ubuntu 24.04 using the ppa:ondrej/php repository. You'll configure PHP-FPM, verify the installation using two test methods (Apache and PHP's built-in server), and ensure common extensions are working properly.

Prerequisites

Before you begin:

Add the PHP PPA Repository

Ubuntu’s default package sources may not include older PHP 8.x versions. To install PHP 8.4, you’ll use the widely trusted ppa:ondrej/php Personal Package Archive (PPA), which offers maintained versions and related extensions.

  1. Update the APT package index.

    console
    $ sudo apt update
    
  2. Add the ppa:ondrej/php repository to your APT sources.

    console
    $ sudo add-apt-repository ppa:ondrej/php
    

    Press Enter when prompted to add the PPA to your APT sources.

  3. Update the APT package index to apply the PPA repository information.

    console
    $ sudo apt update
    
  4. PHP 8.4 is currently available through the ppa:ondrej/php repository. You can verify the available version in your APT sources by running:

    console
    $ sudo apt policy php
    

    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 that PHP 8.4 is available from the ppa:ondrej/php repository.

    For the latest official release details, refer to the PHP Releases page.

Install PHP 8

Now that you've added the PPA, install PHP 8.4 and essential extensions required by most modern web applications.

  1. Install PHP 8.4.

    console
    $ sudo apt install php8.4 -y
    
  2. Install commonly used PHP extensions.

    console
    $ 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 -y
    

    These extensions are frequently used in content management systems, 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 (UTF-8, etc.)
    • php8.4-gd: Image manipulation
    • php8.4-intl: Internationalization support
    • php8.4-bcmath: Arbitrary precision math
  3. Verify the PHP installation.

    console
    $ php -v
    

    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
  4. List installed PHP versions and optionally make PHP 8.4 the default.

    console
    $ ls /etc/php
    

    If only PHP 8.4 is installed, set it as the default system version:

    console
    $ sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.4 84
    

    Then confirm:

    console
    $ php -v
    
  5. Verify active modules to ensure extensions are loaded:

    console
    $ php -m
    

    This lists all active PHP modules.

Install PHP 8 FPM

PHP-FPM (FastCGI Process Manager) runs PHP as a separate background service and handles dynamic requests more efficiently than traditional CGI. It improves performance, resource control, and is ideal for high-traffic applications.

  1. Install PHP 8.4 FPM.

    console
    $ sudo apt install php8.4-fpm -y
    
  2. Start the PHP-FPM service.

    console
    $ sudo systemctl start php8.4-fpm
    
  3. Enable PHP-FPM to start automatically on boot.

    console
    $ sudo systemctl enable php8.4-fpm
    
  4. Check the status of PHP-FPM service.

    console
    $ sudo systemctl status php8.4-fpm
    

    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"

    Press Q to exit the status output.

Configure PHP-FPM Socket (For Apache Integration)

When using PHP-FPM with Apache, it's recommended to connect via a secure Unix socket instead of TCP for better performance and security. This section shows how to configure PHP-FPM to use a socket that Apache can access.

  1. Open the PHP-FPM pool configuration file.

    console
    $ sudo nano /etc/php/8.4/fpm/pool.d/www.conf
    
  2. Update the following lines (if not already present).

    ini
    listen = /run/php/php8.4-fpm.sock
    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660
    

    These settings allow Apache (running as www-data) to send PHP requests through the socket used by PHP-FPM.

PHP-FPM allows Apache to delegate PHP processing to a separate process manager, improving performance and resource handling under heavy traffic. The integration is seamless with modules like proxy_fcgi and configuration snippets enabled through a2enconf.

For advanced tuning options (e.g., process limits, slow logs), see the official PHP-FPM documentation.

Test and Use PHP 8

This section guides you through integrating Apache with PHP-FPM and verifying the setup with a sample PHP script. Using proxy_fcgi, Apache delegates PHP processing to PHP-FPM, improving performance and scalability.

If Apache is not already installed, install it:

console
$ sudo apt install apache2
  1. Verify the Apache service is running.

    console
    $ sudo systemctl status apache2
    

    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
    ...

    Press Q to exit the status output.

  2. Enable required Apache modules for PHP-FPM:

    console
    $ sudo a2enmod proxy_fcgi setenvif
    
  3. Enable the PHP-FPM configuration for Apache:

    console
    $ sudo a2enconf php8.4-fpm
    
  4. Restart Apache to apply changes:

    console
    $ sudo systemctl restart apache2
    
  5. Allow HTTP traffic through the firewall.

    console
    $ sudo ufw allow 80
    
  6. Reload the firewall configuration.

    console
    $ sudo ufw reload
    
  7. Create a test info.php file in the Apache web root directory.

    console
    $ sudo nano /var/www/html/info.php
    
  8. Add the following code to the file.

    php
    <?php
    phpinfo();
    ?>
    

    Save and exit the editor.

  9. Access your server's IP address in a web browser and load 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 PHP info, delete info.php to avoid exposing server configuration.
    console
    $ sudo rm /var/www/html/info.php
    

Conclusion

In this article, you installed PHP 8.4 on Ubuntu 24.04, added the required PPA repository, installed core PHP extensions, and configured PHP-FPM. You also verified the installation by creating a test script served through Apache. With PHP 8.4 now running on your system, you’re ready to start building and deploying modern web applications using the latest features and improvements. For more information, visit the official PHP documentation.

Comments

No comments yet.