---
slug: Installing and Configuring LAMP Stack on Debian 10 A Comprehensive Tutorial
title: Installing and Configuring LAMP Stack on Debian 10 A Comprehensive Tutorial
authors: [slorber, yangshun]
tags: [hola, docusaurus]
---

<!-- truncate -->

import CodeBlock from '@site/src/components/CodeBloack';
import debian from '@site/static/img/debian.png';
import todo from '@site/static/img/todo_list.png';

<div className="head">Introduction</div>

<div className="text">A LAMP stack is a group of computer programs that work together to run websites and apps smoothly. LAMP stands for:</div>
<div className="text"><ol><li>L for Linux: This is the computer's operating system.</li><li>A for Apache: Apache helps manage the flow of information on the internet.</li><li>M for MariaDB: This is like a big, organized file cabinet where important information for our website is kept.</li><li>P for PHP: PHP takes care of things behind the scenes, making sure everything works smoothly.</li></ol></div>

<div className="text">Normally, people use MySQL as the file cabinet, but we're using MariaDB because it's like an upgraded version.In this guide, we'll show you how to set up this group of programs on a Debian 10 computer. It's like assembling your own team for a website!</div><br/>

<div className="head">Requirements</div>

<div className="text">Before we start, make sure you have a computer running Debian 10. You'll need a special account that lets you do important things (it's called 'sudo-enabled'), and there should be a basic protective barrier called a firewall. If you haven't set up your computer yet, we have a simple guide to help you do that. Once it's ready, you're good to go with the next steps!</div><br/>

<div className="head">Step 1 — Set up Apache and update the firewall</div>
<div className="text">Let's begin by installing Apache, a popular web server. It's like a traffic director for your website, making sure everything goes smoothly.</div><br/>

<div className="text">To start, we'll make sure our computer knows about the latest stuff available. It's a bit like updating your apps on your phone. If it's the first time you're doing something important, the computer might ask for your password just to make sure it's really you making the decisions. Once that's done, we're ready for the next steps!</div>

<CodeBlock code={`sudo apt update`} /><br/>

<div className="text">Now, let's install Apache. Just follow these steps:</div>

<CodeBlock code={`sudo apt install apache2`} /><br/>

<div className="text">After installing Apache, you might be asked to confirm by pressing 'Y' and then hitting 'ENTER'. Once that's done, let's make sure your computer's firewall is set up right.</div><br/>

<div className="text">If you used the UFW firewall following our guide, we'll check a few things. Think of UFW like a protector for your computer. We want it to allow good website traffic.</div><br/>

<div className="text">To do that, look at a list of rules it already knows. Follow these steps:</div>

<CodeBlock code={`sudo ufw app list`} /><br/>

<CodeBlock code={`Output
Available applications:
. . .
  WWW
  WWW Cache
  WWW Full
  WWW Secure
. . .
`} /><br/>

<div className="text">"If you check the WWW Full profile, it's like looking at your website's settings. It allows traffic on the important ports, like port 80 and 443:</div>

<CodeBlock code={`sudo ufw app info "WWW Full"`} /><br/>

<CodeBlock code={`Output
Profile: WWW Full
Title: Web Server (HTTP,HTTPS)
Description: Web Server (HTTP,HTTPS)
Ports:
  80,443/tcp
`} /><br/>

<div className="text">Let's make sure your website can accept visitors. Allow incoming traffic for your website by following these steps:</div>

<CodeBlock code={`sudo ufw allow in "WWW Full"`} /><br/>

<div className="text">To check if everything worked, open your web browser and visit your server's public address. It's like looking at your website to make sure it's up and running.</div>

<CodeBlock code={`http://your_server_ip`} /><br/>

<div className="text">You'll see a page that says 'Apache2 Debian Default Page.' This page is just for testing and giving you information:</div><br/>

<img src={debian} alt="Debain" /> <br/>

<div className="text">If you see a page that says 'Apache2 Debian Default Page,' then your website is set up correctly, and people can visit it through the internet.</div><br/>

<div className="text" style={{ fontSize:'28px' }}>The best way to find your server's IP address</div>

<div className="text">Finding your server's public IP address is like figuring out its location on the internet. This is the address you use to connect to your server. <br/> <br/> Here's a simple way to find it:" <br/> <br/> If you don't know your server's public address, it's like not knowing the address of your friend's house. But don't worry, there are easy ways to find it. One way is to use the following command:</div>

<CodeBlock code={`ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'`} /><br/>

<div className="text">When you run the command, you'll see two or three lines. All of them are correct addresses, but your computer might use only one. You can try each one to see which works.</div><br/>

<div className="text">Another way is to use a tool called curl to check your server's address. It's like using a special tool to find out the address.</div><br/>

<div className="text">Since Debian 10 doesn't have curl installed by default, you'll need to add it first:</div>

<CodeBlock code={`sudo apt install curl`} /><br/>

<div className="text">After that, use this command to ask a specific server what your IP address is:</div>

<CodeBlock code={`curl http://icanhazip.com`} /><br/>

<div className="text">No matter which way you found your IP address, type it into your web browser to make sure your server is showing the default Apache page.</div><br/>

<div className="head">Step 2 — Setting up MariaDB</div>
<div className="text">Now that your website has a web server running, it's time to set up a storage space for all its data. This is like installing a big filing cabinet for your website.</div><br/>

<div className="text">In Debian 10, we use something called 'default-mysql-server' to set up this filing cabinet. It's like a special piece of furniture that helps organize and manage all the information your website needs. This 'default-mysql-server' actually points to MariaDB, a type of database system that works a lot like the original MySQL. Let's get this filing cabinet installed!</div><br/>

<div className="text">For better compatibility in the long run, it's suggested to install MariaDB using a specific program called 'mariadb-server' instead of a general one.</div><br/>

<div className="text">To do this, just run the following command:</div>

<CodeBlock code={`sudo apt install mariadb-server`} /><br/>

<div className="text">After the installation is done, it's a good idea to run a special security script that comes with MariaDB. This script helps make sure everything is safe and secure by fixing some default settings and restricting access to your database system.To run this helpful script, just enter the following command:</div><br/>

<CodeBlock code={`sudo mysql_secure_installation`} /><br/>

<div className="text">This script will ask you a few questions to make sure your MariaDB is set up securely. The first question is about the 'database root password.' It's not the same as the computer's main password. Think of it as a special key for your database. Since you just installed MariaDB and haven't set up any passwords yet, just press 'ENTER' when it asks for the password.</div><br/>

<div className="text">The next question is about setting up a special password for the 'database root.' However, MariaDB has a safer way to handle this, so you don't have to set a password right now. Just type 'N' and then press 'ENTER'.</div><br/>

<div className="text">After that, you can just press 'Y' and then 'ENTER' to accept the default options for the rest of the questions. This will remove some unnecessary settings, make sure remote login is disabled, and apply the changes immediately.
Once that's done, you can log in to the MariaDB console."</div>

<CodeBlock code={`sudo mariadb`} /><br/>

<div className="text">This will connect you to the MariaDB server as the main database user called 'root.' This happens because of the way we're running the command. You should see the following message:</div><br/>

<CodeBlock code={`Output
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.3.36-MariaDB-0+deb10u2 Debian 10
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)]>
`} /><br/>

<div className="text">You probably noticed that you didn't have to type a password to log in as the main database user ('root'). This happens because of a special way the database checks who you are. This might seem strange, but it actually makes things more secure. Only people with special access (like you, using the console or certain applications) can log in as the main 'root' user. So, if you're using a regular program or your website, you won't use this main 'root' user. It's like having different keys for different doors, and your website has its own key.</div><br/>

<div className="text">For better security, it's a good idea to create special user accounts for each database, especially if you're going to have more than one on your server.</div><br/>

<div className="text">If you want to leave the MariaDB console, just follow these steps:</div>

<CodeBlock code={`exit`} /><br/>

<div className="text">Your MariaDB server is now installed and safe. Next up, you'll install PHP, which is the last part of the LAMP stack.</div><br/>

<div className="head">Step 3 — Adding PHP to the server</div>
<div className="text">You've got Apache serving your website, and MariaDB handling all your data. Now, let's introduce PHP - it makes your site do cool things. It can run special scripts, talk to your MariaDB database for info, and then show the final result on your website.</div><br/>

<div className="text">Along with the main PHP package, we need two more things to make everything work smoothly. One is called 'php-mysql,' which helps PHP talk to the database. The other is 'libapache2-mod-php,' which makes sure Apache can handle PHP files properly. Don't worry, the necessary basic PHP stuff will be installed automatically.</div><br/>

<div className="text">To get all these, just run this command:</div>

<CodeBlock code={`sudo apt install php libapache2-mod-php php-mysql`} /><br/>

<div className="text">After everything is installed, you can check which version of PHP you have by using this command:</div>

<CodeBlock code={`php -v`} /><br/>

<CodeBlock code={`Output
PHP 7.3.31-1~deb10u2 (cli) (built: Dec 15 2022 09:39:10) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.31, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.31-1~deb10u2, Copyright (c) 1999-2018, by Zend Technologies
`} /><br/>

<div className="text">Usually, when someone asks for a folder on your website, Apache looks for a file called index.html. To make it look for PHP files first, we need to tweak a setting.</div><br/>

<div className="text">Just follow these steps: Run the following command to open a file called dir.conf. We'll use a simple text editor called nano for this</div>

<CodeBlock code={`sudo nano /etc/apache2/mods-enabled/dir.conf`} /><br/>

<div className="text">Inside the file, you'll see something like this:</div>

<CodeBlock code={`<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
`} /><br/>

<div className="text">After you've made your changes, save and close the file. If you're using nano, just press CTRL+X, then Y, and finally, press ENTER to confirm.</div><br/>

<div className="text">Now, let's make sure Apache knows about the changes. To do that, follow these steps:</div>

<CodeBlock code={`sudo systemctl reload apache2`} /><br/>

<div className="text">To see how Apache is doing, you can use this command:</div>

<CodeBlock code={`sudo systemctl status apache2`} /><br/>

<CodeBlock code={`Sample Output
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset:
   Active: active (running) since Fri 2023-01-20 22:21:24 UTC; 2min 12s ago
     Docs: https://httpd.apache.org/docs/2.4/
  Process: 13076 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCC
  Process: 13097 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/
 Main PID: 13080 (apache2)
    Tasks: 6 (limit: 4915)
   Memory: 13.7M
   CGroup: /system.slice/apache2.service
           ├─13080 /usr/sbin/apache2 -k start
           ├─13101 /usr/sbin/apache2 -k start
           ├─13102 /usr/sbin/apache2 -k start
           ├─13103 /usr/sbin/apache2 -k start
           ├─13104 /usr/sbin/apache2 -k start
           └─13105 /usr/sbin/apache2 -k start
`} /><br/>

<div className="text">Now, your LAMP stack is ready to go, but before you try out a PHP script, let's organize your website's files and folders properly. We'll do this by setting up something called an 'Apache Virtual Host.' It's like creating a dedicated space for your website, and we'll do that in the next step.</div><br/>

<div className="head">Step 4 — Creating a Virtual Host for your Website</div>

<div className="text">Alright, so with the Apache web server, you can create something called a 'Virtual Host.' Think of it like having different rooms for different websites on the same server. In this step, we'll set up a 'room' for your website, and we'll call it a 'Virtual Host.' We'll use a pretend domain name called 'your_domain,' but you'll replace this with your actual domain name.</div><br/>

<div className="note"><strong>Note</strong>:  If you're using Cloudtopiaa for your website's address (DNS), you can find detailed instructions in our guides on how to set up a new domain name and link it to your server. It's like telling the internet where to find your website.</div><br/>

<div className="text">By default, Apache puts its website stuff in a folder at /var/www/html and follows the rules in a file at /etc/apache2/sites-available/000-default.conf. Instead of changing these default settings, let's make a special setup just for testing your PHP setup. We're going to create a new 'Virtual Host,' which allows Apache to handle multiple websites on the same server. We'll also organize the files for your pretend website in a special folder inside /var/www, leaving the default stuff at /var/www/html for now. It's like having different places for different websites, and we're creating one for your practice site.</div><br/>

<div className="text">Start by making the main folder for your website, 'your_domain,' like this:</div>

<CodeBlock code={`sudo mkdir /var/www/your_domain`} /><br/>

<div className="text">Now, let's make sure your website folder is owned by you. You can do this by running the following command, which uses a special code ($USER) to refer to your user account:</div>

<CodeBlock code={`sudo chown -R $USER:$USER /var/www/your_domain`} /><br/>

<div className="text">After that, let's open a new settings file for your website in a special place called 'sites-available' in Apache. We'll use a simple text editor called nano for this. Here's how:</div>

<CodeBlock code={`sudo nano /etc/apache2/sites-available/your_domain.conf`} /><br/>

<div className="text">Now that you have a new file, let's add some basic settings to it. Copy and paste the following simple configuration, but don't forget to replace 'your_domain' with your actual domain name:</div>

<CodeBlock code={`<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
</VirtualHost>
`} /><br/>

<div className="text">With this configuration, Apache will use the folder '/var/www/your_domain' to show your website when someone visits 'your_domain'.If you're just testing Apache without a real domain name, you can skip this step. Just put a '#' in front of 'ServerName' and 'ServerAlias' lines to ignore them.</div><br/>

<div className="text">Once you're finished, save and close the file.</div><br/>

<div className="text">Now, let's tell Apache to start using this setup by running this command:</div>

<CodeBlock code={`sudo a2ensite your_domain`} /><br/>

<div className="text">If you're not using a custom domain name and want to make sure your new setup works, it's a good idea to turn off the default website that comes with Apache. This step is necessary to avoid any conflicts with your own setup. To do this, just run the following command:</div>

<CodeBlock code={`sudo a2dissite 000-default`} /><br/>

<div className="text">To check if you didn't make any mistakes in your configuration file, run the following command:</div>

<CodeBlock code={`sudo apache2ctl configtest`} /><br/>

<div className="text">Now, let's make sure Apache knows about the changes you made. To do that, run the following command:</div>

<CodeBlock code={`sudo systemctl reload apache2`} /><br/>

<div className="text">Now, let's make a simple PHP script to check if PHP is working correctly on your server.</div><br/>

<div className="head">Step 5 — Verifying PHP processing on your server</div>

<div className="text">Now that you've set up a location for your website, let's check if everything is working by creating a simple PHP test script.<br/><br/>
First, make a new file called 'info.php' in your website's folder:</div>

<CodeBlock code={`nano /var/www/your_domain/info.php`} /><br/>

<div className="text">This will open a new empty file. Copy and paste the following text into the file. Don't worry, it's just some code that PHP understands:</div>

<CodeBlock code={`<?php
phpinfo();
`} /><br/>

<div className="text">After you've pasted the code, save and close the file.<br/><br/>
Now, let's check if everything is working. Open your web browser and type in your server's address or IP, followed by the script name 'info.php':</div>

<CodeBlock code={`http://your_domain/info.php`} /><br/>

<div className="text">Here's an example of what the basic PHP web page looks like:</div>

<!-- <need add a image> -->

<div className="text">This page shows some basic details about your server from the PHP perspective. It's handy for checking if everything is set up correctly.</div><br/>

<div className="text">If you see this page in your browser, it means your PHP installation is good to go.<br/><br/>
After checking the info, it's a good idea to delete the file you created because it has sensitive information about your PHP setup and your Debian server. You can do this by running the following command:</div>

<CodeBlock code={`sudo rm /var/www/your_domain/info.php`} /><br/>

<div className="text">If you ever need to see this information again, you can always make this page once more. It's like creating a note that you can take out whenever you want.</div>

<div className="head">Step 6 — Connecting to the database from PHP (optional)</div>

<div className="text">If you want to check if PHP can talk to MariaDB (the database) and do some cool stuff, you can create a test table with data and ask PHP to fetch it. But before we do that, let's set up a database and a special user for it.</div><br/>

<div className="text">To start, let's connect to the MariaDB console using the main 'root' account:</div>

<CodeBlock code={`sudo mariadb`} /><br/>

<div className="text">To make a new space for our data, run this command in the MariaDB console:</div>

<CodeBlock code={`CREATE DATABASE example_database;`} /><br/>

<div className="text">Next, let's make a new user and give them full control over the database you just made.<br/><br/>
Use this command to create a new user called 'example_user' who logs in with a password. We're setting the password as 'password' for this example, but you should use a strong password of your own:</div>

<CodeBlock code={`CREATE USER 'example_user'@'%' IDENTIFIED BY 'password';`} /><br/>

<div className="text">Now, let's give our new user control over the 'example_database' we created. Use this command:</div>

<CodeBlock code={`GRANT ALL ON example_database.* TO 'example_user'@'%';`} /><br/>

<div className="text">This makes sure our user 'example_user' can do anything they want with the 'example_database,' but they can't mess with any other databases on your server.</div><br/>

<div className="text">Now, let's save these changes so they take effect right away. Run this command:</div>

<CodeBlock code={`FLUSH PRIVILEGES;`} /><br/>

<div className="text">Now, let's get out of the MariaDB shell. You can do this by running the following command:</div>


<CodeBlock code={`exit`} /><br/>

<div className="text">To check if our new user has the right powers, let's log in to the MariaDB shell again. But this time, we'll use the special credentials we just made for our custom user:</div>

<CodeBlock code={`mariadb -u example_user -p`} /><br/>

<div className="text">Remember to use the -p flag in this command; it will ask you for the password you set when making the 'example_user.' After you've logged in, check that you can access the 'example_database':</div>

<CodeBlock code={`SHOW DATABASES;`} /><br/>

<div className="text">You should see something like this as the output:</div>

<CodeBlock code={`Output
+--------------------+
| Database           |
+--------------------+
| example_database   |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)
`} /><br/>

<div className="text">Now, let's make a pretend table named 'todo_list.' In the MariaDB console, type in this command:</div>

<CodeBlock code={`CREATE TABLE example_database.todo_list (
	item_id INT AUTO_INCREMENT,
	content VARCHAR(255),
	PRIMARY KEY(item_id)
);
`} /><br/>

<div className="text">Let's add some pretend data to our table. In the MariaDB console, run this command a few times, changing the values each time to fill up our test table:</div>

<CodeBlock code={`INSERT INTO example_database.todo_list (content) VALUES ("My first important item");`} /><br/>

<div className="text">To make sure everything got saved in your table, run this command:</div>

<CodeBlock code={`SELECT * FROM example_database.todo_list;`} /><br/>

<div className="text">You should see something like this as the output:</div>

<CodeBlock code={`Output
+---------+--------------------------+
| item_id | content                  |
+---------+--------------------------+
|       1 | My first important item  |
|       2 | My second important item |
|       3 | My third important item  |
|       4 | and this one more thing  |
+---------+--------------------------+
4 rows in set (0.000 sec)
`} /><br/>

<div className="text">If you've checked and everything looks good in your test table, you can leave the MariaDB console. Just run the following command:</div>

<CodeBlock code={`exit`} /><br/>

<div className="text">Now, let's make a PHP file that talks to MariaDB and gets your data. Create a new file in your website folder using a text editor. We'll use nano in this example:</div><br/>

<CodeBlock code={`nano /var/www/your_domain/todo_list.php`} /><br/>

<div className="text">The PHP script below talks to the MariaDB database and asks for the stuff in the 'todo_list' table. It then shows the results as a list. If there's any issue connecting to the database, it will give an error.</div><br/>

<div className="text">Copy and paste this code into your 'todo_list.php' file. Just make sure to replace 'example_user' and 'password' with your own values:</div>

<CodeBlock 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();
}
`} /><br/>

<div className="text">Once you've finished making changes, save and close the file.</div><br/>

<div className="text">Now, you can view this page in your web browser by going to your website's domain name or public IP address, followed by '/todo_list.php':</div>

<CodeBlock code={`http://your_domain/todo_list.php`} /><br/>

<div className="text">This webpage will show the things you added to your pretend list to anyone who visits it:</div><br/>

<img src={todo} alt="Todo-List" /> <br/>

<div className="text">This tells you that your PHP setup is good to go and can talk to your MariaDB server.</div><br/>

<div className="con">Conclusion:</div>
<div className="text">Great job! Now you've set up a strong foundation for showing PHP websites and apps to your visitors, using Apache for the web server and MariaDB as the database.</div>

<div className="text">Next, let's make sure your website is secure by using HTTPS. To do this, you can use Let’s Encrypt. If you're interested in managing dependencies and packages for your PHP projects, check out our guide on How to Install and Use Composer.</div><br/>

<div className="text"><strong>Ready to make your website even better? Follow our guides for more steps and improvements!</strong></div>