How to Install LAMP Server on Debian 9 Stretch


LAMP (a server stack) stands for Linux, Apache, MySQL, and PHP; four popular open source software components used by most of the web hosting companies, web developers, blogs and others. Here is the small guide about installing your web server for the testing environment in the latest release of Debian.

MariaDB replaces MySQL server as a default database in Debian 9.

Install Linux

Here is the article about Step by Step installation of Debian 9 and Upgrading Debian 8 Jessie to Debian 9 Stretch.

Now, we will proceed to the installation of AMP (Apache version 2, PHP version 7, MariaDB version 10.2) on Debian 9. So, let’s install one by one.

Install Apache HTTP server

To start off, we will install Apache web server. Open up the Terminal and then switch to root user.

$ su -


$ sudo su -

Update the package repository index.

apt-get update

The package name of the Apache web server is apache2. Install apache2 package using the apt-get command

apt-get -y install apache2

Controlling Apache HTTP server:

To start Apache2 server, run.

systemctl start apache2

To stop the Apache2 server, run.

systemctl stop apache2

To restart the Apache2 server, run.

systemctl restart apache2

To check the status of Apache2 server, run.

systemctl status apache2.service


Allow Apache HTTP server via the firewall.

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

Testing Apache HTTP server:

We will now test Apache web server to confirm whether it is working correctly or not. Open up any web browser and then navigate it to the following URL.




You will get the “Apache2 Debian Default Page“, this default page confirms you that the Apache HTTP server is working fine.

Install LAMP Server on Debian 9 Stretch - Apache2 Default Page
Install LAMP Server on Debian 9 Stretch – Apache2 Default Page

Apache HTTP server’s default document root is /var/www/html/ on Debian and the main configuration file is /etc/apache2/apache2.conf. Additional configuration files are found in the /etc/apache2 directory and the directory hireacrchy as follows.

       |-- apache2.conf
       |       `--  ports.conf
       |-- mods-enabled
       |       |-- *.load
       |       `-- *.conf
       |-- conf-enabled
       |       `-- *.conf
       `-- sites-enabled
               `-- *.conf

Install MySQL (MariaDB) Server

MariaDB is the drop in replacement of MySQL server, developed by the original developers of MySQL to stay and have open source RDBMS. MariaDB is now one of the most popular database servers in the world.

To install MariaDB, run the following command

apt-get -y install mariadb-server

Secure MariaDB server:

Next is to make MariaDB secure by using “mysql_secure_installation” command.

READ: How to Secure MariaDB installation using mysql_secure_installation.

This command enables you to improve the security of your MariaDB installation in the following ways:

  • You can set a password for DB root account.
  • You can restrict root accounts to the local host.
  • You can remove anonymous user accounts.
  • You can remove the test database and privileges that permit anyone to access databases with names that start with test_.
root@debian:~# 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): 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: Enter Password
Re-enter new password: Re-Enter 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!

Install PHP

By default Apache server supports the .html only not PHP, for that you need to install PHP and the php extension package for the MariaDB server. Here, we will install PHP 7 on Debian 9.

apt-get -y install php php-mysql

You would need to restart the Apache HTTP server after the installation of the PHP package.

systemctl restart apache2

Testing PHP:

To test a PHP 7, we need to place a .php file on the default document root of Apache HTTP server. The document root of the default site is /var/www/html. S, we will now create a PHP file (info.php) in that directory and call it in a browser. The output will display lots of useful details about our PHP installation, such as the installed PHP version.

Use a VI editor to create the info.php file.

vi /var/www/html/info.php

This vi command will open up a file called info.php. Copy/Paste below lines into the info.php file:

<?php phpinfo(); ?>

Save and close the file using Esc + :wq.

Now open up your internet web browser and type the following URL into the web address.




The page will look like below,

Install LAMP Server on Debian 9 Stretch - PHPInfo Page
Install LAMP Server on Debian 9 Stretch – PHPInfo Page

That’s All!, you have successfully installed the LAMP on Debian 9.

You might also like