How to Install PHP 8 on Debian 12

Updated on 12 June, 2025
Install PHP 8.4 on Debian 12 with Apache. Set up PHP-FPM for efficient performance and explore new features like JIT, named arguments, and union types.
How to Install PHP 8 on Debian 12 header image

PHP (Hypertext Preprocessor) is a widely used, open-source server-side scripting language for building dynamic websites and applications. The PHP 8.x series introduces significant performance improvements, including Just-In-Time (JIT) compilation, named arguments, union types, and other modern features that enhance efficiency and modernize the development experience.

This article guides you through installing PHP 8.4 on Debian 12, setting up PHP-FPM for efficient request handling, and integrating it with the Apache web server.

Prerequisites

Before you begin, you need to:

Add the PHP PPA Repository

Debian 12's default APT repositories may not include the latest PHP releases. The SURY repository, maintained by Debian PHP package maintainer Ondřej Surý, provides up-to-date PHP versions and is widely trusted in the community.

  1. Update the system package index and upgrade existing packages.

    console
    $ sudo apt update && sudo apt upgrade -y
    
  2. Install required dependencies.

    console
    $ sudo apt install -y lsb-release apt-transport-https ca-certificates
    
  3. Import the SURY GPG key to verify the repository's package authenticity.

    console
    $ sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    
  4. Add the SURY PHP repository to your system's APT sources.

    console
    $ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
    
  5. Update the APT package index again to include the new repository.

    console
    $ sudo apt update -y
    
  6. PHP 8.4 is currently available through the packages.sury.org/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+96+0~20250402.56+debian12~1.gbp84a5b7
      Version table:
         2:8.4+96+0~20250402.56+debian12~1.gbp84a5b7 500
            500 https://packages.sury.org/php bookworm/main amd64 Packages
         2:8.2+93 500
            500 https://deb.debian.org/debian bookworm/main amd64 Packages
            500 https://debian.mirror.constant.com bookworm/main amd64 Packages
    

    This confirms that PHP 8.4 is available from the SURY repository.

    You can also visit the PHP Releases page to verify the latest official version.

Install PHP 8

With the SURY repository enabled, you can now install PHP 8.4 and configure commonly used extensions. These are required for features like database access, string manipulation, file handling, and API communication.

  1. Install PHP 8.4.

    console
    $ sudo apt install -y php8.4
    
  2. Verify the installed version.

    console
    $ php8.4 -v
    

    Output:

    PHP 8.4.6 (cli) (built: Apr 11 2025 02:09:29) (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
    
  3. List installed PHP versions and optionally set PHP 8.4 as the default system version.

    console
    $ ls /etc/php
    

    If only PHP 8.4 is installed, you can set it as the default system-wide PHP interpreter using update-alternatives.

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

    Then verify:

    console
    $ php -v
    
  4. Install common PHP extensions used for database, networking, and string handling.

    console
    $ sudo apt install -y php8.4-mysql php8.4-curl php8.4-xml php8.4-mbstring
    
    • php8.4-mysql: MySQL/MariaDB database support
    • php8.4-curl: Enables cURL for remote data fetching
    • php8.4-xml: Handles XML parsing
    • php8.4-mbstring: Supports multibyte string encoding (important for UTF-8)
  5. List the active PHP modules to verify installation.

    console
    $ php8.4 -m
    

    This lists all active PHP modules, including the extensions you just installed.

Install PHP 8 FPM

PHP-FPM (FastCGI Process Manager) separates PHP execution from the web server, improving performance and scalability. In this section, you’ll install PHP-FPM 8.4, configure its socket permissions, and ensure it starts on boot.

  1. Install PHP 8.4 FPM.

    console
    $ sudo apt install -y php8.4-fpm
    
  2. Open the default PHP-FPM pool configuration file.

    console
    $ sudo nano /etc/php/8.4/fpm/pool.d/www.conf
    

    Define the Unix socket PHP-FPM will listen on.

    ini
    listen = /run/php/php8.4-fpm.sock
    

    Set socket permissions for Apache access.

    ini
    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660
    

    These settings ensure Apache can communicate with PHP-FPM via the socket.

  3. Enable PHP-FPM to start automatically on boot.

    console
    $ sudo systemctl enable php8.4-fpm
    
  4. Restart the PHP-FPM service to apply the configuration changes.

    console
    $ sudo systemctl restart php8.4-fpm
    
  5. Verify PHP-FPM is running.

    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; preset: enabled)
         Active: active (running) since Wed 2025-04-16 07:09:39 UTC; 13s ago
           Docs: man:php-fpm8.4(8)
        Process: 13047 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: 13044 (php-fpm8.4)
    ...

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

Test and Use PHP 8

PHP-FPM runs as a separate service that handles PHP execution more efficiently than traditional mod_php. It listens on a Unix socket and works alongside your web server. In this section, you’ll integrate Apache with PHP-FPM using proxy_fcgi, then deploy a test PHP script to confirm the setup.

  1. Install Apache.

    console
    $ sudo apt install -y apache2
    
  2. Enable the proxy_fcgi and setenvif modules in Apache to handle PHP via PHP-FPM.

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

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

    console
    $ sudo systemctl restart apache2
    
  5. Create a sample PHP application file.

    console
    $ sudo nano /var/www/html/sample.php
    

    Add the following content to the file:

    php
    <?php
    phpinfo();
    ?>
    
  6. Allow HTTP traffic through the firewall.

    console
    $ sudo ufw allow 80/tcp
    
  7. Reload the firewall configuration.

    console
    $ sudo ufw reload
    
  8. Open a browser and visit:

    http://SERVER-IP/sample.php

    Replace SERVER-IP with your server’s actual IP address. You should see the PHP information page confirming the active PHP version and configuration.

    Verify PHP Installation

Conclusion

In this article, you installed PHP 8.4 and PHP-FPM on a Debian 12 server. You added the SURY repository to access the latest PHP packages, installed and verified PHP 8.4, configured PHP-FPM for efficient process management, and integrated it with the Apache web server. You also deployed a test PHP script to confirm the setup works as expected. With this environment in place, your server is now ready for hosting modern PHP applications. For more details on PHP features and configuration, refer to the official PHP documentation.

Tags:

Comments

No comments yet.