Install PHP with Apache and Nginx on Debian 12 with Easy Steps

Posted on

Install PHP with Apache and Nginx on Debian 12 with Easy Steps

Install PHP with Apache and Nginx on Debian 12 with Easy Steps

In this guide, we’ll walk you through the process to Install PHP with Apache and Nginx on Debian 12. PHP is a powerful and versatile open-source, server-side scripting language. It’s widely used for web development, allowing you to create dynamic websites, web applications, and much more. Debian 12 comes equipped with the latest stable release of PHP, currently PHP 8.2. This guide, brought to you by Orcacore, will provide you with the steps needed to install PHP and configure it to work seamlessly with both Apache and Nginx web servers on your Debian 12 system. Let’s Install PHP with Apache and Nginx on Debian 12.

Before you begin the process to Install PHP with Apache and Nginx on Debian 12, ensure you have the necessary prerequisites in place. You should have access to a Debian 12 server as a non-root user with sudo privileges. If you haven’t already set this up, refer to the Initial Server Setup with Debian 12 Bookworm guide for detailed instructions.

Now, let’s dive into the step-by-step instructions to Install PHP with Apache and Nginx on Debian 12:

Step 1 – Update and Upgrade Debian 12 APT Repository

To ensure you have the latest package information, it’s crucial to update and upgrade your Debian 12’s APT repository before installing any new software. Execute the following commands in your terminal:

# sudo apt update
# sudo apt upgrade -y

These commands will refresh the package lists and upgrade any outdated packages on your system, ensuring a smooth installation process for PHP and its related modules.

Step 2 – Install PHP with Apache Module on Debian 12

Debian 12 comes pre-packaged with PHP 8.2, the latest stable release. To install PHP and its corresponding Apache module, run the following command:

sudo apt install php libapache2-mod-php8.2 -y

This command will install PHP 8.2 along with the libapache2-mod-php8.2 module, which allows Apache to process PHP files.

After the installation is complete, restart Apache to activate the changes:

sudo systemctl restart apache2

To install PHP-FPM, you can use the following command:

sudo apt install php8.2-fpm libapache2-mod-fcgid -y

Enable PHP-FPM for Apache on Debian 12

  • Note: By default, PHP-FPM isn’t enabled for Apache. Enable it using the following commands:
# sudo a2enmod proxy_fcgi setenvif
# sudo a2enconf php8.2-fpm

Restart Apache again to apply these changes:

sudo systemctl restart apache2

Verify that the PHP-FPM service is active and running:

sudo systemctl status php8.2-fpm
**Output**
● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled; preset: e>
     Active: **active** (**running**) since Tue 2023-06-20 06:58:53 EDT; 38s ago
       Docs: man:php-fpm8.2(8)
    Process: 8349 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run>
   Main PID: 8346 (php-fpm8.2)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 4653)
     Memory: 9.3M
        CPU: 94ms
     CGroup: /system.slice/php8.2-fpm.service
...

You can also verify the PHP installation by checking its version:

php --version
**Output**
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

Step 3 – Install PHP with Nginx Module on Debian 12

Unlike Apache, Nginx doesn’t have built-in PHP processing capabilities. To enable PHP support with Nginx, you need to install PHP-FPM (FastCGI Process Manager), which will handle the execution of PHP files.

Install PHP, PHP-FPM, and the PHP command-line interface (CLI) using the following command:

sudo apt install php php8.2-fpm php8.2-cli -y

PHP-FPM should start automatically after the installation is finished. Verify its status using the following command:

sudo systemctl status php8.2-fpm
**Output**
● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled; preset: en>
     Active: **active** (**running**) since Tue 2023-06-20 07:02:34 EDT; 4s ago
       Docs: man:php-fpm8.2(8)
    Process: 12188 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run>
   Main PID: 12185 (php-fpm8.2)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 4653)
     Memory: 9.3M
        CPU: 78ms
     CGroup: /system.slice/php8.2-fpm.service
...

Now, you need to configure your Nginx server block to properly process PHP files. Edit your Nginx server block configuration file (usually located in /etc/nginx/sites-available/) and add the following block within the server block:

server {
    # ... other configurations ...

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

    # ... other configurations ...
}

This location block tells Nginx to pass any requests for files ending in .php to the PHP-FPM service. The include snippets/fastcgi-php.conf; directive includes a pre-defined configuration snippet that handles the necessary FastCGI parameters.

Before restarting Nginx, check for any syntax errors in your configuration:

sudo nginx -t
**Output**
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the configuration test is successful, restart Nginx to apply the changes:

sudo systemctl restart nginx

Verify the PHP installation again by checking the version:

php --version
verify that PHP is installed on your Debian 12
Install PHP with Apache and Nginx on Debian 12

For more information and advanced configuration options, refer to the official PHP Documentation.

Conclusion

PHP is an essential language for web development, and this guide showed you how to Install PHP with Apache and Nginx on Debian 12. You now have a functional PHP environment ready to build dynamic web applications on your Debian 12 server. We hope you found this tutorial helpful. You might also be interested in these articles:

How To Install Java with Apt on Debian 12 Bookworm

How To Install Grafana on Debian 12 Bookworm

Alternative Solutions for Installing PHP on Debian 12

While the above method provides a straightforward approach to installing PHP using the default Debian repositories, there are alternative methods that can offer different benefits, such as access to more recent PHP versions or greater control over the installation process.

1. Using Ondřej Surý’s PHP Repository

Ondřej Surý maintains a popular and well-regarded PHP repository that provides more recent PHP versions than the default Debian repositories. This is particularly useful if you require a specific PHP version or want to stay up-to-date with the latest features and security patches.

First, install the necessary dependencies:

sudo apt install apt-transport-https lsb-release ca-certificates wget -y

Then, add the repository key:

wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Next, add the repository to your APT sources. Determine your Debian version’s codename (Bookworm is Debian 12) and execute the following command (replacing bookworm if necessary):

echo "deb https://packages.sury.org/php/ bookworm main" | sudo tee /etc/apt/sources.list.d/php.list

Update the APT package list:

sudo apt update

Now, you can install specific PHP versions (e.g., PHP 8.3):

sudo apt install php8.3 php8.3-fpm libapache2-mod-php8.3 php8.3-cli -y

The rest of the configuration for Apache or Nginx remains largely the same as outlined in the original guide, just remember to adjust the PHP version numbers (e.g., php8.3-fpm.sock instead of php8.2-fpm.sock).

2. Compiling PHP from Source

This method offers the greatest control over the PHP installation process. You can customize compile-time options, choose specific extensions, and optimize PHP for your specific hardware. However, it’s also the most complex and time-consuming method.

First, install the necessary build dependencies:

sudo apt install build-essential autoconf libtool pkg-config re2c bison flex libxml2-dev libbz2-dev libpng-dev libjpeg-dev libfreetype6-dev libgmp-dev libxslt-dev libldap2-dev libicu-dev libpspell-dev libenchant-dev -y

Download the PHP source code from the official PHP website (https://www.php.net/downloads). Choose the source code package for the version you want to install.

Extract the downloaded archive:

tar -xf php-8.2.7.tar.gz
cd php-8.2.7

Configure the build using the ./configure script. This is where you can specify various options and extensions. Here’s an example configuration:

./configure --with-apxs2=/usr/bin/apxs2 --with-mysqli --with-pdo-mysql --with-openssl --enable-mbstring --enable-soap --enable-exif --with-gd --with-zlib --with-curl
  • --with-apxs2=/usr/bin/apxs2: Enables Apache module support.
  • --with-mysqli, --with-pdo-mysql: Enables MySQL support.
  • --with-openssl: Enables OpenSSL support.
  • --enable-mbstring: Enables multi-byte string support.
  • --enable-soap: Enables SOAP support.
  • --enable-exif: Enables EXIF support.
  • --with-gd: Enables GD library support for image manipulation.
  • --with-zlib: Enables Zlib compression support.
  • --with-curl: Enables cURL support.

Adjust these options based on your specific requirements.

Compile the source code:

make

Install PHP:

sudo make install

After installation, you’ll need to manually configure Apache or Nginx to use the newly compiled PHP. This involves updating configuration files and ensuring that the necessary modules are enabled. This method requires a deeper understanding of server configuration and is best suited for experienced users.

These alternative methods offer different approaches to installing PHP on Debian 12, allowing you to choose the method that best suits your needs and technical expertise. Remember to carefully consider the trade-offs between ease of use, control, and access to specific PHP versions before making your decision.

Leave a Reply

Your email address will not be published. Required fields are marked *