Setting Up A Lemp Stack

The LEMP stack is a software stack for web development that consists of four key components: Linux, Nginx, MySQL or MariaDB, and PHP, Perl, or Python. It is an alternative to the more widely known LAMP stack, which uses Apache instead of Nginx.

Let's get hands-on experience in setting this up. In case you missed my post on setting up a LAMP stack click here.


  • Cloud Service Provider - AWS, Azure, GCP, etc.

  • Knowledge of how to Launch a Linux server(Ubuntu preferably).

  • Prior knowledge of how to SSH into a remote server.


We will use Nginx, a high-performance web server, to show web pages to our site visitors. To install this package, we'll utilize the apt package manager. Because this is our first time using apt, begin by updating your server's package index. After that, you can install Nginx with apt install:

sudo apt update

sudo apt install nginx

When prompted, enter Y to confirm that you want to install Nginx

To verify that nginx was successfully installed and is running as a service in Ubuntu, run:

sudo systemctl status nginx

If it is green and running, then you did everything correctly.

Open a web browser of your choice and try to access the following URL



Again, use apt to acquire and install this software

sudo apt install mysql-server

When prompted, confirm installation by typing Yand then ENTER. When the installation is finished, log in to the MySQL console by typing

sudo mysql

Define the user's password using this command

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord'

Exit the MySQL shell wit

mysql> exit

Start the interactive script by running

sudo mysql_secure_installation

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN


After installing Nginx to serve your content and MySQL to manage your data, the next step is to install PHP to process code and generate dynamic content for your web server. While Apache includes the PHP interpreter in each request, Nginx requires an external program to handle PHP processing and act as a bridge between the PHP interpreter and the web server. This approach typically results in better overall performance for PHP-based websites but requires additional configuration steps. To configure PHP processing in Nginx, you will need to install php-fpm (PHP fastCGI process manager) and instruct Nginx to send PHP requests to this software for processing. Additionally, you will need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will be installed automatically as dependencies.

To install these 2 packages at once, run

sudo apt install php-fpm php-mysql

When prompted, type Y and press ENTER to confirm the installation

You now have your PHP components installed. Next, you will configure Nginx to use them


If you're using the Nginx web server, you have the option to create server blocks that allow you to group configuration details and manage multiple domains on a single server. These server blocks are similar to virtual hosts in Apache.

Create the root web directory for your domain as follow

sudo mkdir /var/www/projectLEMP

Next, assign ownership of the directory with the $USER environment variable, which will reference your current system use

sudo chown -R $USER:$USER /var/www/projectLEMP

Create a new configuration file in Nginx’s sites-available directory

sudo nano /etc/nginx/sites-available/projectLEMP

Paste in the following bare-bones configuration

server { 
        listen 80; 
        server_name projectLEMP www.projectLEMP; 
        root /var/www/projectLEMP;

        index index.html index.htm index.php;

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

        location ~ .php$ { 
        include snippets/fastcgi-php.conf; 
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; 
        location ~ /.ht { 
        deny all; 

These are the different directives and location blocks and their functions:

  • 'listen' sets the port where Nginx will listen (port 80 by default).

  • 'root' defines the document root where website files are stored.

  • 'index' determines the order of priority for index files, usually prioritizing index.html over index.php files for PHP applications.

  • 'server_name' specifies which domain names and/or IP addresses the server block should respond to.

  • The first 'location' block includes a 'try_files' directive, which checks for the existence of files and directories matching a URI request and returns a 404 error if no match is found.

  • The second 'location' block ('location ~ .php$') handles PHP processing by directing Nginx to the fastcgi-php.conf configuration file and the php7.4-fpm.sock file.

  • The last 'location' block ('location ~ /.ht') deals with .htaccess files that are not processed by Nginx. The 'deny all' directive ensures that any .htaccess files in the document root will not be served to visitors.

Activate your configuration by linking to the config file from Nginx’s sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/projectLEMP /etc/nginx/sites-enabled/

You can test your configuration for syntax errors by typing:
sudo nginx -t

You shall see the following message:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

We also need to disable the default Nginx host that is currently configured to listen on port 80, for this run:
sudo unlink /etc/nginx/sites-enabled/default

Reload Nginx to apply the changes:
sudo systemctl reload nginx


Your LEMP stack should now be completely set up.

You can test it to validate that Nginx can correctly hand .php files off to your PHP processor.
You can do this by creating a test PHP file in your document root. Open a new file called info.php within your document root in your text editor:
sudo nano /var/www/projectLEMP/info.php

Type or paste the following lines into the new file. This is a valid PHP code that will return information about your server:


You can now access this page in your web browser by visiting the domain name or public IP address you’ve set up in your Nginx configuration file, followed by /info.php. You will see a web page containing detailed information about your server:

In my next post, I'd talk about how to retrieve data from MySQL databases with PHP.