How to Install LAMP Stack on Debian 9 Stretch

This tutorial is going to show you how to install Apache, MariaDB and PHP7 (LAMP stack) on Debian 9 stretch. LAMP is the most common web service stack for building dynamic websites and web applications. It’s made up of four components – Linux, Apache, MySQL/MariaDB, PHP – Linux is the operating system; Apache is the web server; MySQL/MariaDB is database; PHP is a server-side scripting language.

All of the four components are free and open-source. However, since MySQL is now owned by Oracle and there’s a chance that Oracle turns it to a closed-source product, we will choose MariaDB instead of MySQL. You can follow this tutorial on a VPS (Virtual Private Server) or on a local Debian 9 computer.

Please note that you need to have root privilege when installing software on Debian. You can add sudo at the beginning of a command, or use su - command to switch to root user.

Step 1: Update Software Packages

Before we install the LAMP stack, it’s a good idea to update repository and software packages. Run the following command on your Debian 9 OS.

sudo apt update

sudo apt upgrade

Step 2: Install Apache Web Server

Enter the following command to install Apache Web server. The apache2-utils package will install some useful utilities like Apache HTTP server benchmarking tool (ab).

sudo apt install apache2 apache2-utils

debian 9 lamp

After it’s installed, Apache should be automatically started. Check its status with systemctl.

systemctl status apache2


 apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset:
Active: active (running) since Sun 2017-08-13 02:36:48 UTC; 5min ago
Main PID: 14463 (apache2)
CGroup: /system.slice/apache2.service
├─14463 /usr/sbin/apache2 -k start
├─14465 /usr/sbin/apache2 -k start
└─14466 /usr/sbin/apache2 -k start

If it’s not running, use systemctl to start it.

sudo systemctl start apache2

It’s also a good idea to enable Apache to automatically start at boot time.

sudo systemctl enable apache2

Check Apache version:

sudo apache2 -v


Server version: Apache/2.4.25 (Debian)
Server built: 2017-07-18T18:37:33

Now type in the public IP address of your Debian 9 server in the browser address bar. You should see “It works!” Web page, which means Apache Web server is running properly.

debian 9 install apache2 web server

You can use the following commands to fetch the public IP address of your Debian 9 server in the terminal.

sudo apt install curl


If you are installing LAMP on your local Debian 9 box, then you should type or localhost in the browser address bar.

Finally, we need to set www-data (Apache user) as the owner of web root directory. By default it’s owned by root user.

sudo chown www-data:www-data /var/www/html/ -R

Step 3: Install MariaDB Database Server

MariaDB is a drop-in replacement for MySQL. Enter the following command to install it on Debian 9.

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mariadb


 mariadb.service - MariaDB database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2017-08-13 03:05:17 UTC; 1min 37s ago
Main PID: 16127 (mysqld)
Status: "Taking your SQL requests now..."
CGroup: /system.slice/mariadb.service
└─16127 /usr/sbin/mysqld

If it’s not running, start it with this command:

sudo systemctl start mariadb

To enable MariaDB to automatically start at boot time, run

sudo systemctl enable mariadb

Now run the post installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

how to install lamp stack on Debian 9 stretch

Next, you can just press Enter to answer all remaining questions. This will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security.

debian 9 lamp stack install

Now you can use the following command to log in to MariaDB server.

sudo mariadb -u root

To exit, run


Check MariaDB server version information.

mariadb --version


mariadb Ver 15.1 Distrib 10.1.23-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Step 4: Install PHP7

Enter the following command to install PHP7.

sudo apt install php7.0 libapache2-mod-php7.0 php-common php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline

Enable the Apache php7.0 module then restart Apache Web server.

sudo a2enmod php7.0

sudo systemctl restart apache2

Check PHP version information.

php --version


PHP 7.0.19-1 (cli) (built: May 11 2017 14:04:47) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.19-1, Copyright (c) 1999-2017, by Zend Technologies

To test PHP scripts with Apache server, we need to create a info.php file in the Web root directory.

sudo nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Now in the browser address bar, enter server-ip-address/info.php. Replace sever-ip-address with your actual IP. If you follow this tutorial on your local computer, then type or localhost/info.php.

You should see your server’s PHP information. This means PHP scripts can run properly with Apache web server. You can find that Zend OPcache is enabled.

Debian 9 stretch php7

Congrats! You have successfully installed Apache, MariaDB and PHP7 on Debian 9 Stretch. For your server’s security, you should delete info.php file now to prevent prying eyes.

sudo rm /var/www/html/info.php

That’s it! I hope this tutorial helped you install LAMP stack on Debian 9 Stretch. As always, if you found this post useful, then subscribe to our free newsletter to get new tutorials.

The post How to Install LAMP Stack on Debian 9 Stretch appeared first on LinuxBabe.Com.