How to Install a LAMP Server on Debian 9 Stretch Linux

Introduction

The LAMP server is the cornerstone of Linux web hosting. In the early days of dynamic web content LAMP was what won Linux the crown in the web space, and it still is responsible for powering a very large portion of the Internet’s sites.

If you’re looking to set up a LAMP stack to host your website, it’d be hard to find a better option to build it on than Debian Stretch. Debian is, after all, well known for its stability, security, and massive package repositories, and Stretch is certainly no exception.



MariaDB(MySQL)

To get started, install and setup the database portion of the stack, MariaDB. Traditionally, the “M” in LAMP stands for MySQL. However, MariaDB is a drop-in replacement that isn’t controlled by Oracle, so it tends to be a better option.

To install MaridaDB on Stretch, just use apt to install the packages.

# apt install mariadb-client mariadb-server

During the install process, you will be prompted to create a root password for MariaDB. Make sure to choose something as secure as possible, since it will determine, in part, the security of your databases.

Now that the MariaDB server is installed, you can log in as your root user and set up a regular user and a database.

mysql -u root -p

MariaDB will then prompt you for the root password that you just set up.

Creating a database is fairly simple. Just run the following.

CREATE DATABASE newdb;

You need to create a regular user now to use the database. It is an absolutely terrible idea to use the root user for anything other than managing MariaDB as a whole.

CREATE USER 'username'@'localhost' IDENTIFIED BY 'userpassword';

That command creates a regular user that can sign in locally and set that user’s password.

In order for that user to be able to use the database that you just created, you need to grant them privileges on it. Since this is a general purpose user for managing everything on this database, it will be given all privileges.

GRANT ALL PRIVILEGES ON newdb.* to 'username'@'localhost';

Once that’s done, flush all privileges from the console and exit.

FLUSH PRIVILEGES;
quit

That’s all for the database. Certainly, you can customize any portion of this as you need.



PHP

The next step in getting the LAMP server set up is installing PHP. In the LAMP stack, PHP powers the web content and interacts with the database. To install PHP on Debian Stretch, run the following line.

# apt install php7.0 php7.0-mysql

That’s really all that you need. PHP is now ready to use.

Apache

The Apache web server is extremely powerful and can be extremely easy to set up or ridiculously difficult, depending how in-depth you wan to go. Because this is just a simple guide, it’s going to follow the quickest path for getting a basic server set up.

So, install both the Apache server and the module for PHP support.

# apt install apache2 libapache2-mod-php7.0

Testing Your Server

By default, Apache will server the contents of /var/www/html and will look first for a file called index.php or index.html. Create that file, and place the following line of code in it.

<?php phpinfo(); ?>

Open up your browser and type in localhost in your address bar. If you aren’t doing this locally, type your domain name or IP. You should see a long table containing information about your PHP install. At this point, your sever is officially working.

If you want an easy way to manage your database through a graphical web interface, you can install an application called, phpmyadmin. It allows you to manage your database using PHP through your LAMP server. To install it on Stretch, just pull it with apt.

# apt install phpmyadmin

Once the package installs, you can navigate in your browser to localhost/phpmyadmin You will be greeted with a login screen that will accept your database credentials and finally, an interface to work with your database.

Closing Thoughts

Your LAMP server is now ready to go. Of course, there are tons of other options, and if you plan to use this as a public facing server, you may want to look into more security options for Apache. That said, this LAMP server can run everything from your custom PHP application to popular solutions like WordPress and even development frameworks like Laravel.