How to Install PHP on Debian 12, 11 or 10

PHP is a go-to choice if you’re using Debian and looking to set up a dynamic website or web application. This guide will help you install PHP on Debian 12 Bookworm, Debian 11 Bullseye, or Debian 10 Buster. PHP is a server-side programming language that’s widely used for web development. It’s versatile, easy to integrate with databases, and has a strong community backing.

Here’s why many web developers favor PHP:

  • Server-Side Execution: PHP runs on the server, delivering the final output as HTML to the user’s browser.
  • Works Everywhere: Whether on Windows, Linux, or macOS, PHP is there for you.
  • Database Friendly: Connect PHP with popular databases like MySQL, PostgreSQL, or Oracle to power your web applications.
  • Ready-to-Use Functions: PHP has built-in functions for various tasks, from handling strings to managing files.
  • Strong Community: A vast community of developers continuously contributes to PHP, ensuring it stays updated and efficient.
  • Open and Free: Being open-source, PHP is free to use, modify, and distribute.

To get started with PHP on Debian, this guide will introduce you to the Ondřej Surý Repository, ensuring you get the latest and most secure version of PHP for your needs.

Import PHP APT Repository on Debian 12, 11, or 10

Step 1: Update Debian Before PHP Installation

Ensure all existing packages on your Debian operating system are up to date by updating them with the following command.

sudo apt update

If updates are available, use the command to initiate the upgrade process.

sudo apt upgrade

Step 2: Import Ondřej Surý PHP APT Repository

To follow this tutorial, specific packages need to be installed. Please run the following command to install them.

sudo apt-get install ca-certificates apt-transport-https software-properties-common curl lsb-release -y

The following step is to import and install the GPG key and repository using an automated script, which can be done by running the curl command. In the terminal, execute the following command.

curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x

Step 3: Refresh APT Package Index

Refresh your APT repository list to reflect the changes made in the previous step.

sudo apt update

Specific packages may need updating after running the update command; be sure to update them before proceeding.

sudo apt upgrade

Install PHP on Debian 12, 11 or 10

Option 1: Install PHP with Apache Option

You can run PHP as an Apache module or use PHP-FPM with the Apache HTTP server. To make it easier to manage and keep clean, it is best to install only the version of PHP that you require.

You can execute the following command to install PHP as an Apache module.

sudo apt install php8.3 libapache2-mod-php8.3
sudo apt install php8.2 libapache2-mod-php8.2
sudo apt install php8.1 libapache2-mod-php8.1
sudo apt install php8.0 libapache2-mod-php8.0
sudo apt install php7.4 libapache2-mod-php7.4

Once installation is complete, restart your Apache server for the new PHP module to be loaded.

sudo systemctl restart apache2

Option 2: Install Apache with PHP-FPM

PHP-FPM (FastCGI Process Manager) is a widely used alternative to the standard PHP (Hypertext Processor) FastCGI implementation.

Select the command to correspond to the version of PHP or PHP-FPM with Apache you want to install.

sudo apt install php8.3-fpm libapache2-mod-fcgid
sudo apt install php8.2-fpm libapache2-mod-fcgid
sudo apt install php8.1-fpm libapache2-mod-fcgid
sudo apt install php8.0-fpm libapache2-mod-fcgid
sudo apt install php7.4-fpm libapache2-mod-fcgid

PHP-FPM is not enabled for Apache by default, so you must enable it using the following command:

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php{version}-fpm

It’s important to note that you must replace the {version} section in the above command with your desired PHP version. For example, if you want to use PHP 8.2, the command should be as follows.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.2-fpm

Finally, restart the Apache web server.

sudo systemctl restart apache2

Option 3: Install PHP support for Nginx on Debian Linux

Install PHP & PHP-FPM

Unlike other web servers like Apache, Nginx does not have built-in support for processing PHP files. You must install PHP-FPM (FastCGI Process Manager) to handle PHP files with Nginx.

Choose one the PHP versions below to install PHP and PHP-FPM for Nginx:

sudo apt install php8.3 php8.3-fpm php8.3-cli
sudo apt install php8.2 php8.2-fpm php8.2-cli
sudo apt install php8.1 php8.1-fpm php8.1-cli
sudo apt install php8.0 php8.0-fpm php8.0-cli
sudo apt install php7.4 php7.4-fpm php7.4-cli

After installation, the PHP-FPM service will automatically start running. If this fails to occur, use the following command.

sudo systemctl start php{version}-fpm

Configure Nginx Server Block for PHP-FPM

For Nginx to process PHP files, you will need to edit your Nginx server block and add the following example. This should be added to all server blocks that handle PHP files, specifically the “location ~ .php$.”

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

Replace {version} with your desired PHP version.

You can test it using the following command to ensure no errors with the adjustments made to your Nginx configuration.

sudo nginx -t

It will check the syntax of the configuration files and tell you if there are any issues.

Example output if successful:

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

To finalize the installation, it is necessary to restart the Nginx service.

sudo systemctl restart nginx

As a reminder, you can use the following command to check the PHP version currently installed on your system.

php --version

Example output with PHP 8.2 installed:

Screenshot showing systemctl status of PHP 8.2 on Debian 12, 11 or 10 Linux.
Example “systemctl status” output for PHP 8.2 on Debian 12, 11, or 10 Linux.

Install PHP Extensions on Debian 12, 11, or 10

Customizing PHP for Specific Project Needs

To customize PHP for your project on Debian (versions 12, 11, or 10), select and install the necessary extensions. This process is crucial for enhancing your project’s performance and security.

Command for Installing PHP Extensions

Use the following command as a guide to install PHP 8.3 extensions:

sudo apt install php8.3-{cli,fpm,curl,mysqlnd,gd,opcache,zip,intl,common,bcmath,imagick,xmlrpc,readline,memcached,redis,mbstring,apcu,xml,dom,memcache}

Understanding PHP 8.3 Extensions

  • php-cli: Facilitates running PHP scripts via the command line.
  • php-fpm: Optimizes request handling as a FastCGI Process Manager.
  • php-curl: Enables server communication capabilities.
  • php-mysqlnd: Provides MySQL Native Driver for efficient database interactions.
  • php-gd: A library for image manipulation.
  • php-opcache: Boosts PHP performance by caching precompiled script bytecode.
  • php-zip: Handles operations related to zip files.
  • php-intl: Supports international character sets for global applications.
  • php-common: Delivers functionalities common across different PHP modules.
  • php-bcmath: Manages precise floating-point arithmetic operations.
  • php-imagick: Facilitates image processing using ImageMagick.
  • php-xmlrpc: Offers XML-RPC server and client functionalities.
  • php-readline: Enhances interactive terminal input.
  • php-memcached & php-redis: Improves performance through effective caching strategies.
  • php-mbstring: Manages multibyte character encodings for diverse language support.
  • php-apcu: Provides user caching to enhance PHP application performance.
  • php-xml & php-dom: Essential for XML parsing and manipulation.

Finding More Modules

To discover additional modules, use the terminal search command:

sudo apt search php8.3-

Monitoring Installed PHP 8.3 Modules

It’s important to regularly monitor and manage your PHP modules for optimal system efficiency. To list the loaded PHP modules, execute:

php8.3 -m

Install PHP Development Tools on Debian

For advanced PHP development and debugging, consider installing the following tools:

sudo apt install php8.3-xdebug php8.3-pcov php8.3-dev

Conclusion: Installing PHP on Debian Linux

In conclusion, successfully installing PHP versions 8.3, 8.2, 8.1, 8.0, or 7.4 on Debian 12, 11, or 10 is a critical skill for administrators and developers seeking to leverage the power of this scripting language. The process has been outlined with clear, step-by-step instructions to ensure that even those relatively new to Debian and PHP can navigate the installation successfully. It’s important to remember to select the correct PHP version based on your specific application requirements and compatibility. Regular maintenance and updating are crucial for efficiently operating your PHP installation, and the security implications should not be overlooked.

Leave a Comment


Your Mastodon Instance
Share to...