How to Set Up a LEMP Server on Debian 10 Buster

LEMP is an excellent alternative to traditional LAMP servers. Nginx is lighter weight and faster than Apache in some situations. It can also be configured to do other useful things, like serve as a reverse proxy. Just like with LAMP, Debian is an excellent platform for LEMP servers. Everything you need is available in the Debian repositories, so it’s simple to get started.

In this tutorial you will learn:

  • How to Set Up MariaDB
  • How to Install PHP
  • How to Install Nginx
  • How to Configure Nginx
  • How to Test Your Server

PHPinfo on LEMP on Debian 10

PHPinfo on LEMP on Debian 10.

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Debian 10 Buster
Software Nginx, MariaDB, and PHP
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Set Up MariaDB

The best place to start in setting up your LEMP server is the database. For this guide, MaraiDB will serve as the database. Since it’s a drop-in replacement for MySQL, MariaDB is an excellent option.Before you can use MariaDB for any of your projects, you’re going to need to configure it. It doesn’t require much to get an app like WordPress running, so it won’t take long.

Install MariaDB

Start off by actually installing the MariaDB server.

$ sudo apt install mariadb-server


Set Up The Database

With MariaDB installed, you’re ready to get your database ready to work with web apps. MariaDB has a script that will help you automatically secure your database server, so begin by running it.

$ sudo mysql_secure_installation

Follow the prompts in the script. Set up a root password, when you’re asked. The defaults are good for everything else.

Now, log in to MariaDB using the mysql command and specifying the root user.

# mysql -u root -p

Create a database for your project.

CREATE DATABASE newdb;

Next, make a user that will connect to and manage the database.

CREATE USER 'username'@'localhost' IDENTIFIED BY 'userpassword';

That user will need full control of the database to be able to run a web applicaiton. Grant it all privileges on the database and its tables.

GRANT ALL PRIVILEGES ON newdb.* TO 'username'@'localhost';

Everything’s set up, and ready to use. Flush your privileges, and exit MariaDB.

FLUSH PRIVILEGES;
\q

Install PHP

PHP is the next piece of the puzzle. You’re not going to need Debian’s whole PHP package, only PHP-FPM. Install it with the MySQL module.

$ sudo apt install php-fpm php-mysql

Install Nginx

Almost everything is in place. You just need to set up the web server, Nginx. To start, install Nginx on your system from the Debian repositories.

$ sudo apt install nginx

Configure Nginx

Nginx is a powerful web server, and it offers a ton of options. That also mens that there’s a lot of configuration needed to get it running exactly the way you want. This guide is going to get you a basic working setup, but you can do a lot more with Nginx.

Nginx site configurations are stored in /etc/nginx/sites-available and linked to /etc/nginx/sites-enabled. Create a new file for your server’s configuration in /etc/nginx/sites-available, and open it in your text editor.

Start by creating a server block to tell Nginx that this is a new site configuration.

server {
}


The rest of your configuraiton is going to go in that block. Next, add the listen addresses. These are the port numbers followed by important attributes of your site. If this is going to be your only or default site, add default_server after the port number.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
}

Add in the web root folder where you want to put your site. /var/www/html is the usual default. Follow that by letting Nginx know the name of your site’s index, better known as the home page.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /ver/www/html;
    index index.php index.html;
}

Now, add your site’s domain name as the server_name. If this is just a local server, use _ in place of a domain.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /ver/www/html;
    index index.php index.html;

    server_name yourwebsite.com;

}

This next configuration block tells Nginx to check for files matching the current web address.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /ver/www/html;
    index index.php index.html;

    server_name yourwebsite.com;

    location / {
        try_files $uri $uri/ =404;
    }

}
Nginx config for LEMP on Debian 10

Nginx config for LEMP on Debian 10.

Finally, this last block tells Nginx to pass off PHP files to PHP-FPM. That’s where the bulk of the work is going to get done.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /ver/www/html;
    index index.php index.html;

    server_name yourwebsite.com;

    location / {
        try_files $uri $uri/ =404;
    }

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

}


When you’re done, save your configuration, and exit. Now, link it to sites-enabled.

$ sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/your-site

Then, restart the server to enable the site.

$ sudo systemctl restart nginx

Test Your Server

The easiest way to make sure that your server is working right is to create a new PHP file in your web root to run some PHP code. Create index.php at /var/www/html/, and open it with your text editor. Put the following line of code in the file, and save it.

<?php phpinfo(); ?>

Open your browser, and navigate to your server. You should see a table of PHP information about your server. If you do, your server is working normally, and it’s ready for your PHP application, like WordPress.

Conclusion

From there, you can do just about anything. The server configuration will remain in place, and everything should stay functional, unless you change it. You can also add multiple more sites to the same Nginx server with similar configruations. Just change the server_name to point to a different domain.