// Engineering Log
LAMP, LEMP, or Nginx + Apache: which web stack to choose
Published on 2026-09-22
// Fast route
This article belongs to the topic Servers and infrastructure.
LAMP and LEMP are two classic stacks for PHP sites: Linux, a web server, a database, and PHP. They differ by the web server: LAMP uses Apache, LEMP uses Nginx. In practice the more important difference is how the web server runs PHP. That affects memory usage, behavior under load, and whether you need a second web server.
What the stack consists of
| Component | LAMP | LEMP |
|---|---|---|
| Operating system | Linux | Linux |
| Web server | Apache HTTP Server | Nginx |
| Database | MySQL or MariaDB | MySQL or MariaDB |
| PHP | mod_php module or PHP-FPM | only PHP-FPM |
Instead of MySQL you can use PostgreSQL in both stacks — most modern PHP applications support it. The stack name doesn’t change because of that.
How Apache runs PHP
Apache has two ways to run PHP.
mod_php. The PHP interpreter is embedded directly into Apache processes. This is the simplest option, but it doesn’t work well with Apache’s multithreaded models. Third-party PHP extensions are not always thread-safe, so mod_php is typically used with the prefork MPM. According to Apache documentation, prefork is “a non-threaded, pre-forking web server”, and it’s suitable for sites that need to avoid threads because of libraries that aren’t designed for multithreading. Each process handles a single connection, so with a hundred concurrent visitors you need a hundred processes, and each contains a full PHP interpreter, even if an image was requested.
PHP-FPM. PHP runs as a separate service with its own pool of processes, and Apache hands requests to it via the mod_proxy_fcgi module. In that case Apache can use the event MPM: according to the documentation, after sending a response a worker thread hands the connection to a separate listener thread, and idle keep-alive connections do not occupy worker threads. Apache serves static files itself without touching PHP.
Minimal Apache + PHP-FPM via a socket:
<FilesMatch "\.php$">
SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost/"
</FilesMatch>The socket path depends on the PHP version and the distribution. The main takeaway: the statement “Apache creates a process per request” is true only for prefork. Apache with event and PHP-FPM is architecturally similar to Nginx with PHP-FPM.
How Nginx runs PHP
Nginx cannot execute PHP itself — there is no built-in module. It always forwards PHP requests to PHP-FPM over FastCGI, and serves static files directly. Minimal configuration:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}The file snippets/fastcgi-php.conf is present in Debian and Ubuntu packages; in other distributions the necessary FastCGI parameters are set manually.
The main practical difference — .htaccess
Apache reads .htaccess files in site directories: they contain redirect rules, directory protection, caching settings. Many CMSs, primarily WordPress and Bitrix, rely on this mechanism and write their rules to .htaccess. Nginx does not read .htaccess: everything is defined in the server configuration, and after changes it needs to be reloaded.
For the administrator this is a plus — configuration is in one place and not checked on every request. For the owner of a site running a ready-made CMS this can be a problem: rules from .htaccess will need to be migrated into Nginx configuration manually, and when plugins are updated you must watch that nothing gets lost.
Hybrid: Nginx in front of Apache
In a hybrid setup Nginx accepts connections on ports 80 and 443, terminates HTTPS, serves static files itself and forwards the remaining requests to Apache, which listens on an internal port, for example 127.0.0.1:8080. The site continues to use .htaccess.
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}On the Apache side you need the mod_remoteip module so that the real visitor address is visible in logs and in the application, instead of 127.0.0.1. The application must understand the X-Forwarded-Proto header, otherwise when HTTPS is terminated it may go into an infinite redirect to HTTPS.
This scheme is used by many hosting control panels by default. Read more about reverse proxying with Nginx and how to choose between Nginx, Apache, HAProxy, Caddy and Traefik in the article “Caddy vs. Traefik vs. HAProxy vs. Nginx vs. Apache”.
When to choose what
| Situation | What to choose |
|---|---|
A ready-made CMS that writes rules to .htaccess, single site | Apache with PHP-FPM (event) or hybrid |
| New project, configuration under your control | Nginx with PHP-FPM |
| Many sites on CMS, hosting control panel | Hybrid Nginx + Apache — this is how most panels are set up |
| Application is not PHP (Node.js, Go, Python) | Nginx or Caddy as a reverse proxy, LAMP/LEMP stack not needed |
| Need automatic HTTPS without configuration | Caddy |
A hybrid is justified when .htaccess is needed. If the site doesn’t depend on it, a second web server is extra memory, an extra layer where configuration can break, and an extra package to keep updated.
Common mistakes
mod_phpwith the event MPM. Debian and Ubuntu packages switch Apache to prefork whenlibapache2-mod-phpis installed. If you later enable event manually, Apache may refuse to start or work unreliably. With event use PHP-FPM.MaxRequestWorkersnot set according to memory. In prefork each process uses tens of megabytes. Apache documentation explicitly says: the value should be large enough for expected load and small enough so that all processes fit in available RAM. Otherwise the server will start swapping.- Apache in hybrid listening on an external interface. Port 8080 should be accessible only from
127.0.0.1, otherwise the site can be accessed bypassing Nginx. - Different PHP versions in pools. Each PHP-FPM version has its own socket; check that the web server points to the correct one.
- Static files via PHP. If all requests, including images, go to PHP-FPM, the server wastes resources. Rules for serving static files should come before the PHP rule.
// Similar task
If you are dealing with something similar
This article belongs to one of the main working topics. You can keep reading on the topic, go to the homepage to understand what I do, or open the service pages directly.
Article topic
Servers and infrastructure
VPS, Linux, web stack, migrations, hosting, databases, and core operations.
Typical tasks behind this topic
- Move a site or service to a new server
- Set up Linux, Nginx, databases, and backups
- Figure out why the system behaves unstably
// Next step
If you need help with this topic, not just another article, it is better to go straight to the service page. The homepage and topic collection stay available as secondary routes.
Open services// Reviews
Related reviews
I came with an expensive request to configure a VPS server, but during the consultation Mikhail suggested a much simpler, more affordable solution. In the end I saved time and money. Mikhail — a true expert who works for the client's result, not for the fee. I recommend him!
I came with an expensive request to configure a VPS server, but during the consultation Mikhail suggested a much simpler and more cost-effective solution. In the end I saved budget and time. Mikhail — a true expert who …
VPS setup, server setup
2026-05-12 · ★ 5/5
Excellent work! Set up the server very quickly, installed the control panel, and configured the IP. Definitely recommend!
Excellent work! Very quickly set up the server, installed the panel, configured the IP I can definitely recommend it!
Everything was excellent; helped promptly and professionally. Thank you — I recommend them to the community.
Everything's great, helped promptly and professionally, thank you, I recommend it to the community
VPS setup, server setup
2026-04-16 · ★ 5/5
There were several issues concerning both the technical side and overall understanding. Mikhail responded quickly, resolved the technical problems, and helped me understand them — many thanks. I'm satisfied with the result.
There were several issues concerning both the technical side and overall understanding. Mikhail responded quickly to the request, helped sort things out and resolved the technical problems and helped clarify …
VPS setup, server setup
2026-02-18 · ★ 5/5
Everything was done quickly and efficiently. I recommend.
Everything was done quickly and efficiently. I recommend.
VPS setup, server setup
2026-01-17 · ★ 5/5
Everything went well; the contractor responded quickly to questions and helped resolve the issue. Thanks!
Everything went well, the contractor responded quickly to questions and helped resolve the issue. Thank you!
VPS setup, server setup
2025-12-16 · ★ 5/5
// Contact
Need help?
Get in touch with me and I'll help solve the problem
I reply within one business day (03:00-13:00 GMT)
Или оставьте заявку здесь:
// Related