This tutorial shows you how to install a LAMP server in Debian 9 Stretch. A LAMP (Linux, Apache, MySQL and PHP) server can be used to host dynamic web content such as a WordPress site. Debian Linux is a very stable distribution, which also contains a huge amount of software in its repositories. This makes it ideally suited as a server operating system. Apache is a powerful and flexible web server that integrates well with PHP to produce dynamic content.

I am based in Australia and I’ve found Vultr to be great for fast, modern VPSs. There are other providers out there, but I’ve yet to find one that matches Vultr for competitive pricing and speed. If I lived somewhere where DigitalOcean have local servers, they are also a great option (disclosure: both of these are affiliate links). Both of them offer up-to-date system images, and both have Debian 9 Stretch images available. For this guide I am going to assume that you have a clean, secured installation of Debian 9 Stretch, and that you’re working as root user. If you prefer, you can just precede the commands with sudo. If you’re running as a sudo user, it seems easier to just become root, like this (you will need your user password here, NOT the root password):

root@system:~# sudo su

First we should bring our system fully up to date:

root@system:~# apt update
root@system:~# apt -y upgrade

We’re going to be installing a basic LAMP server. You already have the Linux bit covered if you are using Debian 9. Start by installing Apache and required php modules:

root@system:~# apt install apache2
root@system:~# apt install php libapache2-mod-php php-mcrypt php-mysql

We may also want to install other handy PHP modules. I like:

root@system:~# apt install php-curl php-zip php-mail

Make sure Apache is running and enabled:

root@system:~# systemctl start apache2
root@system:~# systemctl enable apache2

When you check:

root@system:~# systemctl status apache2

You should see something like the following:

root@system:~# root@system:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: **active (running)** since Thu 2017-08-24 13:43:37 AEST; 1 day 6h ago
Process: 28644 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
Main PID: 19157 (apache2)
Tasks: 9 (limit: 4915)
CGroup: /system.slice/apache2.service
├─ 2156 /usr/sbin/apache2 -k start
├─19157 /usr/sbin/apache2 -k start
├─28665 /usr/sbin/apache2 -k start
├─28666 /usr/sbin/apache2 -k start
├─28667 /usr/sbin/apache2 -k start
├─28668 /usr/sbin/apache2 -k start
├─28670 /usr/sbin/apache2 -k start
├─28726 /usr/sbin/apache2 -k start
└─29392 /usr/sbin/apache2 -k start
Aug 24 13:43:37 my.website.com systemd\[1\]: Starting The Apache HTTP Server...
Aug 24 13:43:37 my.website.com systemd\[1\]: Started The Apache HTTP Server.

Now open port 80 on your firewall (this assumes you’re using ufw):

root@system:~# ufw allow http

And check that it’s open:

root@system:~# ufw status
Status: active

To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)

Check that PHP is working: Create info.php in your web root directory:

root@system:~# nano /var/www/ltb/html/info.php

Add the following contents to the file:

<?php
  phpinfo();
?>

Save and then load it in your browser:

http://your.server.address/info.php

If all is working as it should, you should see something like this:

You should see the contents of your info.php file describing the configuration of your server

Once you’ve established that Apache and PHP are up and working it would be best to erase this file, as it gives a LOT of system information to wannabe hackers. This is as easy as:

root@system:~# rm /var/www/ltb/html/info.php

Next we need to install and initialise a database. For this tutorial we’ll be using MariaDB.

root@system:~# apt install mariadb-server

Make sure your installation is secure by running:

root@system:~# mysql\_secure\_installation

You can just hit enter when asked for the current root password. Set a new root password (and keep it safe!) and answer ‘yes’ to all the other questions.

There is a bit of system tidy-up and configuration to be done. To avoid getting some Apache errors we need to add server name information to the Apache main configuration file.

root@system:~# nano /etc/apache2/apache2.conf

We need to add an extra line telling Apache the name or IP of your server. You can use your FQDN if it has DNS records pointing to it, your main domain name hosted in this server, or just your IP address. Insert this line (modified to suit you) right at the bottom of the file:

ServerName your_server_domain_name_or_IP_address

Next you need to decide on the directory structure for your site (or sites). A popular way to go is to create separate directories in /var/www

Let’s say that your website is going to be called example.com. I’m going to assume that you have set up the DNS to point your domain name at your server.

root@system:~# mkdir /var/www/example.com
root@system:~# mkdir /var/www/example.com/html

We also need to tell Apache where to find your website-to-be. This is done by creating a new Apache virtual host in /etc/apache2/sites-available

root@system:~# nano /etc/apache2/sites-available/example.com.conf

You can copy /etc/apache2/sites-available/000-default.conf as a base. Once you trim all the fat (comments), you should end up with something like this:

<VirtualHost *:80>
	ServerName www.example.com
	ServerAlias example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/example.com/html

	<Directory /var/www/>
		Options -Indexes +FollowSymLinks
		AllowOverride All
		Require all granted
	</Directory>

	ErrorLog ${APACHE\_LOG\_DIR}/error.log
	CustomLog ${APACHE\_LOG\_DIR}/access.log combined
</VirtualHost>

And we need to let Apache know to make the website public:

root@system:~# a2ensite example.com

We need to remove the default site:

a2dissite 000-default

We should reload Apache to load up all our changes:

root@system:~# systemctl reload apache2

If there are no error messages you’re good to go. Create a dummy web page to test that the site is working:

root@system:~# nano /var/www/example.com/html/index.html

And write in something. For example:

<h1>This is my test website, example.com</h1>

Save, and then point your browser at your page. You should see your content:

If all goes well you should see your index page being served by your brand new LAMP server

This is it! You now have a functional LAMP server in Debian 9 Stretch, capable of serving dynamic web content such as WordPress websites. If you’re interested in how to install WordPress in your new server, I have another tutorial covering this: How to install WordPress in Debian 9 Stretch.