Medha Cloud Logo

How to Install Linux, Apache, MariaDB, PHP (LAMP) Stack on Debian 11

The LAMP stack - a fusion of Linux, Apache, MariaDB, and PHP - is an open-source software stack that empowers developers to create dynamic web applications and websites. This guide will walk you through installing a LAMP stack on a Debian 11 server.


Before you start, ensure you have a Debian 11 server with a non-root user with sudo privileges and a basic firewall setup. If you don't have these, you can configure them using the initial server setup guide for Debian 11.

Step 1 - Installing Linux

Linux forms the operating system layer of the LAMP stack. Debian 11 is a popular version of the Linux operating system and is the one we'll be using for this tutorial. As you install the LAMP stack on a Debian 11 server, the Linux part is already taken care of!

Step 2 - Installing Apache

Apache is a widely used web server software known for its reliability, robustness, and extensive documentation.

Begin by refreshing your package manager cache:

sudo apt update

Next, install Apache using the following command:

sudo apt install apache2

Once the installation is complete, adjust your firewall settings to allow HTTP and HTTPS traffic. Assuming you installed and enabled UFW (Uncomplicated FireWall) following the initial server setup instructions, you can adjust your firewall settings using the following commands:

sudo ufw app list sudo ufw app info "WWW Full" sudo ufw allow in "WWW Full"

You can verify the successful installation of Apache by visiting your server's public IP address in your web browser:


Step 3 - Installing MariaDB

MariaDB is a community-developed fork of MySQL, used as the database management system in the LAMP stack.

To install MariaDB, use the following command:

sudo apt install mariadb-server

After the installation is complete, it's advisable to run a security script that comes with MariaDB. This script will help you secure your database system by removing some insecure default settings:

sudo mysql_secure_installation

To connect to the MariaDB server as the administrative database user root, use the following command:

sudo mariadb

Step 4 - Installing PHP

PHP is a server-side scripting language that processes code to display dynamic content to the end user. To install PHP, as well as the PHP modules that allow PHP to communicate with MySQL-based databases like MariaDB and enable Apache to handle PHP files, use the following command:

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

You can verify the successful installation of PHP by checking its version:

php -v

Step 5 - Creating a Virtual Host for Your Website

When using the Apache web server, you can create virtual hosts to host more than one domain from a single server.

Start by creating the root web directory for your domain:

sudo mkdir /var/www/your_domain

Assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your_domain

Next, open a new configuration file in Apache's sites-available directory:

sudo nano /etc/apache2/sites-available/your_domain.conf

In this new file, add the following configuration:

<VirtualHost *:80>     
ServerName your_domain     
ServerAlias www.your_domain     
ServerAdmin webmaster@localhost     
DocumentRoot /var/www/your_domain     
ErrorLog ${APACHE_LOG_DIR}/error.log     
CustomLog ${APACHE_LOG_DIR}/access.log combined 

Save and close the file when you're done.

Now, enable this virtual host:

sudo a2ensite your_domain

To make sure your configuration file doesn't contain syntax errors, run the following command:

sudo apache2ctl configtest

Finally, reload Apache so these changes take effect:

sudo systemctl reload apache2

Step 6 - Testing PHP Processing on Your Web Server

To test that PHP is correctly installed and configured on your server, create a new PHP file named info.php inside your custom web root folder:

nano /var/www/your_domain/info.php

In this new file, add the following PHP code:

<?php phpinfo(); ?>

Save and close the file when you're done.

To test the script, visit the following URL in your web browser:


If you can see the PHP information page in your browser, then your PHP installation is working as expected.

For security reasons, it's best to remove the info.php file once you've finished testing:

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

Step 7 - Testing Database Connection from PHP (Optional)

If you want to test whether PHP can connect to MariaDB and execute database queries, you can create a test table with test data and query for its contents from a PHP script.

Start by connecting to the MariaDB console using the root account:

sudo mariadb

Create a new database and a new user with full privileges on the custom database you've created:

CREATE DATABASE example_database; CREATE USER 'example_user'@'%' IDENTIFIED BY 'password'; GRANT ALL ON example_database.* TO 'example_user'@'%'; FLUSH PRIVILEGES;

Next, create a test table and insert some content:

CREATE TABLE example_database.todo_list (item_id INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(item_id)); INSERT INTO example_database.todo_list (content) VALUES ("My first important item");

Confirm that the data was successfully saved to your table:

SELECT * FROM example_database.todo_list;

Now, create a PHP script that will connect to MariaDB and query for your content:

nano /var/www/your_domain/todo_list.php

In this new file, add the following PHP code:

<?php $user = "example_user"; $password = "password"; $database = "example_database"; $table = "todo_list";  try {      $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);      echo "<h2>TODO</h2><ol>";      foreach($db->query("SELECT content FROM $table") as $row) {          echo "<li>" . $row['content'] . "</li>";      }      echo "</ol>"; } catch (PDOException $e) {      print "Error!: " . $e->getMessage() . "<br/>";      die(); } ?>

Save and close the file when you're done.

To test the script, visit the following URL in your web browser:


If you can see the content you've inserted in your test table, then your PHP environment is ready to connect and interact with your MariaDB server.


Congratulations! You've successfully installed a LAMP stack on your Debian 11 server. You now have a flexible base for serving PHP websites and applications to your visitors.

As a next step, you should ensure that connections to your web server are secured by serving them via HTTPS. You can use Let's Encrypt to accomplish this.

Finally, for a high-performance, scalable, and reliable VPS hosting solution, consider Medha Cloud VPS With Medha Cloud VPS, you can easily manage and scale your applications and websites, ensuring optimal performance and security. Get started today with Medha Cloud VPS!

Connect with Medha Cloud

Microsoft 365 Migration Form

I would like to send my contact information to MedhaCloud so that MedhaCloud can share additional information about products, services, thought leadership and invitations to flagship events with me by email. *

By submitting this form, I acknowledge that someone from MedhaCloud will contact me via email or phone to discuss my request.

Related Article

View All
chevron-down linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram