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

Output:

 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

Output:

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

curl http://icanhazip.com

If you are installing LAMP on your local Debian 9 box, then you should type 127.0.0.1 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

MariaDB is recommended over MySQL. As a matter of fact, if you try to install MySQL on Debian 9 using the following command, it will install MariaDB and set MariaDB as the default database server.

sudo apt install mysql-server mysql-client

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

systemctl status mariadb

Output:

 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

exit;

Check MariaDB server version information.

mariadb --version

Output:

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 php7.0-mysql 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

Output:

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 127.0.0.1/info.php 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.

Rate this tutorial
[Total: 78 Average: 4.9]

18 Responses to “How to Install LAMP Stack on Debian 9 Stretch

  • Thank you! These steps works fine also on Rapsberry pi 3!

  • Rodriguez Joe
    6 years ago

    I have to Say this is the Time Everything worked. I needed to add PHPMyAdmin and I’m right back where I stated PHPMyAdmin without “Create Users and ”
    Privileges”. You did a Great Job Explaining everything. But I could not see how to add Databases or Tables??? Thanks Again for a Great Job. I guess I just can’t get this 2018 Raspberry Pi 3 Stretch Debian to Work PHPMyAdmin. Thanks Joe

  • dwg1503
    6 years ago

    Great tutorial for setting up the lamp software. I want to host the database, web etc files on an attached USB drive, but whatever I try I seem to crash the database and can’t get it to open.
    Do you have any info that would help me ?
    Thanks for your help

  • First tutorial I’ve found that actually works. Thank you for the help.

  • Thanx! Had no problem following the above for my Raspberry Pi 3B=.
    And actually mananged to move my www folder to the attached usb-drive.

  • Efrain HUerta Araujo
    5 years ago

    Very, very good indeed, excellent tutorial, thank you for your help, very good indeed !!!!

  • scott harrison
    5 years ago

    ibapache2-mod-php7.0 – can’t find this in the sources

  • Merci beaucoup.

  • Excellent step by step tutoral, got my LAMP system up and running on my Raspberry Pi 3+ in no time at all.

    It would be nice to have a follow on tutoral to show us how to install phpMyAdmin do this as I have managed to break this.

    Thank you

  • Thanks for the guide.
    Feel free to use this info to update your page and help other users like myself.

    Enter the following command to install PHP7:

    sudo apt install php7.2 libapache2-mod-php7.2 php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline -y

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

    sudo a2enmod php7.2
    PHP 7.2.19-0ubuntu0.18.04.1 (cli) (built: Jun  4 2019 14:48:12) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
        with Zend OPcache v7.2.19-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

    Here is the phpinfo() output for php7.2:
    http://drive.google.com/uc?export=view&id=1AU7EobFC9dhoMg4RzRYZQizCCoYkStDR

    Thanks!

  • Aderras Lahcen
    5 years ago

    Hi Dear Xiao Guo, according to comments your article is very helpful. So I will follow it surely; but the black hole for me is the address of my local host. Once I put 127.0.0.1 in the browser it changes to 192.x.x.x. Please how can I fix this problem? and how can we install wordpress on localhost after following your present post. Thank you in advance!

  • You don’t need this line to install php

    sudo apt install php7.2 libapache2-mod-php7.2 php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline -y
    

    this works just as well

    sudo apt install php
    
  • Viacheslav
    4 years ago

    Thank you a lot Xiao, all works for me at opennebula VM
    (provisioned by vagrant) as you wrote.

    So wish you Good Luck and Good mood :)!

  • This is the first time in 3 years since I installed LAMP on Debian 8, last time it was from the digital ocean website and worked great. Since then I had to go to 6 sites to get a smooth install without errors. No errors on this page.
    Fantastic job on this, I wish you could add a continuation to add a WordPress site including the database configs as well. I can do it but need to design a site before launching it to the customers’ site. I’m lazy and love the copy and paste way.

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here