Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9

Early this week (June 17, 2017), the Debian project announced the availability of the new stable version (Debian 9) codenamed Stretch.

With this release, the well-known and widely-used Debian 8 Jessie gained old-stable status, which designates the previous stable repository. As it always happens with the release of a new stable version, Stretch includes hundreds of new packages, and updates to thousands of others.

Since Debian powers a large percentage of web servers all over the world, in this article we will explain how to install the LAMP stack in Debian 9 Stretch.

This will allow system administrators to set up brand new web servers on top of Stretch using the recent updates to the distribution’s official repositories. It is assumed that you have installed Debian 9 on a bare metal, virtual machine, or VPS, or have upgraded from Jessie.

Installing LAMP in Debian 9 Stretch

The “M” in LAMP stands for MariaDB or MySQL, the database server for the stack. Depending on your choice, you can install the database server and the other components (the Apache web server and PHP) as follows.

Install LAMP with MariaDB on Debian 9

# aptitude update && aptitude install apache2 mariadb-server mariadb-client mariadb-common php php-mysqli

Install LAMP with MySQL on Debian 9

# aptitude update && aptitude install apache2 mysql-server mysql-client mysql-common php php-mysqli

As a personal preference, I will use MariaDB in the rest of this article.

Once the installation is complete, let us make sure all the services are running. If so, the following commands.

# systemctl is-active apache2
# systemctl is-active mariadb

should return active for both. Otherwise, start both services manually:

# systemctl start {apache2,mariadb}

Finally, before proceeding let’s use mysql_secure_installation to set up the password for the database root account. If you don’t know how to do this, you can refer to step #4 in How to Install MariaDB 10 on Debian and Ubuntu.

Testing LAMP on Debian 9 Stretch

To begin, we are going to create and populate a sample database. Next, we will use a basic PHP script to retrieve a set of records from the database in JSON format.

Finally, we will use Firefox’s developer tools to verify the version of Apache being used. Although we could find out this same information with.

# apache2 -v

the reason why we’re using a script is to make sure all the components of the stack are working properly when bundled together.

Creating and Populating a Database

Let’s enter the MariaDB prompt with the following command.

# mysql -u root -p

and entering the password that was chosen in the previous section.

Now we will create a database named LibraryDB as follows:

MariaDB [(none)]> CREATE DATABASE LibraryDB;

and add two tables named AuthorsTBL and BooksTBL:

MariaDB [(none)]> USE LibraryDB;

MariaDB [(none)]> CREATE TABLE BooksTBL (
Year VARCHAR(4),

For security reasons, we will create a special account to access our database:

MariaDB [(none)]> CREATE USER 'librarian'@'localhost' IDENTIFIED BY 'Today123';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON LibraryDB.* TO 'librarian'@'localhost';

The last step now consists of populating the tables with Authors and Books:

MariaDB [(none)]> INSERT INTO AuthorsTBL (FullName) VALUES ('Paulo Coelho'), ('Isabel Allende'), ('Jorge Luis Borges');
MariaDB [(none)]> INSERT INTO BooksTBL (AuthorID, ISBN, Title, Year) VALUES
(1, '9788576653721', 'El alquimista', '1988'),
(1, '9780061194740', 'El peregrino', '1987'),
(2, '9789500720380', 'La casa de los espiritus', '1982'),
(3, '9789875666481', 'El Aleph', '1945');

Testing Database Connection with PHP Script

The following PHP script will first connect to the database and retrieve the records that match the query. If an error occurs, a descriptive message will be displayed so as to give us a hint on what’s wrong.

Save the following script as booksandauthors.php under /var/www/html:

	// Show PHP version
	echo "Current PHP version: " . phpversion() .  "\r\n";

	// Connect to database
	$connection = mysqli_connect("localhost","librarian","Today123","LibraryDB") or die("Error " . mysqli_error($connection));

	// SQL query
	$sql = "SELECT A.ISBN, A.Title,
        	A.Year, B.FullName
        	FROM BooksTBL A JOIN AuthorsTBL B
        	ON A.AuthorID = B.AuthorID;";
	$result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));

	// Populate an array with the query results
	$libraryarray = array();
	while($row = mysqli_fetch_assoc($result))
    	$libraryarray[] = $row;

	// Convert to JSON
	echo json_encode($libraryarray);

Change the ownership to www-data and add the appropriate permissions:

# chown www-data:www-data /var/www/html/booksandauthors.php
# chmod 600 /var/www/html/booksandauthors.php

Finally, open a web browser and point it to the URL where the script resides.
Verify LAMP on Debian 9

Verify LAMP on Debian 9

That’s it! In this article we have explained how to install and test the LAMP stack on Debian 9. If you have questions or comments about this article, don’t hesitate to let us know by using the form below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

3 Responses

  1. Byron says:

    lol, past two days trying to get “Stretch” installed, downloaded Fedora, an hour later my home dev server was up and running. If Debian really wants users, they should consider making it easy to install, like most other flavors of Linux.

    • Gabriel A. Cánepa says:

      What difficulties did you face while trying to set up your home dev server with Debian? I have just installed Stretch myself and at one point I was asked what group of packages I wanted to install. Development (I don’t remember the exact name, though) was one of them.

    • Eddie G. says:

      I’ll second that sentiment! While I like Debian, there seems to be a huge issue regarding the wireless and being able to just install and go. I’ve tried over, and over, and over again to install it on some “not-that-old” laptops and everytime its the same issue: No Wireless. Cannot Connect To Wireless Network. Need To Find a hardwired connection in order to proceed with install.

      While this might not be an issue in a corporate office, it can become a real barrier when working in a remote location. And yes, I realize there are wifi files that you can download and install in order to get the wireless up and running. But why is this necessary?….you’re website (Debian’s!) claims: “Debian provides more than a pure OS: it comes with over 51000 packages, precompiled software bundled up in a nice format for EASY INSTALLATION on your machine.”

      I didn’t make that up…..I copied and pasted it from the Debian site! Now…unless you are lying?….you wither need to figure out a way to include the wireless drivers or some form of wireless functionality…(at least something to allow a person to connect to the internet and then download the drivers they might need!) or else REMOVE that statement from your website! Understand, not everyone who comes to the world of Linux is going to want to futz around with tar.gz files…or having to hunt down specific files…..just for what’s now considered “Basic Functionality”….I guess this is why so many people are turning to derivatives?….(Linux Mint…..Ubuntu…..OpenSuSE…..Fedora…etc.) C’Mon Debian!……get your act together!….its been long enough!!!

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.