How to Set Up LEMP Stack (Linux, Nginx, MySQL, PHP)

If you want to build a website or run WordPress, you need a web server. One good choice is the LEMP stack. It’s a group of free tools that work together to serve websites.

LEMP stands for:

  • Linux (the operating system)
  • Nginx (the web server)
  • MySQL (the database)
  • PHP (the programming language)

You may have heard of the LAMP stack. That uses Apache instead of Nginx. I used Apache before, but I switched to Nginx because it uses less memory and handles traffic better.

The funny part? LEMP is spelled with an “E,” but it uses Nginx, which is pronounced “engine-x.” So it’s kind of a silent ‘E’. Just like lasagna.

Let me show you how I set up LEMP on my server. It’s not hard. I’ll walk you through it step by step.


What You’ll Need

Before we start, here’s what you should have:

  • A server with Linux (like Ubuntu)
  • Root access (so you can run commands)
  • A little patience (and maybe coffee)

I’m using Ubuntu 22.04, but these steps are almost the same for other versions too.


Step 1: Update Your Server

First, log in to your server. Then update your system to make sure everything is fresh.

sudo apt update
sudo apt upgrade -y

I always do this first. It saves me from weird bugs later. Think of it like brushing your teeth before breakfast—not fun, but useful.


Step 2: Install Nginx (the Web Server)

Now let’s install Nginx. This is the software that serves your website to visitors.

sudo apt install nginx -y

After it installs, start it and make sure it runs when the server boots:

sudo systemctl start nginx
sudo systemctl enable nginx

You can check if it works by typing your server’s IP address into your web browser. You should see a welcome page.

Nginx is fast and doesn’t use much memory. I like it better than Apache for that reason.


Step 3: Install MySQL (the Database)

Next, we need a database. MySQL stores your site’s content—like blog posts, usernames, and more.

Install MySQL like this:

sudo apt install mysql-server -y

Start and enable the service:

sudo systemctl start mysql
sudo systemctl enable mysql

Now run a quick setup script to make it more secure:

sudo mysql_secure_installation

You’ll be asked questions. Here’s how I answered:

  • Set root password? Yes
  • Remove anonymous users? Yes
  • Disallow root login remotely? Yes
  • Remove test database? Yes
  • Reload privileges? Yes

This tightens security a bit, like putting a lock on your front door.


Step 4: Install PHP (the Language WordPress Uses)

PHP is the language your website speaks. WordPress and many other tools need it.

Install PHP and a few extras like this:

sudo apt install php-fpm php-mysql -y

Here’s what these packages do:

  • php-fpm lets Nginx use PHP (it’s like a translator)
  • php-mysql lets PHP talk to MySQL

PHP-FPM runs in the background and processes PHP files fast.


Step 5: Configure Nginx to Use PHP

Now we need to tell Nginx to use PHP when it sees .php files.

We’ll make a simple server block (like a website config).

First, create a folder to hold your website:

sudo mkdir -p /var/www/lemp-site

Give yourself permission:

sudo chown -R $USER:$USER /var/www/lemp-site

Create a basic HTML file to test:

echo "<?php phpinfo(); ?>" > /var/www/lemp-site/index.php

Now create a new Nginx config:

sudo nano /etc/nginx/sites-available/lemp-site

Paste this inside:

server {
    listen 80;
    server_name your_domain_or_IP;

    root /var/www/lemp-site;
    index index.php index.html;

    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;
    }
}

Make sure the PHP version matches your system. You can check it with:

php -v

Enable the config:

sudo ln -s /etc/nginx/sites-available/lemp-site /etc/nginx/sites-enabled/

Then test and reload:

sudo nginx -t
sudo systemctl reload nginx

Visit your server’s IP again. If you see the PHP info page, it’s working.


Step 6: Create a Database (Optional but Useful)

If you plan to install WordPress or another app, you’ll need a database.

Log into MySQL:

sudo mysql

Then run:

CREATE DATABASE mysite;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mysite.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace mysite, myuser, and mypassword with your own names. Don’t reuse passwords like “1234”—unless you want a hacker party.


What You Can Do With LEMP

Once LEMP is ready, you can:

  • Install WordPress
  • Build your own PHP apps
  • Host static and dynamic websites

It’s a strong and simple stack. I use it for almost everything—from blogs to client projects.


Three Handy Lists

Things You Need for LEMP:

  • A Linux server (like Ubuntu)
  • Internet access (so you can download packages)
  • Root or sudo privileges

Tools Installed in This Guide:

  • Nginx – serves web pages
  • MySQL – stores your data
  • PHP-FPM – runs PHP code
  • PHP-MySQL – connects PHP to MySQL

Basic Nginx Commands:

# Start Nginx
sudo systemctl start nginx

# Restart Nginx
sudo systemctl restart nginx

# Test Nginx config
sudo nginx -t

# Reload Nginx after config changes
sudo systemctl reload nginx

Nginx doesn’t complain much, but always test your config before reloading it.


A Quick Comparison: LAMP vs LEMP

Feature LAMP (Apache) LEMP (Nginx)
Speed Slower under load Faster under load
Memory usage Higher Lower
Setup Slightly easier Slightly trickier
Popularity Very common Growing fast

I switched to LEMP for better performance. If you’re curious, try both and see which one you like more.


Final Thoughts (and a Cheesy Pun)

Setting up LEMP isn’t magic. It just takes a bit of time and care. Once you get it running, you’ve got a stable, fast, and flexible server.

It’s like building your own pizza oven instead of buying frozen pizza. More work? Sure. But way more control—and tastier results.

And remember: LEMP rhymes with “temp,” so keep your cool if something breaks. Google, forums, and a deep breath go a long way.

Let me know if you want help setting up WordPress next, or need help with SSL. Happy server-ing.

 

Leave a Reply