How to Install LEMP Stack on Debian 9

The term LEMP is an acronym of the names of its four open-source components:

  • L - Linux operating system
  • E - Nginx [engine x] an HTTP and reverse proxy server
  • M - MySQL or MariaDB relational database management system
  • P - PHP programming language.

This series of tutorials will show you how to install Nginx, create Nginx server blocks, generate a free Let’s Encrypt SSL certificate, install and secure MySQL and install PHP.

The detailed tutorials that are part of this series are listed at the end of this page.

If you are in a hurry and don’t want to read more detailed documentation you can install LEMP Stack on your Debian 9 server by following the Quickstart section.

Install LEMP Stack on Debian 9 [Quickstart]

This quickstart will show you the basic steps required to get a LEMP stack installed on a Debian 9 server.

Prerequisites

The user you are logged in as must have sudo privileges to be able to install packages.

Step 1. Installing Nginx

Nginx is available in default Debian repositories. Update the packages index and install Nginx with the following commands:

sudo apt update
sudo apt install nginx

Nginx service will automatically start after the installation process is complete.

Step 2. Installing MariaDB

With the release of Debian 9, MySQL was replaced with MariaDB as the default database system. Install MariaDB by running the following command:

sudo apt install mariadb-server

Once the installation is completed, issue the mysql_secure_installation command to improve the security of the MySQL installation:

sudo mysql_secure_installation

You will be asked to set the root password, remove the anonymous user, restrict root user access to the local machine and remove the test database. You should answer “Y” (yes) to all questions.

If you want to install MySQL instead of MariaDB, check our tutorial for installation instructions.

Step 3. Installing PHP

Debian 9 ships with PHP version 7.0. To install PHP FPM and the most common PHP modules type:

sudo apt install php-fpm php-opcache php-cli php-gd php-curl php-mysql
To install PHP 7.2, visit this tutorial.

Step 4. Configuring Nginx to Process PHP Pages

Now that we have all of the LEMP components installed, we can edit the Nginx virtual host configuration file and add the following lines so Nginx can process PHP files:

server {

    # . . . other code

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

Do not forget to restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

More Information

For more detailed instructions about each step, please consult the following tutorials.

Tutorials