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
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.

Facebook Comments

More Stuff

Emblematic Group and Mozilla Team Up to Showcase Next Generation o... Everything you share on the internet is a story. You read blog posts and watch videos that make you feel connected to people across the world. Virtual...
Latest Firefox Release is Faster than Ever With the introduction of the new Firefox Quantum browser in 2017 we changed the look, feel, and performance of our core product. Since then we have la...
Competing without a Marketing Budget: When All You Have is a Produ... This is a story about the power of a good landing page, PR efforts, and marketing strategy. The refreshing twist here is that instead of explaining ho...
The Ongoing Fiasco and Mysterious Afterlife of QuadrigaCX I’d say you were a little late to the cryptocurrency party if you haven’t heard about the ongoing catastrophe that’s been QuadrigaCX these past six ...
Spread the love

Posted by News Monkey