How to Install LAMP Stack on Debian 9 ‘Stretch’

This article is also available in Bahasa Indonesia

Internet is a combination of networks that are connected to each other. There are terms like server and host on the Internet. The server is responsible for serving all kinds of requests requested by the host, in a more concise manner, there are servers serving mailing, to serve database, some are serving web request, LAMP one of them.

LAMP is a collection of each task owned by a server to serve web companies hosting, web developers and others. LAMP itself is stand for Linux, Apache, MySQL (MariaDB), and PHP.

  1. Linux (We will use Debian 9 Stretch).
  2. Apache as Web Server.
  3. MySQL/MariaDB. (Debian 9 makes MariaDB the default database)
  4. PHP (Some people prefer Python or Perl)

1. Linux

Debian Linux will be used as a server this time. Debian is known for its rock-solid Linux distro. Debian does not follow the six-month release cycle like most other Linux distributions, Debian will only release when the collection of apps inside the repo is completely stable.

Please refer to @jekisatria‘s post about How to Install Linux Debian 9 Stretch Gnome. If the process succeeds, update and upgrade patches by running the command;

$ sudo apt update && sudo apt upgrade


2. Apache as Web Server

The second step is to install Apache web server. This can be done with;

$ sudo apt -y install apache2


to start the Apache server, run;

$ systemctl start apache2.service

to stop the Apache server, run;

$ systemctl stop apache2.service

to restart, run the command;

$ systemctl restart apache2.service

if you want to check whether the apache2 service is running or not, it can be done by running;

$ systemctl status apache2.service


In many cases, you will find firewall within the network, now to permit HTTP Apache server to be accessed via a firewall, then run the following command;

ufw status
ufw allow 80/tcp
ufw allow 443/tcp
ufw reload
ufw enable

If all the installation process is successful and running smoothly, the next step is to try Apache web server. Open any web browser you like, then at URL bar write;


Apache2 Debian Default Page: It works - Mozilla Firefox_002.png



You will be redirected to the default “Apache2 Debian” page, this indicates that the web server that we created is already running.

You can change the default page by editingindex.html file located at /var/www/html folder, for example;

$ sudo nano /var/www/html/index.html.

For main configuration file located at/etc/apache2/apache2.conf.

3. MariaDB

Since Debian 9 was released, the Debian developers used MariaDB instead of the MySQL server that Oracle has acquired. MariaDB is one of the most popular database server. Created by MySQL creators with a guarantee it will always be Open Source. To install the MariaDB server, execute the following commands;

$ sudo apt -y install default-mysql-server


$ sudo apt -y install mariadb-server


Typically, during this installation stage, file information and file size will be displayed, user can press y or yes button to continue. Well, the -yparameter as mentioned above is to install immediately downloaded and installed without having to wait for they or yes input of the user.

The next step is to make MariaDB more secure by executing the mysql_secure_installation command. It aims to improve the security of MariaDB database which is not maximal enough, including;

  • Set the root password for the database.
  • Restrict root account for localhost use only .
  • Removes anonymous user.
  • Not allow root account to have remote access.
  • Removes the test database and any permissions associated withtest_.

before proceeding, make sure that MySQL service is running by typing;

$ /etc/init.d/mysql start

[ ok ] Starting mysql (via systemctl): mysql.service.

$ sudo mysql_secure_installation

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 
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: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

Now let’s see the result, type;

$ sudo mysql -u root -p

user@debian: ~_003.png

4. PHP Version 7

Now we will install PHP version 7 together with some PHP modules on our Debian server.

$ sudo apt install php7.0 libapache2-mod-php7.0 php7.0-mysql php7.0-gd php7.0-opcache


If the installation process is successful, we need to restart our Apache server by running the command;

$ systemctl restart apache2.service.

to try PHP installation, create a script file named coba.php in/var/ www/html/coba.php, and insert the following line of code;



dont forget to restart Apache server.


You have now successfully installed LAMP Stack Server on Debian 9 Stretch. Hopefully, this article could help anyone who want to install LAMP Stack on their Debian server, if there are suggestions, please leave a comment.