Install LAMP Stack on Debian 9


Welcome to our guide on how to install LAMP Stack on Debian 9. LAMP Stack basically stands for Linux server, Apache web server, MariaDB/MySQL relational database and PHP the server side scripting language. These all components are installed together on a server so that it can be able host websites and web applications.

Install LAMP Stack on Debian 9

The first component of LAMP stack is the Linux server, which in this case is your Debian 9 server your are working on. Hence, run the command below to upgrade your system packages.

apt update
apt upgrade

Once the upgrade is done, proceed as follows.

Install Apache Web server

Apache web server packages are available on the default Debian 9 repositories. Hence, you can simply run the command below to install it.

apt install apache2

By default, Apache is set to start after installation. It is also enabled to run in system boot. Verify by running the commands below;

systemctl status apache2
 apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-01-12 01:36:47 EST; 2min 23s ago
 Main PID: 2915 (apache2)
   CGroup: /system.slice/apache2.service
           ├─2915 /usr/sbin/apache2 -k start
           ├─2918 /usr/sbin/apache2 -k start
           └─2919 /usr/sbin/apache2 -k start
systemctl list-unit-files --state=enabled | grep apache
apache2.service           enabled

If UFW is running, allow Apache through it. To open both TCP ports 80 and 443, simply run the command;

ufw allow in “WWW Full”

Navigate to your and enter the IP address of your server, http://server-IP to verify that your web server is ready to server your web content. You should be welcomed by Apache2 Default Page.

Install LAMP Stack on Debian 9

That is great. Apace web server is ready.

Install MariaDB on Debian 9

MariaDB is a fork of the famous MySQL. As a result, MySQL defaults to MariaDB when installation is done. That is to say, even if you use the MySQL installation package, mysql-server, it will install MariaDB. Hence, it is a good idea to install MariaDB using its installation package, mariadb-server.

apt install mariadb-server

Just like Apache, MariaDB is started and enabled to run on system boot after the installation is done.

By default, MariaDB ships with a simple security script that can be run to remove anonymous database users, disallow remote root login, remove test databases in a move to perform preliminary database security. Hence run the script as shown below;


When the script is run, it prompts you to enter the current root password. Since there is none yet, press Enter to set a new root password.

Enter current password for root (enter for none): Press Enter
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: P@SSW0rd
Re-enter new password: P@SSW0rd
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, MariaDB ships with anonymous users that can login to the database without requiring a user account. This poses a security risk. Hence, remove these users.

Remove anonymous users? [Y/n] y
 ... Success!

Disable remote root login such that root user can only login to database locally.

Disallow root login remotely? [Y/n] y
 ... Success!

Remove the test database

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Next, reload privilege tables to effect the changes we have made.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

The MariaDB is successfully set on Debian 9

Install PHP on Debian 9

PHP being the server-side scripting language processes PHP code to generate dynamic web content. PHP and its extensions is available on the default Debian 9 repositories. Hence, run the command below to install PHP and other common PHP extensions.

apt install php libapache2-mod-php php-mysql php-common php-gd php-mbstring php-curl php-xml

Now that the installation is done, we need to configure Apache to be able to process PHP content. This can be done by editing the file, /etc/apache2/mods-available/dir.conf and adding index.php as the first value of the DirectoryIndex such that the configuration looks like;

vim /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.php index.xhtml index.htm

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Save the file and quit.

Run PHP tests to confirm the web server can server PHP content correctly.

Create a PHP test page with <?php phpinfo(); ?> content under the web server root directory.

vim /var/www/html/test.php

Place the above content in this file. Save and quit.

Restart Apache and navigate to the browser and enter the URL, http://server-IP/test.php. You should see a PHP info page.

systemctl restart apache2

Install LAMP Stack on Debian 9

That is beautiful. Upto that far, you have learnt how to Install LAMP Stack on Debian 9. But before we call it a day, it is wise to remove the PHP test page to avoid exposing your environment to the internet script-kiddies.

rm -rf /var/www/html/test.php


You can also check our previous articles on;


We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
I am the Co-founder of, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment