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 server1.example.com with the IP address 192.168.1.100. 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.

mysql_secure_installation

The recommended input is shown in red.

mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

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 http://192.168.1.100, 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

<?php
phpinfo();

Now we call that file in a browser (e.g. http://192.168.1.100/info.php):

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 http://192.168.1.100/info.php 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 http://192.168.1.100/phpmyadmin/:

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

Till Brehm

About Till Brehm

Over 20 years experience as Software Developer and Linux System Administrator. Till Brehm is the founder and lead developer of the ISPConfig Hosting Control Panel software (since 2000) and he founded HowtoForge in 2005 as a place to share Linux knowledge with other Linux enthusiasts.

Share this page:

Suggested articles

9 Comment(s)

Add comment

Comments

By: ejjus at: 2018-05-10 22:01:02

Step 8 doesn't work without :

A cleaner way is to create the new configuration file:

/etc/apache2/conf-available/phpmyadmin.conf

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 at: 2018-05-11 05:40:12

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 at: 2018-05-11 19:06:02

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 at: 2018-05-10 22:02:32

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: Hopy at: 2018-06-01 13:26:43

in file info.php

You forget ?>

By: till at: 2018-06-01 13:28:38

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 at: 2018-06-28 09:45:15

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 at: 2018-07-27 18:31:26

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 at: 2018-09-13 12:04:14

Works like a charm ! 

 

Many thanks :) 

Pat