How to Create a Basic PHP Site on Debian 12 Bookworm System
Categories:
4 minute read
Setting up a basic PHP site on Debian 12 (Bookworm) is a great way to learn web development and server management. Debian is known for its stability and security, making it a solid foundation for running PHP-based websites. In this article, we will walk you through the entire process of installing the necessary packages, configuring your server, and deploying a simple PHP web page. By the end, you’ll have a fully functional local web server ready to serve PHP content.
1. Introduction
PHP (Hypertext Preprocessor) is a widely-used open-source scripting language suited for web development. Apache, on the other hand, is a robust, open-source HTTP server. Combined, they form the backbone of many popular websites. In this tutorial, we will be using Debian 12 Bookworm, the latest stable release of the Debian operating system.
2. Prerequisites
Before we begin, ensure you have:
- A Debian 12 Bookworm system (bare metal or virtual machine)
- A non-root user with
sudo
privileges - Internet connectivity for downloading packages
3. Step 1: Update the System
It’s always good practice to update your system before installing new packages.
sudo apt update && sudo apt upgrade -y
This ensures that all packages are up to date and you are installing the latest versions from the Debian repositories.
4. Step 2: Install Apache Web Server
Apache is the most widely used web server in the world. You can install it using the apt
package manager.
sudo apt install apache2 -y
Once installed, you can check if Apache is running:
sudo systemctl status apache2
You should see an output indicating the service is active (running).
To verify Apache installation, open a web browser and navigate to:
http://your_server_ip/
You should see the Apache2 Debian Default Page.
5. Step 3: Install PHP
To enable PHP processing on your server, install PHP along with some commonly used modules.
sudo apt install php libapache2-mod-php php-mysql -y
You can verify the PHP installation with:
php -v
This should return the PHP version number (e.g., PHP 8.2.0).
6. Step 4: Install MariaDB (Optional)
If your PHP site needs a database, installing MariaDB is a good option.
sudo apt install mariadb-server mariadb-client -y
After installation, secure the database server:
sudo mysql_secure_installation
You’ll be prompted to configure root password security, remove anonymous users, disallow remote root login, and remove test databases.
You can log into MariaDB with:
sudo mariadb
And exit with:
exit;
7. Step 5: Configure Apache for PHP
By default, Apache should now use PHP to process .php
files. To ensure that Apache gives priority to PHP files over HTML when loading directory indexes, you can modify the dir.conf
file:
sudo nano /etc/apache2/mods-enabled/dir.conf
Make sure it looks like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save the file and restart Apache:
sudo systemctl restart apache2
8. Step 6: Create a Basic PHP Site
Let’s create a simple PHP file to test your server.
Navigate to Apache’s web root directory:
cd /var/www/html
Remove the default index file:
sudo rm index.html
Create a new index.php
file:
sudo nano index.php
Add the following content:
<?php
echo "<h1>Hello from Debian 12 + Apache + PHP!</h1>";
phpinfo();
?>
This script displays a simple greeting and PHP configuration details.
9. Step 7: Test Your PHP Site
Open your web browser and go to:
http://your_server_ip/
You should now see your greeting message along with a full PHP configuration page, including loaded modules and settings.
If you see this, congratulations! Your PHP server is working.
10. Step 8: Managing Services
It’s important to know how to control your services:
Start Apache:
sudo systemctl start apache2
Stop Apache:
sudo systemctl stop apache2
Restart Apache:
sudo systemctl restart apache2
Enable on Boot:
sudo systemctl enable apache2
Same applies for MariaDB:
sudo systemctl start mariadb
sudo systemctl enable mariadb
11. Security Tips
Here are a few security practices to consider once your basic setup is done:
Use UFW to control access:
sudo apt install ufw sudo ufw allow 'Apache Full' sudo ufw enable
Avoid displaying
phpinfo()
in production, as it leaks configuration information.Regularly update your server and PHP packages:
sudo apt update && sudo apt upgrade
Disable unnecessary PHP modules to reduce attack surface:
php -m # to list modules sudo phpdismod module_name sudo systemctl restart apache2
Create a Virtual Host configuration if you plan to host multiple sites on the same server.
12. Conclusion
By following this guide, you now have a working PHP environment running on Debian 12 Bookworm. You’ve installed Apache, PHP, and optionally MariaDB, configured them to work together, and tested everything with a basic script.
This setup is ideal for personal projects, learning environments, and lightweight web applications. As you grow more comfortable, you can expand this foundation by adding tools like:
- phpMyAdmin for managing databases through a browser
- Let’s Encrypt for SSL/TLS support
- Composer for PHP dependency management
- Virtual Hosts for multi-site configurations
Debian provides a solid and secure environment, and now that you’ve got the basics down, you’re well on your way to becoming a proficient server administrator or web developer.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.