What Is the LEMP Stack?
By the end of this lesson you will be able to name the four parts of a LEMP server, follow one web request from the browser to the database and back, explain how LEMP differs from LAMP, and read the server layout that every later lesson in this course uses.
What you should know first: basic Linux commands from Linux Introduction and The Linux Filesystem, and beginner PHP from PHP Introduction. If you have read What Is the LAMP Stack?, you already know three of the four letters. This course spends its time on the parts that change: Nginx and PHP-FPM.
The four parts
LEMP is a set of four pieces of free software that together run PHP websites. The name comes from their first letters. The "E" is there because Nginx is pronounced "engine-x".
| Letter | Software | Its job | Version taught |
|---|---|---|---|
| L | Linux (Ubuntu 26.04 LTS) | The operating system: files, users, processes, network | Ubuntu 26.04 LTS |
| E | Nginx | The web server: accepts HTTP connections, sends files, passes PHP requests on | 1.28.3 (Ubuntu package) |
| M | MySQL or MariaDB | The database: stores orders, students, marks | MySQL 8.4 (Ubuntu package 8.4.11) or MariaDB 11.8 |
| P | PHP with PHP-FPM | Runs your PHP code and builds the page | PHP 8.5 (Ubuntu package 8.5.4) |
These are the versions in the Ubuntu 26.04 package archive, checked on 16 November 2026. Using the distribution's own packages means security fixes arrive through normal apt updates.
PHP-FPM means "FastCGI Process Manager". It is a separate program that keeps a group of PHP worker processes ready. Nginx cannot run PHP by itself. It hands each PHP request to PHP-FPM over a protocol called FastCGI, a simple way for a web server to talk to a program that builds pages.
How one request travels
Think of a busy bakery in Fort Kochi. The counter staff hand over ready items from the shelf at once: bread, biscuits, banana chips. A custom birthday cake is different. The counter writes the order on a slip, passes it through a window to the kitchen, and the kitchen bakes it, checking the store room for ingredients.
On a LEMP server, Nginx is the counter, static files are the shelf, PHP-FPM is the kitchen, the FastCGI socket is the order window, and MySQL is the store room.
Browser
| 1. GET http://kochibakery.example.com/
v
Nginx (port 80, user www-data)
| 2a. /images/logo.svg -> read file from disk, send it back
| 2b. /index.php -> pass to PHP-FPM through a Unix socket
v
PHP-FPM worker (one process per request in progress)
| 3. run index.php, query the database with PDO
v
MySQL (127.0.0.1:3306, never open to the internet)
| 4. rows come back
v
PHP builds the HTML -> PHP-FPM -> Nginx -> BrowserHere is the same idea from the outside. The first request is a static file, so Nginx answers it alone. The second is the home page, which is index.php, so PHP built it:
curl -s -o /dev/null -w "%{http_code} %{content_type}\n" http://kochibakery.example.com/images/logo.svg
curl -s -o /dev/null -w "%{http_code} %{content_type}\n" http://kochibakery.example.com/200 image/svg+xml 200 text/html; charset=UTF-8
Nginx chose the type image/svg+xml from its mime.types file. The charset=UTF-8 on the second line was added by PHP, which tells you the request went through PHP. You will build this exact site in the next three lessons.
Why Nginx handles many visitors well
Nginx is event-driven. Each Nginx worker process handles thousands of open connections at the same time. It never waits for one slow visitor; it works on whichever connection has something ready.
A restaurant analogy helps. One kind of restaurant gives each table its own waiter, who stands there while the family reads the menu. Another kind has a few quick waiters who move between all the tables and serve whoever is ready. The second kind needs far fewer staff for a full house. Nginx is that second kind.
Ubuntu's /etc/nginx/nginx.conf starts one worker per CPU core (worker_processes auto;) and lets each worker hold 768 connections (worker_connections 768;). Slow mobile visitors on a 4G connection in a train therefore cost very little.
PHP is different. A PHP worker is busy for the whole time your script runs. That is why PHP lives in its own pool of workers, sized for your server's memory. You will tune that pool in PHP-FPM Pools & Tuning.
LEMP compared with LAMP
The LAMP stack uses Apache instead of Nginx. In the classic LAMP setup, PHP runs inside every Apache process through a module called mod_php.
| Topic | LAMP (Apache) | LEMP (Nginx) |
|---|---|---|
| How PHP runs | Usually inside Apache (mod_php) | Always a separate PHP-FPM service |
| Per-folder config | .htaccess files, read on every request | None; all config lives in /etc/nginx and is loaded once |
| Static files | Good | Very fast, low memory per connection |
| Site config name | Virtual host | Server block |
| Reload after a change | systemctl reload apache2 | nginx -t, then systemctl reload nginx |
The .htaccess row matters most when you move an app. Rules such as pretty URLs or "deny this folder" must be rewritten as Nginx location blocks, and you will do that in Redirects, Rewrites & Pretty URLs. Apache can also use PHP-FPM; Ubuntu's PHP-FPM package even ships an Apache config file for it. The real difference is the web server's design, not PHP.
Neither stack is "better" for every case. Shared hosts often run Apache because .htaccess lets customers change settings without root access. On your own VPS, where you control the config, Nginx is a common choice.
The server layout used in this course
Every lesson in this course works on one Ubuntu 26.04 LTS server that hosts two sites: the Kochi Bakery website (plain PHP) and ShikshaPortal, a student and marks app for a Lucknow school (built in the LAMP course). Keep this table handy; later lessons assume these names.
| Item | Value in this course |
|---|---|
| Server IP (example) | 203.0.113.10 (a documentation address; use your own) |
| Your login user | deploy (SSH key login, member of sudo, owns the site code) |
| Kochi Bakery | kochibakery.example.com, code in /var/www/kochibakery, web root /var/www/kochibakery/public |
| ShikshaPortal | shiksha.example.com, code in /var/www/shiksha, web root /var/www/shiksha/public |
| Nginx server blocks | /etc/nginx/sites-available/kochibakery.conf, shiksha.conf, catchall.conf, each linked into /etc/nginx/sites-enabled/ |
| Nginx logs | /var/log/nginx/kochibakery.access.log and kochibakery.error.log (same pattern for shiksha) |
| PHP-FPM | PHP 8.5; default pool socket /run/php/php8.5-fpm.sock |
| Per-site PHP pools | /etc/php/8.5/fpm/pool.d/kochibakery.conf, socket /run/php/kochibakery.sock, Linux user kochibakery (same pattern for shiksha) |
| Databases | kochibakery and shiksha, each with its own MySQL user |
Only the public folder is visible on the web. Settings files with database passwords stay one level up, in /var/www/kochibakery/config.php for example, so a mistake in Nginx can never send them to a browser.
A small PHP check you can keep in mind: php_sapi_name() returns cli when you run PHP in a terminal, and fpm-fcgi when PHP-FPM runs it for a web request. That is a quick way to know which PHP you are talking to.
<?php
declare(strict_types=1);
// Save as /var/www/kochibakery/public/sapi.php, open it once, then delete it.
echo 'This page was run by: ', php_sapi_name(), "\n";This page was run by: fpm-fcgi
Delete test files like this one after use. Small leftover files that reveal details about your server help attackers plan.
Security from the first day
A LEMP server faces the internet, so a few rules apply in every lesson of this course:
- Only ports 22 (SSH), 80 and 443 are open in the firewall. MySQL listens on
127.0.0.1only. - Log in with SSH keys as
deploy, never with a root password. See SSH & Key-based Login. - Each site's PHP runs as its own Linux user, so one hacked site cannot read the other's files.
- Database passwords live outside the web root, and each app has its own least-privilege MySQL user.
- Always run
sudo nginx -tbefore reloading, so a typo never takes both sites down.
Common mistakes
- Thinking Nginx runs PHP. It does not. If PHP-FPM is stopped, Nginx shows "502 Bad Gateway" for every PHP page while static files still work.
- Copying Apache
.htaccessrules to an Nginx server. Nginx ignores.htaccessfiles completely, so any "deny" rules in them do nothing. - Putting the whole project in the web root. Only
public/should be served. Everything else stays outside it. - Mixing stacks on one server by accident. Installing Apache and Nginx together makes both fight for port 80.
Interview questions
1. What does LEMP stand for, and why is there an "E"?
Linux, Nginx, MySQL (or MariaDB) and PHP. The "E" comes from the pronunciation of Nginx, "engine-x". In practice the "P" means PHP running under PHP-FPM, because Nginx cannot execute PHP itself.
2. How does Nginx run a PHP file?
It does not run it. Nginx matches the request to a location block for .php files and forwards it over FastCGI, usually through a Unix socket, to PHP-FPM. A PHP-FPM worker executes the script and returns the output, which Nginx sends to the browser.
3. What is the main architectural difference between Apache with mod_php and Nginx with PHP-FPM?
With mod_php, the PHP interpreter is loaded inside every Apache worker, so even requests for images use a process that carries PHP's memory. With Nginx, light event-driven workers serve static files and connections, and a separately sized pool of PHP-FPM workers handles only PHP requests.
4. Why does Nginx not support .htaccess?
Nginx reads its configuration once at start or reload, which keeps request handling fast. Checking for per-directory files on every request would cost disk reads. All rules go in server blocks instead, and only an administrator can change them.
Key points
- LEMP is Linux, Nginx ("engine-x"), MySQL or MariaDB, and PHP running under PHP-FPM.
- Nginx serves static files itself and passes PHP requests to PHP-FPM over FastCGI.
- Nginx is event-driven, so each worker handles many connections; PHP workers are sized separately.
- LEMP has no
.htaccess: rules move into server blocks andlocationblocks. - This course hosts Kochi Bakery and ShikshaPortal on one Ubuntu 26.04 server, under
/var/www/<site>/public, with one server block and one PHP pool per site. - MySQL stays on
127.0.0.1, secrets stay outside the web root, andnginx -tcomes before every reload.
Exercise
Meera runs a tuition centre website in Pune on a LEMP server. Visitors report that the photo gallery loads fine, but every page that ends in .php shows "502 Bad Gateway". Write down which part of the stack is most likely failing and why the photos still work. Then list two commands from this lesson's ideas she could run to check, and say what each would tell her.
Show solution
PHP-FPM is most likely stopped or Nginx is pointing at the wrong socket. Photos are static files, which Nginx reads from disk by itself, so they keep working. Only PHP requests need the "kitchen".
systemctl is-active php8.5-fpm
sudo tail -n 5 /var/log/nginx/error.logThe first command shows active or inactive for the PHP-FPM service. The error log shows the exact problem, for example a connect() ... failed line that names the socket Nginx tried to reach.
Quick Quiz
1. What does the "E" in LEMP stand for?
The letter comes from how Nginx is pronounced.
2. Which component actually executes index.php on a LEMP server?
Nginx forwards PHP requests over FastCGI; PHP-FPM workers run the code.
3. A rule in an .htaccess file on a LEMP server denies access to a folder. What happens?
Nginx has no per-directory config files; the rule must be written as a location block.
4. In this course, which folder is the web root of the Kochi Bakery site?
Only public is served, so config files with secrets can stay one level up.
Learn LEMP Stack with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
