💾ServerLemp Stack

A LEMP stack (Linux, (E)NGINX, MySQL, PHP stack) is a software stack useful for hosting websites and web services that rely on PHP scripting and a MySQL database for interactive websites.

Initial Setup

This section covers the installation and basic configuration of a LEMP stack; It's important to note that the "M" in "LEMP" may be replaced by other databases; This will depend on what PHP software you wish to run, or your database of choice.


  • A VPS or local server for self-hosting

  • Ports 80 and 443 port-forwarded on your router/modem

  • Your own domain with a DNS entry set to the public IPv4 address of your server

  • Basic UNIX knowledge

The L

One of the most recommended Linux distros for server hosting is Debian 10; This is what will be used as example in this tutorial; while configuration and NGINX entries should remain the same accross distributions, the versions of PHP and MySQL in other distros may be different to Debian.

For more information, see the Self-Hosting/Debian page on this wiki.

The E

Quite confusingly, the "E" in "LEMP" infact stands for (E)NGINX, the web server and reverse-proxy software used to serve html web pages; This is stemmed from the pronounciation of NGINX, "Engine-X", which to anyone who hears it may indicate NGINX is spelled differently, namely with an E at the start.


NGINX is available in the Debian repositories:

sudo apt install nginx


As explained in [self-hosting/nginx | [NGINX]]:

By default, NGINX on Debian scans the /etc/nginx/sites-enabled/ directory for webserver configuration files. The instruction to do so is included in the /etc/nginx/nginx.conf file.

It is recommended to place server configuration files in the /etc/nginx/sites-available directory, and then symbolically linking them to /etc/nginx/sites-enabled to let NGINX see the configurations.

This essentially means we must create configuration files for NGINX websites in /etc/nginx/sites-available and then use these commands to enable/disable them:

 = Enabling a configuration file:
sudo ln -s /etc/nginx/sites-available/YOUR_DOMAIN /etc/nginx/sites-enabled/
 = Disabling a configuration file:
sudo unlink /etc/nginx/sites-enabled/YOUR_DOMAIN

Example PHP-FPM Configuration

An example configuration for a website is like so:

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

        root /var/www/YOUR_DOMAIN;
        index index.php;
        server_name YOUR_DOMAIN;
        location / {
            try_files $uri $uri/ =404;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;

In this example, a PHP file is present in /var/www/YOUR_DOMAIN named index.php and NGINX is using php-fpm to serve the page;

Please note the fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; line! The /var/run/php/php7.3-fpm.sock part points towards the php-fpm version you have installed; In this case, the default version in the Debian 10 repositories is php7.3, thus /run/php/php7.3-fpm.sock. If you are using a different version of php-fpm, edit this line accordingly.

The M

MySQL is a popular database choice for PHP scripts and software; To install in on Debian one can either use the MariaDB package:

sudo apt install mariadb-server

Or use the official MySQL repos:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.16-1_all.deb
sudo dpkg -i mysql-apt-config*
sudo apt update
sudo apt install mysql-server

There's always the option to use a different database; For example, PostgreSQL:

sudo apt install postgresql

Or SQLite3:

sudo apt install sqlite3

Once again, this depends on what the PHP software you wish to run supports. For example, Nextcloud supports MySQL/MariaDB, PostgreSQL, and SQLite3.

To actually use any of these databases, it's recommended to run their systemd services:

sudo systemctl restart mysql
sudo systemctl restart mariadb
sudo systemctl restart postgresql

(Any changes you make to database configuration will only apply if you restart their services.)

The P

To properly serve PHP webpages with NGINX, one must use php-fpm; A FastCGI Process Manager. The default Debian repositories install version 7.3 of PHP and PHP-FPM:


sudo apt install php php-fpm

(This will install php and php-fpm version 7.3 on Debian 10)

However, if you wish to install PHP 7.4 and newer, you must use the sury repositories:

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update
sudo apt install php php-fpm

(The commands above will install php and php-fpm version 8.0)

If you want a specific version of PHP, simply add the version's number after <code>php</code> for your package name:

sudo apt install php7.4 php7.4-fpm php7.4-mysql

To enable support for whichever database you choose to use, install the appropriate PHP package:

sudo apt install php-mysql php-pgsql php-sqlite3

Configuration and Service

As mentioned before, to properly serve a PHP web page with PHP-FPM a specific NGINX configuration is required.

PHP-FPM itself should be used through a systemd service installed with it's package; simply restart it with:

sudo systemctl restart php7.3-fpm

(Replace <code>7.3</code> with your installed version of PHP and PHP-FPM.)


To test this entire configuration out, we can put some PHP code in /var/www/YOUR_DOMAIN/index.php:

sudo echo "<?php  phpinfo();  ?>" >> /var/www/YOUR_DOMAIN/index.php

Then restart the services:

sudo systemctl restart php7.3-fpm nginx

And your domain name should resolve to a PHP info page!