Skip to main content

Installing and Configuring LAMP Stack on Debian 10 A Comprehensive Tutorial

· 18 min read
Yashwin Shankar
Front End Developer @ Tech4Biz Solutions Pvt Ltd.
Introduction
A LAMP stack is a group of computer programs that work together to run websites and apps smoothly. LAMP stands for:
  1. L for Linux: This is the computer's operating system.
  2. A for Apache: Apache helps manage the flow of information on the internet.
  3. M for MariaDB: This is like a big, organized file cabinet where important information for our website is kept.
  4. P for PHP: PHP takes care of things behind the scenes, making sure everything works smoothly.
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!

Requirements
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!

Step 1 — Set up Apache and update the firewall
Let's begin by installing Apache, a popular web server. It's like a traffic director for your website, making sure everything goes smoothly.

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!
sudo apt update

Now, let's install Apache. Just follow these steps:
sudo apt install apache2

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.

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.

To do that, look at a list of rules it already knows. Follow these steps:
sudo ufw app list

Output
Available applications:
. . .
WWW
WWW Cache
WWW Full
WWW Secure
. . .

"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:
sudo ufw app info "WWW Full"

Output
Profile: WWW Full
Title: Web Server (HTTP,HTTPS)
Description: Web Server (HTTP,HTTPS)
Ports:
80,443/tcp

Let's make sure your website can accept visitors. Allow incoming traffic for your website by following these steps:
sudo ufw allow in "WWW Full"

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.
http://your_server_ip

You'll see a page that says 'Apache2 Debian Default Page.' This page is just for testing and giving you information:

Debain
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.

The best way to find your server's IP address
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.

Here's a simple way to find it:"

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:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

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.

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.

Since Debian 10 doesn't have curl installed by default, you'll need to add it first:
sudo apt install curl

After that, use this command to ask a specific server what your IP address is:
curl http://icanhazip.com

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.

Step 2 — Setting up MariaDB
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.

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!

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.

To do this, just run the following command:
sudo apt install mariadb-server

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:

sudo mysql_secure_installation

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.

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'.

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."
sudo mariadb

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:

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)]>

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.

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.

If you want to leave the MariaDB console, just follow these steps:
exit

Your MariaDB server is now installed and safe. Next up, you'll install PHP, which is the last part of the LAMP stack.

Step 3 — Adding PHP to the server
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.

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.

To get all these, just run this command:
sudo apt install php libapache2-mod-php php-mysql

After everything is installed, you can check which version of PHP you have by using this command:
php -v

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

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.

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
sudo nano /etc/apache2/mods-enabled/dir.conf

Inside the file, you'll see something like this:
<IfModule mod_dir.c>
  DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

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.

Now, let's make sure Apache knows about the changes. To do that, follow these steps:
sudo systemctl reload apache2

To see how Apache is doing, you can use this command:
sudo systemctl status apache2

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

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.

Step 4 — Creating a Virtual Host for your Website
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.

Note: 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.

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.

Start by making the main folder for your website, 'your_domain,' like this:
sudo mkdir /var/www/your_domain

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:
sudo chown -R $USER:$USER /var/www/your_domain

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:
sudo nano /etc/apache2/sites-available/your_domain.conf

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:
<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>

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.

Once you're finished, save and close the file.

Now, let's tell Apache to start using this setup by running this command:
sudo a2ensite your_domain

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:
sudo a2dissite 000-default

To check if you didn't make any mistakes in your configuration file, run the following command:
sudo apache2ctl configtest

Now, let's make sure Apache knows about the changes you made. To do that, run the following command:
sudo systemctl reload apache2

Now, let's make a simple PHP script to check if PHP is working correctly on your server.

Step 5 — Verifying PHP processing on your server
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.

First, make a new file called 'info.php' in your website's folder:
nano /var/www/your_domain/info.php

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:
<?php
phpinfo();

After you've pasted the code, save and close the file.

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':
http://your_domain/info.php

Here's an example of what the basic PHP web page looks like:
This page shows some basic details about your server from the PHP perspective. It's handy for checking if everything is set up correctly.

If you see this page in your browser, it means your PHP installation is good to go.

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:
sudo rm /var/www/your_domain/info.php

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.
Step 6 — Connecting to the database from PHP (optional)
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.

To start, let's connect to the MariaDB console using the main 'root' account:
sudo mariadb

To make a new space for our data, run this command in the MariaDB console:
CREATE DATABASE example_database;

Next, let's make a new user and give them full control over the database you just made.

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:
CREATE USER 'example_user'@'%' IDENTIFIED BY 'password';

Now, let's give our new user control over the 'example_database' we created. Use this command:
GRANT ALL ON example_database.* TO 'example_user'@'%';

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.

Now, let's save these changes so they take effect right away. Run this command:
FLUSH PRIVILEGES;

Now, let's get out of the MariaDB shell. You can do this by running the following command:
exit

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:
mariadb -u example_user -p

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':
SHOW DATABASES;

You should see something like this as the output:
Output
+--------------------+
| Database           |
+--------------------+
| example_database   |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)

Now, let's make a pretend table named 'todo_list.' In the MariaDB console, type in this command:
CREATE TABLE example_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);

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:
INSERT INTO example_database.todo_list (content) VALUES ("My first important item");

To make sure everything got saved in your table, run this command:
SELECT * FROM example_database.todo_list;

You should see something like this as the output:
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)

If you've checked and everything looks good in your test table, you can leave the MariaDB console. Just run the following command:
exit

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:

nano /var/www/your_domain/todo_list.php

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.

Copy and paste this code into your 'todo_list.php' file. Just make sure to replace 'example_user' and 'password' with your own values:
<?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();
}

Once you've finished making changes, save and close the file.

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':
http://your_domain/todo_list.php

This webpage will show the things you added to your pretend list to anyone who visits it:

Todo-List
This tells you that your PHP setup is good to go and can talk to your MariaDB server.

Conclusion:
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.
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.

Ready to make your website even better? Follow our guides for more steps and improvements!