Debian webserver II: install LAMP stack

October 10, 2020 - Reading time: 4 minutes

This tutorial, part of a series that will guide you in making a Debian webserver, will show you how to install the LAMP stack; Apache with PHP and MySQL (MariaDB). The tutorials work on Debian 10 (Buster) but will probably work on older versions and newer versions to come.

Always make sure your system is up to date!

$ sudo apt update && sudo apt upgrade

1. Apache
Install Apache and the Apache utilities and check its status.
$ sudo apt install apache2 apache2-utils
$ systemctl status apache2

If it doesn't say 'running', start it manually. 
$ sudo systemctl start apache2

Enable apache at boot.
$ sudo systemctl enable apache2

Open the default port in ufw (you can check this with $ sudo ufw app info 'Apache' )
$ sudo ufw allow 80/tcp

Check to see if the servername is correct.
$ sudo apache2ctl -t

If it doesn't say 'Syntax OK', change the name (best to go with the system's hostname).
$ sudo vim /etc/apache2/conf-available/servername.conf

ServerName host.name.net


Activate the configuration and reload apache.
$ sudo a2enconf servername.conf
$ sudo systemctl reload apache2

2. MariaDB databaseserver (MySQL)
$ sudo apt install mariadb-server mariadb-client

Check the status.
$ sudo systemctl status mariadb

If it doesn't say 'running', start it manually. 
$ sudo systemctl start mariadb

Make MariaDB start at boot.
$ sudo systemctl enable mariadb

Start the postinstallation script and choose a root password, in most cases you can answer all other questions with yes e.g. 'Y'.
$ sudo mysql_secure_installation

Try to log in (no password needed).
$ sudo mariadb -u root or $ sudo mysql -u root

Log out with
> exit;

3. PHP (skip this step and go to 3.x if you want to use PHP-FPM)
Install the PHP modules. At the time of writing PHP 7.3 is the latest stable version.
$ sudo apt install php7.3 libapache2-mod-php7.3 php7.3-mysql php-common php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline

Activate PHP en restart apache.
$ sudo a2enmod php7.3
$ sudo systemctl restart apache2
                     
Test if PHP works with a test file.
$ sudo vim /var/www/html/info.php

<?php phpinfo(); ?>


Save the file and point your browser to it. You should see a long list of information. After that, delete the file, it is a potential security risk (but fine for testing).
$ sudo rm /var/www/html/info.php

3.x PHP-FPM (optional, instead step 3)
Install the PHP-FPM module. At the time of writing PHP 7.3 is the latest stable version.
$ sudo apt install php7.3-fpm

Activate the proxy_fcgi and setenvif modules.
$ sudo a2enmod proxy_fcgi setenvif

Activate PHP-FPM and restart apache.
$ sudo a2enconf php7.3-fpm
$ sudo systemctl restart apache2
                     
Test if PHP works with a test file.
$ sudo vim /var/www/html/info.php

<?php phpinfo(); ?>


Save the file and point your browser to it. You should see a long list of information. After that, delete the file, it is a potential security risk (but fine for testing).
$ sudo rm /var/www/html/info.php

That's it!