Install a Full LAMP Stack on a Debian Server

Anyone in the know will tell you that Debian makes an outstanding server operating system. It’s one of the most stable operating systems available, is highly secure and offers all of the tools and apps you need. Whether you’re a small business, an enterprise company, or a development team from one to 100, Debian has something to offer.
Like many Linux distributions, Debian makes for a great LAMP server.
You know LAMP: Linux, Apache (web server), MySQL, PHP. It’s the software stack that has powered the internet for decades.
There is one change of note, in that many users have opted to drop the MySQL option in favor of MariaDB. Why is that, you ask? Simple. MariaDB is more scalable, faster and more secure than Oracle’s MySQL. I’m going to walk you through the installation using MariaDB because I also find this database server to be easier to use.
With that said, let’s get to work.
What You’ll Need
The only things you’ll need for this are a running instance of Debian with a network connection (I’m using version 12, “Bookworm“) and a user with sudo privileges.
Sudo Privileges
Before we get into the installation, you’re going to want to give a standard user sudo privileges. Why? When you install Debian, you get the opportunity to create a new user, but you cannot assign that user as an administrator. A lot of tutorials will tell you to change to the root user (with su –), but I’ve always avoided that because it could lead to security issues. I’m one of those Linux users who will avoid using root at all costs.
So, to make this change, you have to first change to the root user with the command:
1 |
su - |
With the – in the above command, you do not gain access to root’s $PATH, and since commands like usermod are only accessible to root’s path, you need to use the – option.
Once you’ve successfully authenticated and are at the root user bash prompt, issue the following command to add your user to the sudo group:
1 |
usermod -aG sudo USER |
Where USER is the username in question.
Once you’ve done that, exit out of root with the exit command and then either log out and log back in (if your server has a GUI), or exit from your SSH session and log back in.
You’re now ready to install the LAMP stack.
Installing Apache
The first thing we’ll do is install Apache. For that, issue the command:
1 |
sudo apt-get install apache2 -y |
You might run into a situation where you are prompted to insert a CD. If that happens, it means the default sources were not added to the sources.list file. Should that occur, open the necessary file with:
1 |
sudo nano /etc/apt/sources.list |
You might find only a single line in that file that starts with cdrom. Comment that line out (by adding a # to the beginning) and then paste the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
deb https://ftp.debian.org/debian/ bookworm contrib main non-free non-free-firmware # deb-src https://ftp.debian.org/debian/ bookworm contrib main non-free non-free-firmware deb https://ftp.debian.org/debian/ bookworm-updates contrib main non-free non-free-firmware # deb-src https://ftp.debian.org/debian/ bookworm-updates contrib main non-free non-free-firmware deb https://ftp.debian.org/debian/ bookworm-proposed-updates contrib main non-free non-free-firmware # deb-src https://ftp.debian.org/debian/ bookworm-proposed-updates contrib main non-free non-free-firmware deb https://ftp.debian.org/debian/ bookworm-backports contrib main non-free non-free-firmware # deb-src https://ftp.debian.org/debian/ bookworm-backports contrib main non-free non-free-firmware deb https://security.debian.org/debian-security/ bookworm-security contrib main non-free non-free-firmware # deb-src https://security.debian.org/debian-security/ bookworm-security contrib main non-free non-free-firmware |
Save and close the file. Run apt-get update and the apache2 installation command will work.
Install Uncomplicated Firewall
You may or may not have ufw installed by default (depending on how you installed Debian). To install it, issue the command:
1 |
sudo apt-get install ufw -y |
Once that’s installed, allow HTTP traffic with the command:
1 |
sudo ufw allow in "WWW Full" |
If you open a web browser and point it to http://SERVER (Where SERVER is the IP address of your Debian server), you should see the Apache welcome page.
Install MariaDB
Next, we’ll install the database portion of the stack with the command:
1 |
sudo apt-get install mariadb-server -y |
When that completes, secure the installation with the command:
1 |
sudo mysql_secure_installation |
Yes, the above command uses mysql, but it is correct.
You’ll first be asked for the current root password. Since that hasn’t been set, hit Enter on your keyboard and type “n” when asked if you want to switch to unix_socet authentication. After that, you’ll be prompted to type/verify a new (strong/secure) password for the root user. Answer “y” to the remaining questions.
Installing PHP
The final piece of the puzzle is PHP, which is installed with the command:
1 |
sudo apt-get install php libapache2-mod-php php-mysql -y |
Let’s verify PHP is installed and working. To do that, create a new file with:
1 |
sudo nano /var/www/html/info.php |
In that file, paste the following:
Save and close the file. Point your browser to http://SERVER/info.php (where SERVER is the IP address of your Debian server) and you should see a listing of the PHP version and a list of other important information (Figure 1).
-
Figure 1: The site of this page means Apache and PHP are working as expected.
Congratulations, you’ve just built your first LAMP server using Debian. You can now start building websites and even use it for serving up web apps (with a bit more work, of course). We’ll be coming back to Debian in future tutorials, but with this base knowledge, there’s no limit to what you can do with this open source operating system.