Install LAMP Stack on Debian 12

The LAMP stack, an acronym for Linux, Apache, MySQL, and PHP, is a powerful and widely used platform for web development. Debian 12, known for its stability and security, is an excellent choice for deploying a LAMP stack.

In this article, we will cover the steps to install and configure each component of the LAMP stack on a Debian 12 server.

Prerequisites

Before we begin to install LAMP Debian 12, make sure you have the following:

  1. A server running Debian 12 (Bookworm).
  2. A non-root user with sudo privileges.
  3. Basic knowledge of Linux commands.

How to Install LAMP Stack on Debian 12

Following are the steps described below to install LAMP Stack Debian 12 server:

Step 1: Update System

The first step is to update the package index and upgrade installed package list to the latest versions. Open a terminal and run the following commands:

sudo apt update && sudo apt upgrade -y
update and upgrade

These commands will update your package list and upgrade all installed packages to their latest versions.

Step 2: Install Apache

Apache is a robust and popular web server. To install Apache, run the following command:

sudo apt install apache2 -y
install apache2

Once the installation is complete, enable Apache to start on boot and start the Apache service:

sudo systemctl enable apache2 && sudo systemctl start apache2
start apache2

You can verify that Apache is running by visiting your server’s IP address in a web browser. You should see the default Apache welcome page.

apache2 default page

Step 3: Install MySQL

MySQL database is a powerful relational database management system. The MySQL server package is not available in the default APT repositories on Debian 12. Download the MySQL repository using wget command:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
wget mysql server

Install the MySQL repository using the file:

sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb
mysql config
  • Select MySQL Server and Cluster and press Enter.
  • Select your desired MySQL server version.
  • Press Down then select Ok and press Enter to apply the changes on the MySQL repository.

Now install MySQL, first update the repository then run the following command:

sudo apt install mysql-server -y
install mysql server

After the installation is complete, secure your MySQL installation by running the security script:

sudo mysql_secure_installation

This script will guide you through a series of prompts to improve the security of your MySQL installation. It’s recommended to answer “yes” (y) to all the prompts.

Step 4: Install PHP

PHP is a widely-used scripting language, particularly suited for web development. To install PHP along with some common PHP modules, run the following command:

sudo apt install php libapache2-mod-php php-mysql -y
install php

After the installation is complete, you can verify the PHP version installed by running:

php -v
php version

Step 5: Configure Apache to Use PHP

By default, Apache serves HTML files first. To make Apache process PHP files first, modify the dir.conf file:

sudo nano /etc/apache2/mods-enabled/dir.conf

Move the index.php directive to be the first in the list:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Save the file and exit the text editor. Then, restart Apache command to apply the changes.

Step 6: Test PHP Processing

To ensure PHP is correctly installed and configured, create a info.php file in the web root directory:

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

Add the following PHP code to the file:

<?php
phpinfo();
?>

Save the file and exit the text editor. Now, visit http://your_server_ip/info.php in a web browser. You should see the PHP information page, which confirms that PHP is working correctly with Apache.

php build

Step 7: Create MySQL Database

To create a database for your web application, log in to the MySQL root user:

sudo mysql -u root -p

You will be prompted for the MySQL root password. After entering the password, you’ll be at the MySQL prompt. Create a new database and user, and grant the user privileges on the database:

CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace your_database_name, your_username, and your_password with your preferred names and password.

Step 8: Enable Mod Rewrite

Apache’s mod_rewrite module is required for many web applications to handle URLs. To enable it, run:

sudo a2enmod rewrite

Then, restart Apache server with above described command.

Conclusion

You have successfully installed and configured the LAMP stack on your Debian 12 server. With Apache, MySQL, and PHP up and running, you are now ready to deploy your web applications. Whether you’re setting up a blog, an online store, or a complex web application, the LAMP stack provides a robust and scalable platform for your projects.

At Ultahost, we are committed to providing our customers with the best possible service and support and always working to improve our offerings. Rent a VPS from Ultahost that scales with your resources to accommodate your growing demands. Explore our VPS plans to find the perfect option for you.

FAQ

What is a LAMP stack?
Why install the LAMP stack on Debian 12?
How do I install Apache on Debian 12?
How do I install MySQL on Debian 12?
How do I install PHP on Debian 12?
How do I test if my LAMP stack is working?
Can I use alternatives to MySQL in a LAMP stack?

Related Post

How to fix “530 Login Authentication Fa

FTP or File Transfer Protocol is a standard network pro...

How to Install OWASP ZAP in Kali Linux

For ethical hackers and security enthusiasts, Kali Linu...

Install MERN Stack on Debian 12

Installing the MERN stack on Debian 12 helps us set up ...

How To Install and Upgrade PIP To The Latest

Pip is a package-management system written in Python an...

How to Override the rpm.versions System in cP

The rpm.versions file in cPanel serves as an important ...

How to Delete Files and Directories on Linux

To delete files and directories in Linux you can use th...

Leave a Comment