Debian 9 LAMP Server Tutorial with Apache, PHP 7 and MariaDB

LAMP is short for Linux, Apache, MySQL, PHP. This tutorial shows how you can install an Apache web server on a Debian Stretch (9) server with PHP 7 (mod_php) and MariaDB support. MariaDB is a fork of the well known MySQL database server, it provides a MySQL compatible feature set and is a bit faster according to benchmarks that I found on the internet. MariaDB will work with all applications that require MySQL like Wordpress, Joomla etc.

A LAMP setup is a perfect basis for CMS systems like Joomla, Wordpress or Drupal.

1 Preliminary Note

In this tutorial, I use the hostname with the IP address These settings might differ for you, so you have to replace them where appropriate.

2 Installing MariaDB as MySQL replacement

First, we install MariaDB like this:

apt-get -y install mariadb-server mariadb-client

Next, we will secure MariaDB with the mysql_secure_installation command. Run the below command and follow the wizard.


The recommended input is shown in red.

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): <-- Hit return
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] <-- y
New password: <-- Enter the new password for the MariaDB root user
Re-enter new password: <-- Enter the password again
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <-- y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <-- y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <-- y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <-- y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

The MariaDB setup is secured now.

3 Installing Apache web server

Apache is available as a Debian package, therefore we can install it like this:

apt-get -y install apache2

Now direct your browser to, and you should see the Apache2 placeholder page (It works!):

Debian 9 Apache default page

Apache's default document root is /var/www on Debian, and the configuration file is /etc/apache2/apache2.conf. Additional configurations are stored in subdirectories of the /etc/apache2 directory such as /etc/apache2/mods-enabled (for Apache modules), /etc/apache2/sites-enabled (for virtual hosts), and /etc/apache2/conf-enabled.

4 Installing PHP 7.1

We can install PHP and the Apache PHP module as follows:

apt-get -y install php7.0 libapache2-mod-php7.0

We must restart Apache afterward:

service apache2 restart

5 Testing PHP / Getting details about your PHP installation

The document root of the default web site is /var/www/html. We will now create a small PHP file (info.php) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

nano /var/www/html/info.php

Now we call that file in a browser (e.g.

PHP installed on Debian 9 in Apache

As you see, PHP 7.0 is working, and it's working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL / MariaDB is not listed there which means we don't have MySQL support in PHP5 yet.

6 Getting MySQL and MariaDB Support in PHP

To get MySQL support in PHP, we will install the php7.0-mysql package. It's a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP 7 modules like this:

apt-cache search php7.0

Pick the ones you need and install them like this:

apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl

Now restart Apache:

service apache2 restart

7 PHP Cache to improve the PHP speed

To speed up PHP, an Opcache should be installed. Check if the PHP Opcache module has been installed and enabled correctly.Run this command:

php --version

The output shall contain the line I marked in red.

PHP 7.0.27-0+deb9u1 (cli) (built: Jan 5 2018 13:51:52) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.27-0+deb9u1, Copyright (c) 1999-2017, by Zend Technologies

If you do not see the Opcache module in the result, install it with this command:

apt-get -y install php7.0-opcache

There is one more cache which might be useful, it's name is APCu. APCu is a free PHP opcode cacher for caching and optimizing PHP intermediate code.

APCu can be installed as follows:

apt-get -y install php-apcu

Now restart Apache:

service apache2 restart

Now reload in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module which is used as MariaDB driver:

MySQL support enabled in PHP 7

8 phpMyAdmin

phpMyAdmin is a web interface through which you can manage your MySQL and MariaDB databases. It's a good idea to install it:

apt-get -y install phpmyadmin

You will see the following questions:

Configure phpMyAdmin for Apache

Web server to reconfigure automatically: <-- apache2

Use dbconfig-common to configure phpMyAdmin

Configure database for phpmyadmin with dbconfig-common?<-- Yes

Automatically create an application password

MySQL application password for phpmyadmin: <-- Press enter, apt will create a random password automatically.

Afterwards, you can access phpMyAdmin under

PHPMyAdmin on Debian 9

9 Enable MySQL root Login for phpMyAdmin

While you can log in as root user into MariaDB on the shell, the root login will not work in phpMyAdmin. To allow the root user to use phpMyAdmin as well, run the following command on the shell:

echo "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';FLUSH PRIVILEGES;" | mysql -u root -p
Share this page:

Suggested articles

21 Comment(s)

Add comment


By: ejjus

Step 8 doesn't work without :

A cleaner way is to create the new configuration file:


and write the following in it:

Include /etc/phpmyadmin/apache.conf

then, soft link the file to the directory /etc/apache2/conf-enabled:

sudo ln -s /etc/apache2/conf-available/phpmyadmin.conf /etc/apache2/conf-enabled

By: till

What you described is necessary just in case you missed selecting "Apache2"  in the first apt dialog from Stp 8 shown in this tutorial. The better and easier way is to just follow the tutorial and select 'Apache2' when apt ask you which web server shall be configured.

By: ejjus

I actually came back to add that I might have wrongfuly selected or typed something. It definetally is a misstep on my part, because I struggled with some server internal things for 5 or 6 hours prior to setting up LAMP. I should have worded my first comment differentely, more like in the format of "In my case..."

Have a nice day.

By: ejjus

I didn't try the echo on the last step. This did the same:


Maybe a bit late, but I found this answer looking over the internet. It could help others with the same problem.

$sudo mysql -u root

[mysql] use mysql;

[mysql] update user set plugin='' where User='root';

[mysql] flush privileges;

[mysql] \q

Now you should be able to log in as root in phpmyadmin.

Nice tutorial. Helped me a lot. Cheers.

By: Michael Guldhammer

Run theese 2 commands again, it works for me.

systemctl start mariadb mysql_secure_installation

By: Hopy

in file info.php

You forget ?>

By: till

I don't forgot it, the closing ?> is not required anymore in current PHP versions. Try it out, you will see it works without it :)

By: Vassilis

Thank you for your sharing and your secure setup. I have just installed this for a new website development on a debian9 LXC container through proxmox, everything works perfect. thank you again

By: jim

While I do appreciate Till's site and sharing... I'm not sure this should be considered a "secure setup."

AppArmor/SELinux is disabled, access to MariaDB beyond localhost, root access to phpMyAdmin/SSH, no key pair access, etc.

A series of articles entitled "Secured Perfect Servers" might be interesting.

By: Patrice

Works like a charm ! 


Many thanks :) 


By: Baptiste


( Sorry i'm french )


I have been follow your tutorial but on the step 8 at the end Phpmyadmin not work don't know why .... can you help me pls


Thanks ^^

By: Abdurakhman

so i fixed by this command

sudo ln -s /usr/share/phpmyadmin /var/www/html/

By: till

The error means that you did not select "apache" as described in the tutorial when the phpmyadmin installer asked you "Web server to reconfigure automatically". You can redo the phpmyadmin configuration with the command dpkg-reconfigure phpmyadmin

Ensure to select apache2 by navigation to that point with the tab key on your keyboard and then select it with the space key. If you miss to select it with space, then the option is not used by apt and phpmyadmin will not get activated for apache.

By: Leilf Falkenstrom

Very good and clear instruktion, but I would recommend that the instruktions updates to install a newer version of php because Joomla will soon stop supporting that old version. When update you might update the path for joomla too as it have got closer to version 4 (3.8.14 today)

Now when most webb sites use https would it be nice for that to be included in the guide too.

By: Gilson

 Prob: phpmyadmin: "Access denied for user 'root'@'localhost'"

I followed the instruction but cannot login to phpmyadmin. I tries both, the echo code and the "

$sudo mysql -u root

[mysql] use mysql;

[mysql] update user set plugin='' where User='root';

[mysql] flush privileges;

[mysql] \q"

But didn't work.

So, how can I solve this? Paword root phpmyadmin...



By: sky

Nice tutorial

Helped me a loot

Many thanks


By: piotao

on fresh debian install, the step 2 is NOT WORKING, because there apparently IS the password in root account set in the database, so:

Enter current password for root (enter for none): <-- Hit return DO NOT WORK.

By: till

Step 2 is working fine as it is. I've installed it on a fresh Debian system today, no default password was set there. It might be thought that you used a different base system from an ISP which had MariaDB installed and therefore a password was set already, in that case, you should enter the password that you had set when the command requests it.

Please don't claim that a step is wrong just because your particular server as a different preset of applications than a default empty Debian server.

By: Gustavo

Hola Till, muchas gracias por tu aporte, muy útil.

Yo tengo una Raspberry Pi 3 y de vez encuando reinstalo el Raspbian (cuando empiezo a tener problemas de lentitud entre otras cosas), como estoy aprendiendo PHP, HTML y MARIADB también instalo LAMP y así ir familiarizandome con los software.

Mi problema comienza cuando instalo PHPMYADMIN, siempre, siempre tengo problemas al ejecurtarlo no me reconoce el USUARIO ni el PASSWORD, me he guiado con una página distinta cada vez que instalo el paquete y siempre lo mismo, a la hora de acceder a PHPMYADMIN el mismo resultado incluyendo tu tutorial.

Los mensajes que me da PHPMYADMIN son:

1.- #1698 - Access denied for user 'root'@'localhost'

2.- mysqli_real_connect(): (HY000/1698): Access denied for user 'root'@'localhost'

Cabe señalar que cada vez realice los pasos lo hice al pie de la letra y debo buscar tutoriales más invasivos con el software para poder romper la barrera que impide ingresar a la primera.

By: Kachro

Installed Apache2 and PHP7.0 on Debian 9 (Stretch). I left out the phpMyAdmin for a later date, once I understand the security aspects better.

Went smoothly. Thanks for the clear instructions!

By: Tom Travis

I would love to see his updated using PHP8.1 or whatever the latest version is.  It took me some stumbling around but I got it to work:


mysql_secure_installationnow asks whether you want to switch to unix socket authentification. I chose NO. Step 9 throwing an error: ERROR 1356 (HY000) at line 1: View 'mysql.user' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them. This is where I fumbled around and got it to work, maybe because I tried entering the commands from the earlier comment below.