How to Install PHP on Debian Linux

PHP powers dynamic websites by processing server-side code before delivering HTML to browsers. Whether you need to host WordPress on Debian, run Laravel applications, or build custom web tools, PHP integrates with Apache on Debian or Nginx on Debian to handle PHP requests.

By the end of this guide, you will have PHP installed from Debian’s default repositories with PHP-FPM configured for your web server, common extensions for database connectivity and caching, and the knowledge to troubleshoot connection issues. Specifically, the steps apply to Debian 13 (PHP 8.4), Debian 12 (PHP 8.2), and Debian 11 (PHP 7.4), with version-specific commands clearly marked where they differ.

Choose Your PHP Version for Debian

Compare Default PHP Versions by Debian Release

Each Debian release ships a specific PHP version in its default repositories. As a result, the PHP version you receive depends entirely on which Debian release you run.

Debian VersionDefault PHPDebian Security SupportBest For
Debian 13 (Trixie)PHP 8.4Until ~2028 (estimated)New deployments wanting latest features
Debian 12 (Bookworm)PHP 8.2Until June 2028Production servers prioritizing stability
Debian 11 (Bullseye)PHP 7.4Until August 2026Legacy applications requiring PHP 7.x

PHP 7.4 reached upstream end-of-life in November 2022, meaning the PHP project no longer releases updates. However, Debian’s security team backports critical security fixes to the Debian 11 package until August 2026. Therefore, for new projects, consider upgrading to Debian 12 or newer for actively maintained PHP versions.

Compare PHP Installation Options

Beyond the distro default, you can install newer PHP versions from the Sury repository. Consequently, the comparison below helps you decide which installation path fits your project requirements.

PHP VersionPrimary FocusPerformanceBest ForTrade-offs
PHP (Distro Default)Stability with Debian security team maintenanceVaries by version; Debian 12’s PHP 8.2 includes JIT compilation and OPcacheProduction servers prioritizing official Debian updates and minimal external dependenciesVersion varies by release (8.4 on Debian 13, 8.2 on Debian 12, 7.4 on Debian 11)
PHP 8.5Pipe operator, URI extension, clone with properties, array helpersImproved JIT and continued IR-based JIT framework refinementsProjects leveraging function chaining, advanced URL handling, or needing array_first()/array_last()Newest release; requires Sury repository on all Debian versions
PHP 8.4Property hooks, asymmetric visibility, HTML5 DOM APIContinued JIT improvements; new IR-based JIT frameworkProjects using modern OOP patterns, applications requiring array_find() and BCMath object APIRequires Sury repository on Debian 11/12; available in Debian 13 default repositories
PHP 8.3Typed class constants, json_validate(), readonly property cloningMature JIT optimization with broad extension compatibilityWordPress 6.x, Laravel 11, Drupal 10 sites needing stable features with security support through December 2027Requires Sury repository on all Debian versions; community-maintained updates

Recommendation: For most users, the distro default PHP provides the best balance of stability, security updates from Debian’s security team, and minimal maintenance. Alternatively, if you need a newer PHP version than your Debian release provides, see the PHP 8.5 on Debian guide for the pipe operator and array_first()/array_last() helpers, the PHP 8.4 on Debian guide for property hooks and the HTML5 DOM API, or the PHP 8.3 on Debian guide for typed class constants and json_validate().

Decide When to Use Default PHP

Install the default PHP package when your application supports the PHP version included in your Debian release and you prefer Debian’s security team to manage updates. In addition, this approach minimizes third-party dependencies and follows Debian’s tested, stable release cycle. However, if your application requires specific PHP features only available in newer versions, the Sury repository offers PHP 8.4 and PHP 8.3 across all supported Debian versions.

Update Debian Before PHP Installation

First, refresh the package index and upgrade existing packages to ensure your system has the latest security patches:

sudo apt update
sudo apt upgrade

Install PHP on Debian

Now, choose the installation option that matches your web server. Apache users can select either mod_php (simpler configuration) or PHP-FPM (better performance under load). Meanwhile, Nginx requires PHP-FPM exclusively.

Option 1: Install PHP with Apache mod_php

The mod_php approach embeds PHP directly into Apache, thereby making configuration straightforward for smaller sites. To begin, install PHP as an Apache module:

sudo apt install php libapache2-mod-php

Next, restart Apache to activate the new PHP module:

sudo systemctl restart apache2

Option 2: Install PHP with Apache and PHP-FPM

PHP-FPM (FastCGI Process Manager) handles PHP requests in a separate process pool, thereby providing better performance and resource isolation for high-traffic sites. Install PHP-FPM with Apache:

sudo apt install php-fpm libapache2-mod-fcgid

If you previously installed mod_php (libapache2-mod-php), disable it before using PHP-FPM to avoid handler conflicts: sudo a2dismod php (or the versioned name like php8.2)

Subsequently, enable the required Apache modules and PHP-FPM configuration. Use the commands matching your Debian release:

For Debian 13 (PHP 8.4):

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.4-fpm
sudo systemctl enable php8.4-fpm --now

For Debian 12 (PHP 8.2):

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm
sudo systemctl enable php8.2-fpm --now

For Debian 11 (PHP 7.4):

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.4-fpm
sudo systemctl enable php7.4-fpm --now

Afterwards, restart Apache to apply the configuration:

sudo systemctl restart apache2

Option 3: Install PHP with Nginx and PHP-FPM

Unlike Apache, Nginx uses PHP-FPM exclusively to process PHP files. First, install PHP with the FPM and CLI packages:

sudo apt install php php-fpm php-cli

Once installed, enable and start PHP-FPM:

sudo systemctl enable php8.2-fpm --now

Remember to replace php8.2-fpm with your Debian’s PHP version: php8.4-fpm on Debian 13, php8.2-fpm on Debian 12, or php7.4-fpm on Debian 11.

Configure Nginx Server Block for PHP-FPM

Next, add the following location block to your Nginx server configuration to process PHP files through PHP-FPM. Use the socket path matching your PHP version:

For Debian 13 (PHP 8.4):

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}

For Debian 12 (PHP 8.2):

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}

For Debian 11 (PHP 7.4):

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

Before applying changes, first validate the Nginx configuration syntax:

sudo nginx -t

Expected output for valid configuration:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Once validation passes, restart Nginx to apply the configuration:

sudo systemctl restart nginx

Verify PHP Installation

Finally, confirm PHP installed correctly by checking the version:

php --version

Expected output on Debian 13:

PHP 8.4.x (cli) (built: ...)
Copyright (c) The PHP Group
Zend Engine v4.4.x, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.x, Copyright (c), by Zend Technologies

Expected output on Debian 12:

PHP 8.2.x (cli) (built: ...)
Copyright (c) The PHP Group
Zend Engine v4.2.x, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.x, Copyright (c), by Zend Technologies

Expected output on Debian 11:

PHP 7.4.x (cli) (built: ...)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.x, Copyright (c), by Zend Technologies

Additionally, if you installed PHP-FPM, verify the service is running. Replace the version number with your PHP version (php8.4-fpm on Debian 13, php8.2-fpm on Debian 12, or php7.4-fpm on Debian 11):

sudo systemctl status php8.4-fpm   # Debian 13
sudo systemctl status php8.2-fpm   # Debian 12
sudo systemctl status php7.4-fpm   # Debian 11

Expected output showing active status (Debian 12 example):

● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled)
     Active: active (running)

Install PHP Extensions on Debian

Install Common PHP Extensions

In most cases, PHP applications require additional extensions beyond the base installation. Therefore, install commonly needed extensions for WordPress with Nginx on Debian, Laravel, and general PHP development.

For Debian 13 (PHP 8.4):

sudo apt install php8.4-{curl,mysql,gd,opcache,zip,intl,common,bcmath,imagick,xmlrpc,readline,memcached,redis,mbstring,apcu,xml}

For Debian 12 (PHP 8.2):

sudo apt install php8.2-{curl,mysql,gd,opcache,zip,intl,common,bcmath,imagick,xmlrpc,readline,memcached,redis,mbstring,apcu,xml}

For Debian 11 (PHP 7.4):

sudo apt install php7.4-{curl,mysql,gd,opcache,zip,intl,common,bcmath,xmlrpc,readline,mbstring,xml,json} php-{imagick,memcached,redis,apcu}

Debian 11 uses unversioned package names for some extensions (php-imagick, php-memcached, php-redis, php-apcu) that automatically install for the default PHP version. JSON support is built into PHP 8.0 and newer, so Debian 12+ has no separate php-json package.

Importantly, after installing extensions, restart PHP-FPM and your web server to load the new modules:

sudo systemctl restart php8.2-fpm   # Replace with your PHP version
sudo systemctl restart nginx        # Or apache2 for Apache users

Extension Reference

For reference, the extensions above provide the following functionality:

  • php-curl: Enables HTTP requests and API communication.
  • php-mysql: MySQL and MariaDB on Debian database connectivity.
  • php-gd: Image manipulation and processing library.
  • php-opcache: Bytecode caching for improved PHP performance.
  • php-zip: ZIP archive compression and extraction.
  • php-intl: Internationalization and locale support.
  • php-bcmath: Arbitrary precision mathematics.
  • php-imagick: Advanced image processing via PHP ImageMagick on Debian.
  • php-xmlrpc: XML-RPC protocol support for remote procedure calls.
  • php-memcached and php-redis: Distributed caching backends for session storage and object caching. See the Redis on Debian and Memcached on Debian guides for server setup.
  • php-mbstring: Multibyte string handling for UTF-8 and international text.
  • php-apcu: User-level opcode caching for application data.
  • php-xml: XML parsing including DOM, SimpleXML, and XSL transformations.

Furthermore, to search for all available extensions for your PHP version, replace the version number with yours (8.4, 8.2, or 7.4):

apt search php8.4-   # Debian 13
apt search php8.2-   # Debian 12
apt search php7.4-   # Debian 11

List Installed PHP Extensions

To verify your setup, view all loaded PHP modules to confirm which extensions are active:

php -m

Example output showing loaded modules:

[PHP Modules]
calendar
Core
ctype
curl
date
exif
fileinfo
filter
gd
hash
iconv
intl
json
libxml
mbstring
mysqli
openssl
pcre
PDO
pdo_mysql
Phar
readline
Reflection
session
SimpleXML
sockets
sodium
SPL
standard
tokenizer
xml
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

Install PHP Development Tools

For advanced PHP development and debugging, you can also install Xdebug and the PHP development headers. Use the commands matching your Debian release:

For Debian 13 (PHP 8.4):

sudo apt install php8.4-xdebug php8.4-dev

For Debian 12 (PHP 8.2):

sudo apt install php8.2-xdebug php8.2-dev

For Debian 11 (PHP 7.4):

sudo apt install php-xdebug php7.4-dev

Debian 11 uses the unversioned php-xdebug package name, while Debian 12 and 13 use versioned names like php8.2-xdebug.

In particular, php-xdebug provides step debugging and profiling for development environments, while php-dev includes headers and tools for compiling PHP extensions from source.

Troubleshoot Common PHP Issues

PHP-FPM Socket Not Found

If Nginx returns a 502 Bad Gateway error, first check the Nginx error log for the actual message:

sudo tail -20 /var/log/nginx/error.log

A socket connection failure appears as:

connect() to unix:/var/run/php/php8.2-fpm.sock failed (2: No such file or directory)

This error occurs when Nginx cannot communicate with PHP-FPM, typically because the PHP-FPM service stopped, failed to start, or the socket path in your Nginx configuration does not match your PHP version.

To diagnose, verify the socket file exists and PHP-FPM is running. Replace the version number with yours (8.4, 8.2, or 7.4):

ls -la /run/php/php8.2-fpm.sock
sudo systemctl status php8.2-fpm

If the service is inactive, start and enable it:

sudo systemctl enable php8.2-fpm --now

Then, verify the service started successfully:

sudo systemctl status php8.2-fpm

Apache Shows PHP Source Code Instead of Executing

If Apache displays PHP code as plain text instead of executing it, the PHP module is not loaded. Typically, this happens when the PHP Apache module was not installed, was disabled, or conflicts with PHP-FPM configuration.

First, check enabled modules:

apache2ctl -M | grep php

If no output appears, enable the PHP module and restart Apache. Replace the version with yours:

sudo a2enmod php8.4   # Debian 13
sudo a2enmod php8.2   # Debian 12
sudo a2enmod php7.4   # Debian 11
sudo systemctl restart apache2

Extension Not Loading

If an extension fails to load, it may not be installed, or PHP-FPM may need a restart to load the new module. To troubleshoot, first verify the extension is installed (replace php8.2-curl with your version and extension):

dpkg -l | grep php8.2-curl

Expected output for an installed extension:

ii  php8.2-curl  8.2.x  amd64  CURL module for PHP

Next, check if the extension appears in the loaded modules list:

php -m | grep -i curl

If the package is installed but not loading, restart PHP-FPM and your web server to reload the configuration:

sudo systemctl restart php8.2-fpm   # Replace with your PHP version
sudo systemctl restart nginx        # Or apache2 for Apache users

Verify the extension now loads:

php -m | grep -i curl

Expected output if the extension loaded successfully:

curl

Remove PHP from Debian

If you need to remove PHP and its extensions, first identify installed PHP packages:

dpkg -l | grep php

Then, remove all PHP packages. Replace the version with yours (8.4, 8.2, or 7.4):

sudo apt remove --purge php8.4*   # Debian 13
sudo apt remove --purge php8.2*   # Debian 12
sudo apt remove --purge php7.4*   # Debian 11

Next, clean up orphaned dependencies that were installed alongside PHP:

sudo apt autoremove

Finally, verify PHP is removed:

php --version

Expected output after successful removal:

-bash: php: command not found

Conclusion

At this point, your Debian system now runs PHP from the default repositories with Apache or Nginx, including extensions for database connectivity, caching, and image processing. For production deployments, install Composer on Debian for dependency management, configure MariaDB on Debian for database storage, and set up SSL certificates (secure Nginx with Let’s Encrypt on Debian or secure Apache with Let’s Encrypt on Debian). Additionally, for web-based database management, see the phpMyAdmin with Nginx on Debian guide.

Leave a Comment