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

Spread the love

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;
CREATE TABLE AuthorsTBL (
AuthorID INT NOT NULL AUTO_INCREMENT,
FullName VARCHAR(100) NOT NULL,
PRIMARY KEY(AuthorID)
);
MariaDB [(none)]> CREATE TABLE BooksTBL (
BookID INT NOT NULL AUTO_INCREMENT,
AuthorID INT NOT NULL,
ISBN VARCHAR(100) NOT NULL,
Title VARCHAR(100) NOT NULL,
Year VARCHAR(4),
PRIMARY KEY(BookID),
FOREIGN KEY(AuthorID) REFERENCES AuthorsTBL(AuthorID)
);

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:

<?php
// 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.

http://192.168.0.35/booksandauthors.php

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
Spread the love

Posted by News Monkey