Introduction
Setting up a LAMP stack on a Debian 10 server is essential for hosting dynamic PHP-based websites and applications. In this guide, we’ll walk you through the process of installing Linux, Apache, MariaDB, and PHP—key components that form the backbone of any robust web server environment. Whether you’re configuring Apache as your web server or securing your MariaDB database, this article covers all the necessary steps to ensure your server is ready for dynamic content. From creating a virtual host to testing PHP processing, we’ll help you get your LAMP stack up and running efficiently.
What is LAMP stack?
The LAMP stack is a set of open-source software used together to create a server environment for hosting dynamic websites and web applications. It includes the Linux operating system, the Apache web server, the MariaDB database system for storing data, and PHP to process dynamic content. This setup is commonly used to power websites and apps on the internet.
Step 1 — Installing Apache and Updating the Firewall
Alright, let’s jump straight into it! So, you’ve chosen Apache—one of the most popular and widely used web servers. It’s been around for ages, and the best part? It’s well-documented, so you won’t be stuck figuring things out when something goes wrong. Apache is a solid choice for hosting websites, which is why it’s the default server for so many people.
Now, the first step is to update your package manager’s cache. If this is your first time using the sudo command in this session, don’t be surprised if it asks for your password—it just wants to check that you have the right permissions to manage system packages. So, go ahead and run:
$ sudo apt update
Once the package list is updated and ready to go, let’s move on to installing Apache. All you have to do is run:
$ sudo apt install apache2
It’ll ask for your confirmation. Just press Y, hit ENTER, and let it do its thing. Once Apache is installed, you’re not quite done yet! You’ll need to adjust your firewall settings to make sure everything is open and ready for web traffic.
If you followed the earlier steps to set up your server and enabled UFW (Uncomplicated Firewall), you’re already halfway there. UFW comes preloaded with application profiles, making it super easy to open the necessary ports for web servers. To see the full list of application profiles, run:
$ sudo ufw app list
This will show you a list of profiles, but the ones we care about are the WWW profiles. Here’s a quick look:
Available applications:
WWW
WWW Cache
WWW Full
WWW Secure
The “WWW Full” profile is the one you want, as it’s designed specifically for HTTP and HTTPS traffic—ports 80 and 443. If you want to take a closer look at the details of this profile, run this:
$ sudo ufw app info “WWW Full”
You’ll get something like this:
Profile: WWW Full
Title: Web Server (HTTP, HTTPS)
Description: Web Server (HTTP, HTTPS)
Ports: 80,443/tcp
Now, to allow incoming HTTP and HTTPS traffic, you just need to run:
$ sudo ufw allow in “WWW Full”
After that, it’s time to check if your Apache web server is set up correctly. To do this, you’ll need to access your server’s public IP address in your web browser. Not sure what that is? Don’t worry! I’ll show you how to find it.
How to Find Your Server’s Public IP Address
If you’re unsure of your server’s public IP address, there are a few ways to find it. Usually, this is the same address you use to connect to your server via SSH. Here’s one way to get it from the command line using the iproute2 tools:
$ ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s/\/.*$//’
This command will return one or more IP addresses. Typically, all of them are valid, but in some cases, only one might work. You can try each one to see which one fits your setup.
If you’d prefer a simpler method, you can use curl to ask an external server for your public IP. Since curl isn’t installed by default on Debian 10, you’ll need to install it first by running:
$ sudo apt install curl
Once it’s installed, you can run:
$ curl http://icanhazip.com
After that, you’ll get your IP address. Now, just pop that into your web browser’s address bar and see if the default Apache page shows up. If it does, congratulations! Your Apache web server is successfully installed and accessible through the firewall.
And there you have it! Your web server is live and ready to go. You’ve completed the first step, and now you’re all set for the next part of your setup!
How to Install Apache Web Server on Ubuntu
Step 2 — Installing MariaDB
Now that your trusty web server, Apache, is up and running, it’s time to add the next key player to the mix: the database system. This is where your website or web application will store and manage all its data. We’re talking about MariaDB—a super popular, open-source database system that’s fully compatible with MySQL, and it’s been taking the web world by storm.
Here’s the deal: On Debian 10, you’ll notice that the traditional mysql-server package is now replaced by default-mysql-server. But here’s the thing—this metapackage actually points to MariaDB. It’s kind of like a hidden passage that leads to a faster, more secure version of MySQL. While this works, it’s a better idea to skip the metapackage and go straight for the real deal—mariadb-server. It’s more reliable for the long term and guarantees that you’re always using the latest and greatest version.
So, let’s get started! To begin installing MariaDB, just run this command:
$ sudo apt install mariadb-server
Once the installation is complete, there’s a small extra step to make sure your setup is locked down and secure. MariaDB comes with a handy security script that helps tighten things up by removing insecure default settings. Trust me, you’ll want to run it—it’s quick and easy, and it walks you through securing your installation. To start the script, run:
$ sudo mysql_secure_installation
As soon as you run it, the script will ask for the current root password. Since you’re working with a fresh installation, there won’t be a password yet. So, just press ENTER and move on.
Next, it’ll ask if you want to set up a root password. Here’s something cool: MariaDB doesn’t actually need a password for the root user. It uses a more secure method called unix_socket, which relies on your system’s user authentication. So, when it asks, go ahead and press N for no, then hit ENTER.
The script will then ask a series of questions about configuring the database. Don’t worry, these are all safe default settings, so you can just press Y for yes and hit ENTER to go along with them. What this does is remove unnecessary anonymous user accounts, delete a test database, disable remote root logins, and apply everything immediately. This is a key step in securing your MariaDB installation.
Now, to make sure everything is set up properly, you can log into the MariaDB console as the root user by running:
$ sudo mariadb
Once you’ve logged in successfully, you’ll see something like this:
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)]>
Notice how you didn’t need to enter a password to connect as the root user? That’s because of the unix_socket authentication method I mentioned earlier. While this might feel a little strange at first—no password, really?—it’s actually a good thing. It’s more secure because only users with sudo privileges can log in as the MariaDB root user. This means your PHP scripts and applications can’t just waltz in and mess with the database as the root user.
To keep things secure, it’s highly recommended to create separate, less privileged users for each application or service. For example, instead of letting your PHP scripts access the database with root privileges, create a user with limited access to only the databases that need it.
When you’re done, you can exit the MariaDB console by running:
exit
At this point, your MariaDB server is installed, secured, and ready to go! The next step is to install PHP, the final piece of the puzzle in your LAMP stack. Once PHP is installed, your server will be able to process dynamic content and bring your website to life.
Step 3 — Installing PHP
Now that you’ve got Apache all set up, serving your website’s content, and MariaDB is ready to handle all the data storage, we’re down to the final piece of the puzzle: PHP. Think of PHP as the magic ingredient that connects everything—Apache, MariaDB, and your users. It’s the one that processes dynamic content and executes scripts. So, when someone visits your website, PHP is working behind the scenes to fetch the data from MariaDB, process it, and hand it over to Apache, which then delivers it to the user’s browser. Pretty cool, right?
But here’s the thing: to make sure PHP can talk to MariaDB, you’ll need a special module called php-mysql. This allows PHP to communicate with MySQL-based databases like MariaDB. You’ll also need the libapache2-mod-php package to make sure Apache can handle PHP files properly. Don’t worry though—all these core PHP packages will be installed automatically when you install the necessary ones.
Ready? Let’s get PHP installed. Just run this command:
$ sudo apt install php libapache2-mod-php php-mysql
Once the installation is finished, you can double-check that PHP is running by confirming the version installed. Run:
$ php -v
You should see something like this:
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
Alright, PHP is ready to go! But here’s a little tweak you might want to make: by default, Apache looks for an index.html file when someone visits your site. However, if you want to make sure Apache looks for index.php first—because let’s face it, most modern websites rely on PHP—you’ll need to adjust a simple setting.
To do this, you’ll need to change the order in which Apache looks for files in your website’s directories. Here’s how:
First, open the configuration file that controls this order. Use nano (or your preferred text editor) to open the file with root privileges:
$ sudo nano /etc/apache2/mods-enabled/dir.conf
In this file, you’ll see a line like this:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Now, to prioritize index.php over index.html, simply move index.php to the top of the list like so:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Once you’ve made this change, save and close the file. In nano, that’s CTRL+X, then Y to confirm, and ENTER to save.
Next, you’ll need to reload Apache for the changes to take effect:
$ sudo systemctl reload apache2
To make sure everything is running smoothly, you can check Apache’s status with this command:
$ sudo systemctl status apache2
You should see output like this:
● apache2.service – The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
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
If you see this, you’re golden—Apache is up and running smoothly.
Now that you’ve got Apache and PHP ready, it’s time to think about organizing your server. Before jumping into testing your PHP setup, it’s a good idea to set up a proper Apache Virtual Host. This will help you keep your website’s files and folders in order, making everything easier to manage. Don’t worry, we’ll go over how to set that up in the next step.
Step 4 — Creating a Virtual Host for Your Website
Imagine you’re running a web server, and you need a way to manage several websites without everything getting tangled up. That’s where virtual hosts come in. They’re like magical compartments within Apache, helping you neatly organize and serve multiple websites from a single server. Think of them like different rooms in a house—each room (or virtual host) holds its own content, but they all share the same server. Pretty neat, right?
So, let’s say you want to set up a domain, like your_domain . But remember, this is just an example. You’ll want to replace “your_domain” with the actual name of your website when you’re setting it up. Here’s the best part: virtual hosts let you do all this without messing with the default settings in Apache, which is great because we’re not here to mess things up—we’re here to make things work smoothly.
By default, Apache serves content from /var/www/html , and you’ll find its main configuration in /etc/apache2/sites-available/000-default.conf . But instead of playing around with that default file (because let’s face it, it’s easy to break things), we’ll create a shiny new configuration file for your domain. This way, you can manage your websites cleanly, all on one server.
Let’s walk through the setup:
Step 1: Create the Root Directory for Your Domain
First things first—let’s make a space for your website files. This will be the folder where Apache pulls your site’s content from. Go ahead and create it like this:
$ sudo mkdir /var/www/your_domain
Step 2: Assign Ownership to the Directory
Now, you need to ensure you have permission to manage files in that folder. Use this command to give ownership of the directory to your current user:
$ sudo chown -R $USER:$USER /var/www/your_domain
Step 3: Create a New Configuration File
Now comes the fun part! You’ll create a new Apache configuration file for your domain. Think of this as the blueprint for how Apache should handle requests to your site. Use the following command to open a fresh file in Apache’s configuration directory:
$ sudo nano /etc/apache2/sites-available/your_domain.conf
In this new file, you’ll add the configuration details for your domain. Make sure 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>
In this setup:
- ServerName is your domain, like your_domain.com .
- ServerAlias tells Apache to also respond to www.your_domain.com .
- DocumentRoot is where your site’s files live, which we just set up in /var/www/your_domain .
If you don’t yet have a domain name and just want to test things out locally, you can comment out the ServerName and ServerAlias lines by adding a # at the beginning of each one, like this:
# ServerName your_domain
# ServerAlias www.your_domain
Once that’s all in place, save and close the file. If you’re using nano, just press CTRL+X , then Y to confirm, and ENTER to finalize.
Step 4: Enable the Virtual Host
Now, you need to tell Apache to actually use this new configuration. You can do that by enabling your virtual host like this:
$ sudo a2ensite your_domain
Step 5: Disable the Default Apache Website (Optional)
If you’re not using a custom domain or want to avoid conflicts with Apache’s default site, you might want to disable it. This prevents Apache from showing its default page when it can’t find a matching virtual host. Run this to disable it:
$ sudo a2dissite 000-default
Step 6: Check for Configuration Errors
It’s always a good idea to make sure there are no typos or errors in your configuration file. Apache can help with that. Just run:
$ sudo apache2ctl configtest
If everything is good to go, you should see something like this:
Syntax OK
Step 7: Reload Apache to Apply Changes
Now that everything’s set up, it’s time to reload Apache and make it all official. Run:
$ sudo systemctl reload apache2
That’s it! Now Apache is all set to serve your site from /var/www/your_domain whenever someone visits your domain (or even the www version if you set that up). Your virtual host is now active, and everything is running smoothly. The server knows exactly what to do when it gets a request for your website. You’re one step closer to having a fully functional site hosted on Apache.
Ensure your configuration is correct before reloading Apache to avoid any potential issues.
Apache Virtual Hosts Documentation
Step 5 — Testing PHP Processing on Your Web Server
Alright, you’ve done the hard part—setting up Apache, configuring MariaDB, and getting everything ready to go. Now, we’re down to the final test: making sure PHP is working smoothly with Apache. Think of this step as the “final check” before your server is ready to handle all the dynamic content you want to throw at it. And trust me, you’ll want to make sure PHP is doing its job right because it’s going to be the engine behind much of your website’s functionality.
So, here’s the thing: we need to test if Apache can properly process PHP files. The easiest way to do this is by creating a simple PHP test script. It’s like the diagnostic tool that tells you if everything’s humming along nicely.
Creating the PHP Test Script
Start by creating a new PHP file in your website’s custom web root directory, where all your website files live. For example, let’s name the file info.php and place it inside /var/www/your_domain. You’ll run this command:
$ nano /var/www/your_domain/info.php
This opens up a blank file in the nano text editor, ready for you to add some PHP code. Now, let’s add the following PHP code to this file:
<?php
phpinfo();
What does this do? Well, phpinfo() is a built-in PHP function that dumps a ton of useful information about the PHP configuration on your server—like the PHP version, available modules, server information, and a whole bunch of other details. It’s basically your go-to tool for checking whether PHP is doing its job properly.
Testing the PHP Script
Once you’ve added that PHP code, you’ll want to save and close the file. If you’re using nano, you can do this by pressing CTRL+X , then hitting Y to confirm you want to save, and finally pressing ENTER to exit.
Now, it’s time to see if it works. Open your web browser and type in the URL for your server’s domain name or public IP address, followed by /info.php . For example:
http://your_domain/info.php
If everything’s working like it should, you’ll see a page full of information about your PHP configuration. It’ll show you things like the PHP version running, the extensions installed, server environment details, and more. If you see that, congrats! PHP is up and running on your Apache server. You’re all set for dynamic content to start flowing.
Verifying the PHP Setup
This page is super handy for troubleshooting and double-checking that PHP is configured correctly. So, if you see the PHP info page in your browser, it’s a solid sign that everything’s functioning just as it should be.
Security Consideration
But hold on—while this page is great for debugging, it also shows a lot of sensitive information about your server’s PHP environment. For security reasons, you don’t want this information floating around on the web forever. Once you’ve confirmed that PHP is working, go ahead and remove the info.php file to keep things safe.
To delete it, run:
$ sudo rm /var/www/your_domain/info.php
And that’s it! The file is gone, and no one can access that sensitive server information anymore. But don’t worry, if you ever need to check it again in the future, you can always recreate it by going through the same steps.
By following these steps, you’ve successfully tested PHP processing on your web server. That means your server is all set to handle dynamic content for your website or web application, making it ready to serve your PHP-powered pages to the world. You’re officially on the path to a fully functioning server!
Step 6 — Testing Database Connection from PHP (Optional)
So, you’ve got your server up and running with Apache and MariaDB, and now it’s time for the final piece of the puzzle—making sure PHP can talk to your MariaDB database. You want to test if PHP is truly connecting to the database and running queries as it should. It’s like checking that the pipeline between your PHP code and the database is all set up and ready to go. Here’s how we can test that connection.
Creating the Database and User
First thing’s first: before we start making queries, we need to make sure you have a database to connect to and a user with permission to access it. So, let’s log in to the MariaDB console using the root account, which gives us the power to create databases and users.
Run the command:
$ sudo mariadb
Now that you’re in the MariaDB console, it’s time to create a new database. Let’s call it example_database for testing purposes:
CREATE DATABASE example_database;
Next, let’s create a new user who’ll be able to access and interact with this database. Remember, security is important, so don’t use the placeholder password—pick something strong. Here’s an example where the user is called example_user and the password is password (replace this with a stronger one!):
CREATE USER ‘example_user’@’%’ IDENTIFIED BY ‘password’;
The @’%’ part allows the user to connect from any host. If you want to limit access to certain IP addresses or hostnames, you can replace the % with a specific address.
Next, we need to give example_user full access to example_database, so they can read, write, and make changes to the database. Here’s how you do it:
GRANT ALL ON example_database.* TO ‘example_user’@’%’;
This grants full privileges, but restricts them to this specific database only. We’re keeping things neat and secure!
Now, make sure these new privileges take effect by flushing them:
FLUSH PRIVILEGES;
Once that’s done, we’re ready to log out of MariaDB. Run:
exit
Testing the New User’s Permissions
Alright, let’s test if everything is set up correctly. We’ll log back in, but this time with the new user’s credentials. Use the following:
mariadb -u example_user -p
You’ll be prompted to enter the password for example_user. After you do that, you can run a quick command to see if example_user can access example_database:
SHOW DATABASES;
If everything’s good, you should see something like this:
+——————–+
| Database |
+——————–+
| example_database |
| information_schema |
+——————–+
2 rows in set (0.000 sec)
If you see example_database listed, then example_user has been granted access, and we’re ready to move forward.
Creating a Test Table and Inserting Data
Now that the user has the proper access, let’s create a test table and add some data to it. Start by selecting the database you just created:
USE example_database;
Then, let’s create a simple table named todo_list with two columns: one for the item_id, which will be an auto-incrementing primary key, and another for the content (which will hold the task description). Here’s the SQL to do that:
CREATE TABLE example_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
With the table in place, let’s insert some test data into it. We’ll create a few tasks for the list:
INSERT INTO example_database.todo_list (content) VALUES (“My first important item”);
INSERT INTO example_database.todo_list (content) VALUES (“My second important item”);
INSERT INTO example_database.todo_list (content) VALUES (“My third important item”);
INSERT INTO example_database.todo_list (content) VALUES (“And this one more thing”);
Now, let’s check if the data was successfully inserted. Run this query to see the contents of the todo_list table:
SELECT * FROM example_database.todo_list;
The output should look something like this:
+———+————————–+
| item_id | content |
+———+————————–+
| 1 | My first important item |
| 2 | My second important item |
| 3 | My third important item |
| 4 | And this one more thing |
+———+————————–+
Great! Now that we’ve confirmed the table is set up and the data is there, let’s exit the MariaDB console again:
exit
Creating the PHP Script to Query the Database
It’s time to connect the dots. Now, we need to create a PHP script that can talk to MariaDB and fetch that data we just inserted. Let’s create a new PHP file called todo_list.php inside your web root directory:
nano /var/www/your_domain/todo_list.php
Now, add the following PHP code to the file. This script connects to MariaDB, queries the todo_list table, and displays the results:
<?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();
}
This script does a few important things. First, it connects to the MariaDB database with the user credentials you set up. Then, it queries the todo_list table and outputs the results in an ordered list (<ol>).
Testing the PHP Script
Once you’ve saved your file, head over to your web browser and type in the following URL:
http://your_domain/todo_list.php
If everything is set up correctly, you should see the list of tasks that you inserted into the todo_list table. This means your PHP environment is properly configured to connect to MariaDB and retrieve data.
And just like that, you’ve completed the final test. PHP is now able to connect to MariaDB, query data, and display it on your website. You’ve officially made it through the PHP and MariaDB integration—your server is ready to handle dynamic content and database-driven websites!
Conclusion
In conclusion, setting up a LAMP stack on Debian 10 is an essential skill for hosting dynamic PHP-based websites and applications. By following the steps for installing Linux, Apache, MariaDB, and PHP, you can ensure your server is fully equipped to manage data, process dynamic content, and serve web pages efficiently. Whether you’re configuring Apache, securing MariaDB, or testing PHP integration, this guide provides the foundation for a stable, functional environment. As web development evolves, mastering tools like the LAMP stack remains crucial for maintaining high-performing servers and ensuring seamless web experiences.Snippet for Search Result:
Master the LAMP stack setup on Debian 10 by learning how to install Linux, Apache, MariaDB, and PHP for dynamic websites.
Master Linux Permissions: Set chmod, chown, sgid, suid, sticky bit