How to Configure PHP with Apache on Debian 12 Bookworm System

This article provides a step-by-step guide on how to configure PHP with Apache on a Debian 12 Bookworm system, including installation, configuration, and testing.

Setting up a server environment with PHP and Apache on a Debian 12 (Bookworm) system is a common yet essential task for web developers, system administrators, and anyone planning to run dynamic websites or web applications. PHP is one of the most widely-used scripting languages for web development, and Apache remains a solid choice as a web server due to its reliability and wide support.

In this guide, we’ll walk through the entire process of installing and configuring PHP with Apache on Debian 12, including the installation of required modules, verification steps, and tips for optimizing your setup.


1. Introduction to PHP and Apache

Apache HTTP Server is one of the oldest and most robust web servers in existence. Combined with PHP, a server-side scripting language, it enables dynamic content delivery, such as WordPress, Drupal, and other content management systems.

Debian 12 “Bookworm” provides stable and tested versions of both Apache and PHP, making it a suitable base for hosting secure and reliable web applications.


2. System Requirements

To follow along with this guide, you’ll need:

  • A system running Debian 12 Bookworm
  • Root or sudo access to the system
  • Basic familiarity with the terminal

3. Step 1: Update Your System

Before installing anything, make sure your package list and installed packages are up to date.

sudo apt update
sudo apt upgrade -y

Updating your system ensures you get the latest security patches and the most recent package versions available in the Debian repositories.


4. Step 2: Install Apache Web Server

First, let’s install Apache if it’s not already installed:

sudo apt install apache2 -y

Enable and start the Apache service:

sudo systemctl enable apache2
sudo systemctl start apache2

To verify that Apache is running, open a browser and visit:

http://your_server_ip/

You should see the default Apache welcome page.


5. Step 3: Install PHP and Required Modules

Debian 12 includes PHP 8.2 by default. You can install it along with common modules using the following command:

sudo apt install php libapache2-mod-php php-cli php-common php-mysql php-gd php-curl php-xml php-mbstring -y

Let’s break down some of these modules:

  • php-cli: Command-line interface for PHP
  • libapache2-mod-php: Allows Apache to process PHP files
  • php-mysql: MySQL database support
  • php-gd: Graphics and image processing
  • php-curl: Data transfer with URLs
  • php-xml: XML parsing
  • php-mbstring: Multibyte string handling

You can install additional PHP modules depending on your application’s needs.

To verify the installed PHP version:

php -v

Output should be something like:

PHP 8.2.x (cli) (built: ...)

6. Step 4: Configure Apache to Use PHP

Apache uses virtual hosts to handle websites. By default, Debian configures Apache to serve from /var/www/html.

To ensure PHP files are properly handled, make sure the php module is enabled:

sudo a2enmod php8.2

Then reload Apache:

sudo systemctl restart apache2

If you ever need to switch PHP versions later, you can re-enable a different module (more on this below).


7. Step 5: Test PHP with Apache

Create a simple PHP file to test the setup:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now, visit:

http://your_server_ip/info.php

You should see the PHP information page, which shows the current PHP configuration, modules, and environment variables. This confirms that PHP is working correctly with Apache.

⚠️ Important: After verifying that PHP is working, it is highly recommended to remove this file for security reasons:

sudo rm /var/www/html/info.php

8. Step 6: Adjusting PHP Settings (Optional)

Depending on your application’s needs, you may want to tweak some PHP settings. The PHP configuration file is located at:

/etc/php/8.2/apache2/php.ini

Edit it with your favorite text editor:

sudo nano /etc/php/8.2/apache2/php.ini

Some common settings you might adjust:

  • upload_max_filesize
  • post_max_size
  • memory_limit
  • max_execution_time
  • date.timezone

For example, to increase the maximum upload size:

upload_max_filesize = 64M
post_max_size = 64M

After making changes, restart Apache to apply:

sudo systemctl restart apache2

9. Step 7: Managing PHP Versions (If Multiple Versions Are Needed)

Sometimes, you might want to use multiple PHP versions side-by-side, especially if you’re hosting different applications.

Debian allows this using update-alternatives and a2dismod/a2enmod commands.

First, check available PHP versions:

sudo update-alternatives --display php

To switch between versions:

sudo update-alternatives --config php

To change the Apache module:

sudo a2dismod php8.2
sudo a2enmod phpX.X
sudo systemctl restart apache2

Replace phpX.X with the version you want to use (like php8.1).

If a version is not available, you can install it from Sury’s PHP repository, a well-known and trusted third-party source for PHP on Debian systems. Be cautious and understand the implications of using third-party sources.


10. Security Tips and Best Practices

  • Keep your system updated: Regularly update PHP and Apache to receive security patches.
  • Disable unnecessary PHP functions: Edit php.ini and use disable_functions to block risky functions like exec, shell_exec, system, etc.
  • Use HTTPS: Configure SSL with Let’s Encrypt or another provider to secure data in transit.
  • Use firewall rules: Ensure only necessary ports (like 80 and 443) are open.
  • Avoid using root for web apps: Set proper user permissions and avoid running your web application as root.
  • Log and monitor: Keep an eye on Apache logs (/var/log/apache2/) and PHP error logs for unusual activity.

11. Conclusion

Setting up PHP with Apache on Debian 12 Bookworm is a straightforward process, thanks to the robust package management system and pre-configured defaults. Whether you’re developing locally or deploying to a production environment, having a clean and efficient LAMP (Linux, Apache, MySQL/MariaDB, PHP) stack is a tried-and-true approach.

With this setup, you’re ready to install CMS platforms like WordPress or build custom PHP applications. If you’re hosting multiple sites or using different PHP versions, Debian makes it easy to configure Apache virtual hosts and switch between environments.

As with any server-side technology, remember that good configuration is only part of the story—ongoing monitoring, security hardening, and performance tuning are equally important.