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:

21 Comment(s)