How to setup Linux, Apache, MySQL & PHP (LAMP) on Debian 9.4

Created by Jordy Leffers at 06-12-2017 11:05:35 +0100

LAMP is a bundle of open source software used to run web servers. In this tutorial we will go through the installation of three packages: Apache, MySQL and PHP. We will skip Linux since our virtual server is already running Debian and this can easily be acquired through one of the default Linux installations on the website.

This tutorial is based on cloud containers created on the page. This means that by default you are the root user, so all of the command below don't make use of sudo. If however, you're not the root user on your system, you'll have to add "sudo" in front of the commands found in the guide below.

Before we start installing anything, it's good practice to make sure all of our repositories are up-to-date using the following commands:

apt-get update

apt-get upgrade


Install Apache

apt-get install apache2

You'll be prompted to allow extra disc space to be used: press Y

How to find your IP: ip addr | grep inet

To check if Apache installed correctly: navigate to your IP address in browser, you should now see a page telling you everything works correctly like this:

Install MySQL

apt-get install mysql-server


Should Mariadb.service on systemd return an error looking like this:

mariadb.service: Failed at step NO_NEW_PRIVILEGES spawning /usr/sbin/mysqld: Invalid argument)

skip to the bottom of this page to find the fix.


After installing MySQL, finish the setup by running the following script:


Your root password will be prompted, just like some additional questions. You can answer yes to all these questions.

Now, we're almost finished, we only have PHP left to install.


Install PHP

Use the following command to install PHP:

apt-get install php php-pear

You'll be about the use of disc space, answer yes.

Now restart Apache:

service apache2 restart

Congratulations, you've now installed LAMP!


Test PHP

Despite having installed PHP, you might still want to test it. 

We can easily test it by adding a script that will output some data about our server.

create a new php file:

vim /var/www/html/info.php

Add the following script:

Save and close the file. Go to your browser and navigate to this address:


You should get a screen, displaying all kinds of server information.


Finally, delete the php script so no unauthorized users get acces to your server's information.

rm /var/www/html/info.php

Congratulations, you've now installed and tested LAMP on your Debian installation!




How to fix the mariadb.service systemd error?

Go into the /etc/systemd/system/ folder and create a new folder called "mariadb.service.d"

cd /etc/systemd/system

mkdir mariadb.service.d

Here, create a new file called "override.conf":

vim override.conf 

 Insert the following text:

Save and close the file. Installing MySQL with MariaDB should now work!


Comments are turned off.