How to Install PHP and PHP-FPM on Debian 12

Updated on September 11, 2024
How to Install PHP and PHP-FPM on Debian 12 header image

Introduction

PHP (Hypertext Preprocessor) is a popular general-purpose server-side scripting language for developing dynamic and interactive web applications. PHP-FPM (FastCGI Process Manager) enhances PHP performance to handle heavy loads through advanced process management, worker pools, and more.

This article explains how to install PHP and PHP-FPM on Debian 12.

Prerequisites

Before you begin:

Install PHP

PHP is available in the default package repositories on Debian 12 but the available version might be out-of-date. Follow the steps below to download the SURY PHP PPA repository which includes the latest PHP package information and install PHP.

  1. Update the server's package information index.

    console
    $ sudo apt update -y
    
  2. Upgrade all existing packages.

    console
    $ sudo apt upgrade -y
    
  3. Install the necessary dependency packages for HTTPS transport and certificate management.

    console
    $ sudo apt -y install lsb-release apt-transport-https ca-certificates
    
  4. Download the SURY GPG key.

    console
    $ wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    
  5. Add the SURY PPA repository information to your APT sources.

    console
    $ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
    
  6. Update the server's package information index to apply the repository changes.

    console
    $ sudo apt update -y
    
  7. View all the available PHP versions in the APT repository sources.

    console
    $ sudo apt policy php
    

    Output:

    php:
      Installed: (none)
      Candidate: 2:8.3+94+0~20240205.51+debian12~1.gbp6faa2e
      Version table:
         2:8.3+94+0~20240205.51+debian12~1.gbp6faa2e 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

    From the above output, PHP 8.3 is the latest version you can install from the SURY PPA repository.

  8. Install the latest PHP version.

    console
    $ sudo apt install -y php
    
  9. Verify the PHP version.

    console
    $ php -v
    

    Output:

    PHP 8.3.9 (cli) (built: Jul  8 2024 10:27:02) (NTS)

Install PHP Extensions

PHP extensions add functionalities for running most dynamic web applications. Follow the steps below to install common PHP extensions.

  1. Install PHP extensions.

    console
    $ sudo apt install -y php-mysql php-curl php-json php-xml php-mbstring
    

    The above command installs the following PHP extensions:

    • php-mysql: Connects PHP with MySQL databases.
    • php-curl: Allows PHP to create HTTP requests.
    • php-json: Enables PHP to handle JSON data.
    • php-xml: Enables support for XML data.
    • php-mbstring: Manages multi-byte strings.
  2. View the available extensions on your server.

    console
    $ php -m
    

    Output:

    [PHP Modules]
    ...
    curl
    json
    mbstring
    mysqli
    xml
    ...
    
    [Zend Modules]
    Zend OPcache

Install and Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) manages the connection between PHP and other applications, such as web servers, to process dynamic content. Follow the steps below to install and configure PHP-FPM based on the PHP version on your server.

  1. Install PHP-FPM.

    console
    $ sudo apt install -y php-fpm
    
  2. Enable PHP-FPM to automatically start at boot.

    console
    $ sudo systemctl enable php8.3-fpm
    
  3. Start the PHP-FPM service.

    console
    $ sudo systemctl start php8.3-fpm
    
  4. Verify the PHP-FPM service status and verify that it's running.

    console
    $ sudo systemctl status php8.3-fpm
    

    Output:

    ● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
         Loaded: loaded (/lib/systemd/system/php8.3-fpm.service; enabled; preset: enabled)
         Active: active (running) since Mon 2024-07-29 07:16:08 UTC; 20s ago
           Docs: man:php-fpm8.3(8)
       Main PID: 28097 (php-fpm8.3)
         Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0.00req/sec"
          Tasks: 3 (limit: 9468)
         Memory: 13.5M
            CPU: 64ms
         CGroup: /system.slice/php8.3-fpm.service
                 ├─28097 "php-fpm: master process (/etc/php/8.3/fpm/php-fpm.conf)"
                 ├─28098 "php-fpm: pool www"
                 └─28099 "php-fpm: pool www"

Test PHP and PHP-FPM

Follow the steps below to create a sample application to display the available PHP version information.

  1. Install Apache web server package.

    console
    $ sudo apt install -y apache2
    
  2. Start the Apache web server.

    console
    $ sudo systemctl start apache2
    
  3. Create a new info.php PHP application file in the default /var/www/html web root directory.

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

    php
    <?php
    
        echo "<br><h1 align='center'> Greetings from Vultr </h1>";
    
    ?>
    

    Save and close the file.

  5. Enable the PHP-FPM configuration for Apache.

    console
    $ sudo a2enconf php8.3-fpm
    

    Output:

    Enabling conf php8.3-fpm.
    To activate the new configuration, you need to run:
      systemctl reload apache2
  6. Allow network connections to the Apache HTTP port 80 through the firewall.

    console
    $ sudo ufw allow 80/tcp
    
  7. Reload UFW to apply the new changes.

    console
    $ sudo ufw reload
    
  8. Open a web browser such as Chrome and access your server's public IP address. Then, load the /info.php file to access the PHP application.

    http://SERVER-IP/info.php

    Test Access to PHP using Apache

Install Multiple PHP Versions

Multiple web development projects may require specific PHP versions to run on your server. Follow the steps below to install multiple PHP versions and use separate PHP-FPM pools to manage each PHP version.

  1. Install PHP 8.0 and common extensions.

    console
    $ sudo apt install -y php8.0 php8.0-fpm php8.0-mysql php8.0-curl php8.0-xml php8.0-mbstring
    
  2. Enable PHP-FPM for PHP 8.0 to automatically start at boot.

    console
    $ sudo systemctl enable php8.0-fpm
    
  3. Start the PHP-FPM service for PHP 8.0.

    console
    $ sudo systemctl start php8.0-fpm
    
  4. Install PHP 7.4 and common extensions.

    console
    $ sudo apt install -y php7.4 php7.4-fpm php7.4-mysql php7.4-curl php7.4-json php7.4-xml php7.4-mbstring
    
  5. Enable PHP-FPM for PHP 7.4 to start at boot.

    console
    $ sudo systemctl enable php7.4-fpm
    
  6. Start the PHP-FPM service for PHP 7.4.

    console
    $ sudo systemctl start php7.4-fpm
    
  7. Enable the PHP 8.0 FPM configuration for Apache.

    console
    $ sudo a2enconf php8.0-fpm
    
  8. Enable the PHP 7.4 FPM configuration.

    console
    $ sudo a2enconf php7.4-fpm
    
  9. Restart Apache to apply the configuration changes.

    console
    $ sudo systemctl restart apache2
    
  10. Create a new Apache virtual host configuration file.

    console
    $ sudo nano /etc/apache2/sites-available/php-multi.conf
    
  11. Add the following configurations to the file.

    apache
    <VirtualHost *:80>
        DocumentRoot /var/www/html
    
        <Directory /var/www/html>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
        </Directory>
    
        <Location /php83>
            ProxyPass "unix:/run/php/php8.3-fpm.sock|fcgi://localhost/var/www/html/php83"
        </Location>
    
        <Location /php80>
            ProxyPass "unix:/run/php/php8.0-fpm.sock|fcgi://localhost/var/www/html/php80"
        </Location>
    
        <Location /php74>
            ProxyPass "unix:/run/php/php7.4-fpm.sock|fcgi://localhost/var/www/html/php74"
        </Location>
    </VirtualHost>
    

    Save and close the file.

    The above Apache virtual host configuration listens for connection requests on all server interfaces and forwards specific requests to the appropriate PHP-FPM version for processing.

  12. Disable the default Apache virtual host.

    console
    $ sudo a2dissite 000-default.conf
    
  13. Enable the new virtual host.

    console
    $ sudo a2ensite php-multi.conf
    
  14. Enable the necessary Apache proxying and rewrite modules.

    console
    $ sudo a2enmod proxy proxy_fcgi rewrite
    
  15. Create new directories to store PHP 8.3, PHP 8.0, and PHP 7.4 files.

    console
    $ sudo mkdir -p /var/www/html/php83 /var/www/html/php80 /var/www/html/php74
    
  16. Create a sample info.php file under the /var/www/html/php83 directory.

    console
    $ sudo nano /var/www/html/php83/info.php
    
  17. Add the following contents to the file.

    php
    <?php
        phpinfo();
    ?>
    

    Save and close the file.

    Repeat the above steps and create additional info.php files in the /var/www/html/php80 and /var/www/html/php74 directories.

  18. Open a web browser and access the following paths. Each web page should display the respective PHP version.

    • PHP Version 8.3:

      http://SERVER-IP/php83/info.php

      PHP 8.3 Information

    • PHP Version 8.0:

      http://SERVER-IP/php80/info.php

      PHP 8.0 Information

    • PHP Version 7.4:

      http://SERVER-IP/php74/info.php

      PHP 7.4 Information

Conclusion

You have installed PHP and PHP-FPM on Debian 12. You can integrate PHP with multiple applications to deliver and process dynamic web applications using PHP-FPM pools. For more information, visit the PHP documentation.