Easy Guide to Install LAMP Stack on Debian 12 Bookworm
In this guide, we want to teach you how to Install Apache, MySQL or MariaDB, and PHP known as LAMP Stack on Debian 12 Bookworm. The LAMP Stack is a popular and open-source application used for web development. It utilizes Apache as the web server, MySQL or MariaDB as its database management system, and PHP programming languages to create web applications. Follow the steps below on how to Install LAMP Stack on Debian 12 Bookworm.
Before you begin the process to Install LAMP Stack on Debian 12 Bookworm, ensure you have access to your server as a root or non-root user with sudo privileges and have set up a basic firewall. You can refer to the guide on Initial Server Setup with Debian 12 Bookworm for assistance.
Now, follow the steps below to Install LAMP Stack on Debian 12 Bookworm.
Step 1 – How To Install Apache on Debian 12?
First, update your local package index using the following command:
sudo apt update
Then, run the following command in your Debian 12 terminal to install Apache:
sudo apt install apache2
By default, Apache should be started during the installation. You can verify its status using the following command:
sudo systemctl status apache2
**Output**
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enab>
Active: **active** (**running**) since Wed 2023-06-14 10:46:44 EDT; 20s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 5653 (apache2)
Tasks: 55 (limit: 4653)
Memory: 21.2M
CPU: 148ms
CGroup: /system.slice/apache2.service
...
Step 2 – Configure Firewall for Apache on Debian 12
Here, we assume that you have enabled the UFW firewall. You can list UFW’s available applications with the following command:
sudo ufw app list
In your output, you should see something similar to this:
**Output**
Available applications
...
WWW
WWW Cache
WWW Full
WWW Secure
...
To enable HTTP and HTTPS ports 80 and 443, you can use the “WWW Full” option. See the option information with the command below:
sudo ufw app info "WWW Full"
**Output**
Profile: WWW Full
Web Server (HTTP,HTTPS)
Description: Web Server (HTTP,HTTPS)
Ports:
80,443/tcp
Then, allow the ports by using the command below:
sudo ufw allow in "WWW Full"
Reload the firewall to apply the changes:
sudo ufw reload
For more information about the UFW firewall, you can visit the guide on Configure Firewall with UFW on Debian 12 Bookworm.
Step 3 – Access Apache Debian 12 Default Page
At this point, you can test your Apache installation on Debian 12 by accessing the default page. To do this, type your server’s IP address in your web browser:
http://your-server-ip
Note: If you don’t have your IP address, you can use the following command to get it:
hostname -I
You should see the following page:
If you see this page, it means that your Apache web server from the LAMP Stack is up and running on Debian 12.
Step 4 – How To Install MariaDB on Debian 12?
To Install LAMP Stack on Debian 12 Bookworm, we will use MariaDB as our database management system. Debian 12 ships with the latest stable version of MariaDB, which is MariaDB 10.11.
To install MariaDB, you can use the following command:
sudo apt install mariadb-server -y
When your installation is completed, verify your MariaDB service is active and running on your server with the command below:
sudo systemctl status mariadb
**Output**
● mariadb.service - MariaDB 10.11.3 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; preset: enab>
Active: **active** (**running**) since Wed 2023-06-14 11:00:01 EDT; 21s ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 7100 (mariadbd)
Status: "Taking your SQL requests now..."
Tasks: 15 (limit: 4653)
Memory: 202.0M
CPU: 851ms
CGroup: /system.slice/mariadb.service
...
Then, you must run the security script for your MariaDB. To do this, run the command below:
sudo mysql_secure_installation
You will be asked some questions; answer them as shown below and change your MariaDB root password with a strong password:
At this point, you can access your MariaDB shell by using the command below:
sudo mariadb -u root -p
Enter your password, and you should access your shell as shown below:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 39
Server version: 10.11.3-MariaDB-1 Debian 12
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]>
You can exit from your MariaDB shell with the command below:
MariaDB [(none)]> EXIT;
Step 5 – How To Install PHP on Debian 12 Bookworm?
By default, Debian 12 ships with PHP 8.2. You can install it on your server by using the command below:
Also, you’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. You’ll also need libapache2-mod-php to enable Apache to handle PHP files. Core PHP packages will automatically be installed as dependencies.
sudo apt install php libapache2-mod-php php-mysql
Verify your PHP installation by checking its version:
php -v
**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
Now, you can create a PHP test script to confirm that Apache is able to handle and process requests for PHP files on Debian 12:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Then, type your server’s IP address in your web browser followed by info.php:
http://server-ip-address/info.php
Here is an example of the default PHP web page:
After checking the information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your Debian 12 server. To do this, you can run the command below:
sudo rm /var/www/html/info.php
You can always recreate this page if you need to access the information again later.
Conclusion
At this point, you have learned to Install LAMP Stack on Debian 12 Bookworm. The LAMP stack is used for web development. You can easily install it on your Debian 12 and develop your web.
Hope you enjoy it. You may also be interested in these articles:
Install LAMP Stack on Debian 11
Install LAMP Stack on Ubuntu 22.04
Alternative Solutions for Installing a LAMP Stack on Debian 12
While the above guide provides a straightforward method for installing a LAMP stack, alternative approaches exist that offer different advantages. Here are two different ways to solve the problem of setting up a LAMP stack on Debian 12:
1. Using Docker Compose
Docker provides a containerization platform that allows you to package applications and their dependencies into isolated containers. Docker Compose is a tool for defining and running multi-container Docker applications. Using Docker Compose to install a LAMP stack offers several benefits, including reproducibility, isolation, and simplified management.
Explanation:
Instead of installing each component (Apache, MySQL/MariaDB, PHP) directly on the host operating system, Docker Compose allows you to define each component as a separate container in a docker-compose.yml
file. This file specifies the images to use, the ports to expose, the volumes to mount, and other configuration options.
Steps:
-
Install Docker and Docker Compose:
sudo apt update sudo apt install docker.io docker-compose-plugin
-
Create a
docker-compose.yml
file:Create a new directory for your LAMP stack and create a
docker-compose.yml
file inside it.mkdir lamp-docker cd lamp-docker nano docker-compose.yml
-
Add the following content to the
docker-compose.yml
file:version: "3.9" services: db: image: mariadb:10.11 restart: always environment: MYSQL_ROOT_PASSWORD: your_root_password MYSQL_DATABASE: your_database_name MYSQL_USER: your_user_name MYSQL_PASSWORD: your_user_password volumes: - db_data:/var/lib/mysql ports: - "3306:3306" web: image: php:8.2-apache restart: always ports: - "80:80" volumes: - ./html:/var/www/html depends_on: - db environment: PHP_EXTENSION_XDEBUG: 1 # Enable Xdebug (optional) volumes: db_data:
Explanation of the
docker-compose.yml
file:version
: Specifies the Docker Compose file version.services
: Defines the services (containers) to run.db
: Defines the MariaDB container.image
: Specifies the MariaDB image to use from Docker Hub.restart
: Configures the container to restart automatically if it fails.environment
: Sets environment variables for the MariaDB container, including the root password, database name, user name, and user password. Important: Replaceyour_root_password
,your_database_name
,your_user_name
, andyour_user_password
with your desired values.volumes
: Mounts a volume to persist the database data.ports
: Exposes port 3306 on the host to the MariaDB container.
web
: Defines the Apache/PHP container.image
: Specifies the PHP 8.2 with Apache image to use from Docker Hub.restart
: Configures the container to restart automatically if it fails.ports
: Exposes port 80 on the host to the Apache container.volumes
: Mounts the./html
directory on the host to the/var/www/html
directory in the container, allowing you to serve your PHP files.depends_on
: Ensures that the MariaDB container is started before the Apache container.environment
: Sets the environment variable to enable Xdebug if desired.
volumes
: Defines the named volume for persisting the database data.
-
Create an
html
directory:Create an
html
directory in the same directory as thedocker-compose.yml
file. This directory will contain your PHP files.mkdir html
-
Create a test PHP file:
Create an
index.php
file inside thehtml
directory with the following content:<?php phpinfo(); ?>
-
Start the containers:
Run the following command in the directory containing the
docker-compose.yml
file to start the containers:docker compose up -d
This command will download the necessary images and start the containers in detached mode (-d).
-
Access the LAMP stack:
Open your web browser and navigate to
http://localhost
orhttp://your_server_ip
. You should see the PHP info page.
2. Using Tasksel
Tasksel is a Debian/Ubuntu tool that simplifies the installation of multiple related packages as a single "task". It offers a pre-configured LAMP Server task that installs Apache, MySQL/MariaDB, and PHP with a single command.
Explanation:
Tasksel automates the process of selecting and installing the required packages and configuring them to work together. It’s a quick and easy way to get a basic LAMP stack up and running. However, it offers less customization than the manual installation or Docker Compose method.
Steps:
-
Install Tasksel:
If Tasksel is not already installed, install it using the following command:
sudo apt update sudo apt install tasksel
-
Run Tasksel:
Run Tasksel with the following command:
sudo tasksel
-
Select "LAMP Server":
In the Tasksel interface, use the arrow keys to navigate to the "LAMP server" option and press the spacebar to select it. Then, press Enter to start the installation.
-
Set MySQL/MariaDB root password:
During the installation, you will be prompted to set a password for the MySQL/MariaDB root user. Choose a strong password and remember it.
-
Verify the installation:
After the installation is complete, you can verify that the LAMP stack is working by creating a test PHP file in the
/var/www/html
directory, as described in the original guide.
Code Example (for verifying installation):
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Then, access the file in your web browser by navigating to http://your_server_ip/info.php
.
These alternative methods offer different approaches to setting up a LAMP stack on Debian 12, each with its own advantages and disadvantages. Choose the method that best suits your needs and experience level. Remember to secure your LAMP stack after installation by configuring firewalls, setting strong passwords, and keeping your software up to date.