How to Set Up a LAMP Stack (Linux, Apache, MySQL, PHP) on Debian 12 Bookworm
Categories:
4 minute read
Setting up a LAMP stack — Linux, Apache, MySQL, and PHP — is a fundamental step for anyone looking to host dynamic websites or develop web applications locally. Debian 12 “Bookworm,” being a robust and stable release, is an excellent base for this setup.
In this comprehensive guide, we’ll walk through the process of setting up a LAMP stack on a fresh Debian 12 Bookworm system. Whether you’re a beginner or an experienced developer, this tutorial will provide clear, step-by-step instructions to get your server ready for web development and hosting.
1. Introduction to the LAMP Stack
The LAMP stack is a popular open-source software bundle used to host and develop dynamic web applications. Here’s what each component represents:
- Linux – The operating system (Debian 12 in our case).
- Apache – The web server that handles HTTP requests.
- MySQL – The relational database management system (MariaDB, a MySQL fork, is used by default in Debian).
- PHP – The server-side scripting language for processing scripts and interacting with the database.
2. Prerequisites
Before starting the installation, make sure you have:
- A system running Debian 12 Bookworm.
- Root or sudo access.
- A stable internet connection.
3. Step 1: Update Your Debian 12 System
First, ensure that your system is up to date:
sudo apt update && sudo apt upgrade -y
This command updates your local package index and installs the latest security patches and updates.
4. Step 2: Install Apache Web Server
Apache is one of the most reliable and widely used web servers.
Install Apache
sudo apt install apache2 -y
Enable and Start Apache
sudo systemctl enable apache2
sudo systemctl start apache2
Verify Installation
Open a browser and navigate to your server’s IP address:
http://your_server_ip
You should see the Apache2 Debian Default Page.
5. Step 3: Install MySQL (MariaDB) Database Server
Debian 12 Bookworm uses MariaDB as the default MySQL implementation.
Install MariaDB
sudo apt install mariadb-server mariadb-client -y
Secure MariaDB Installation
sudo mysql_secure_installation
You’ll be prompted to:
- Set a root password (optional depending on your setup).
- Remove anonymous users.
- Disallow root login remotely.
- Remove test databases.
- Reload privilege tables.
Test MySQL/MariaDB Access
sudo mariadb
You should get the MariaDB shell. Type exit;
to leave.
6. Step 4: Install PHP and Required Modules
Debian 12 includes PHP 8.2 in its repository.
Install PHP
sudo apt install php libapache2-mod-php php-mysql -y
Additional Useful PHP Modules
Depending on your application, you may need more modules. Here are some commonly used ones:
sudo apt install php-cli php-curl php-zip php-gd php-mbstring php-xml php-bcmath -y
7. Step 5: Configure Apache to Use PHP
Apache uses mod_php
to process PHP files. After installation, it should be enabled by default.
Set Index Priority to PHP Files
Edit the dir.conf
file:
sudo nano /etc/apache2/mods-enabled/dir.conf
Modify this line:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
To:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Restart Apache to Apply Changes
sudo systemctl restart apache2
8. Step 6: Test the LAMP Stack
Let’s verify if PHP is working with Apache.
Create a PHP Info Page
sudo nano /var/www/html/info.php
Insert the following code:
<?php
phpinfo();
?>
Access the PHP Info Page
In your browser, go to:
http://your_server_ip/info.php
You should see a PHP information page that shows your PHP configuration.
Important: Delete the file afterward to avoid exposing sensitive info.
sudo rm /var/www/html/info.php
9. Optional: Secure Your Server
Set Up UFW Firewall
If you’re running a firewall, allow necessary services:
sudo apt install ufw -y
sudo ufw allow OpenSSH
sudo ufw allow in "Apache Full"
sudo ufw enable
Harden MariaDB
If not already done during mysql_secure_installation
, consider editing the MariaDB configuration and disabling remote root access.
You can also create a dedicated MySQL user with limited privileges for your applications:
CREATE DATABASE example_db;
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
FLUSH PRIVILEGES;
10. Conclusion
Setting up a LAMP stack on Debian 12 Bookworm is a straightforward process and an essential step for hosting web applications or launching development environments. The combination of Linux, Apache, MariaDB, and PHP provides a solid and stable foundation for dynamic websites.
To recap, you’ve:
- Installed and started Apache as your web server.
- Set up MariaDB (MySQL) for database management.
- Installed PHP and linked it with Apache and MariaDB.
- Tested the entire stack to ensure everything is working properly.
From here, you can begin uploading your PHP projects, installing content management systems like WordPress, or building your own web applications from scratch.
Next Steps
Here are a few recommendations to expand your setup:
- Set up virtual hosts if you’re hosting multiple domains.
- Install phpMyAdmin for web-based database management.
- Use SSL certificates with Let’s Encrypt to secure your website.
- Consider automating backups for your web and database files.
- Set up version control with Git for collaborative development.
With your LAMP stack ready, you’re now well-equipped to begin your web development journey on Debian 12 Bookworm.
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.