Introduction to LNMP Architecture and High‑Performance Web Services with Nginx, PHP‑FPM, and LORMP
This article explains the evolution from LAMP to LNMP, details how Nginx interacts with PHP‑FPM via CGI/FastCGI, compares Nginx and Apache processing models, and introduces LORMP (OpenResty + Lua) with configuration and code examples for building high‑performance backend web services.
LNMP (Linux, Nginx, MySQL, PHP) is a widely used web‑application stack that evolved from the earlier LAMP architecture when Nginx’s low‑resource, high‑concurrency design began to replace Apache as the dominant web server.
The article examines the interaction between web servers and backend scripts, tracing the progression from CGI to FastCGI, then to PHP‑CGI and finally to PHP‑FPM, highlighting how PHP‑FPM resolves the performance and restart‑ability issues of its predecessors.
CGI works by launching a new process for each request, which leads to poor performance under high load. FastCGI introduces a persistent process manager that reuses CGI instances, reducing fork overhead and enabling better monitoring and scaling; Apache’s mod_fastcgi is a typical implementation.
FastCGI request handling involves the web server passing environment variables and input to a FastCGI child (e.g., PHP‑CGI), which processes the request and returns output over the same connection before waiting for the next request.
PHP‑CGI lacks smooth restart capabilities and can terminate the entire PHP runtime when a child process is killed, which is why PHP‑FPM—integrated into PHP since version 5.3.3—has become the standard FastCGI manager, offering graceful restarts, memory control, and higher throughput.
In the LNMP stack, Nginx’s event‑driven, asynchronous architecture (master/worker processes, modular design, and efficient event modules like ngx_epoll_module ) enables thousands of concurrent connections with minimal CPU and memory usage, making it a natural fit for PHP‑FPM. By contrast, Apache’s prefork and worker MPM models rely on multiple processes or threads, which consume more resources and are less suited for extreme concurrency.
A sample Nginx configuration demonstrates how to enable fastcgi caching, route PHP requests, and set variables; the code is shown below:
server {
listen *:80;
server_name domain.com;
charset utf-8;
index index.html index.htm index.php;
root /var/vhosts/domain.com/public;
location / {
try_files $uri $uri/ index.php$is_args$args;
}
location ~ .php$ {
set $nocache "";
if ($http_cookie ~ (cookie_name)) { set $nocache "Y"; }
set $cachekey $request_method$scheme://$host$request_uri;
fastcgi_cache_bypass $nocache;
fastcgi_cache dynamic;
fastcgi_cache_key $scheme$request_method$host$request_uri;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_catch_stderr "PHP Fatal error";
add_header X-Cache-Status $upstream_cache_status;
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_scriptName;
}
}The article then introduces LORMP, a variant of LNMP that replaces Nginx with OpenResty (Nginx + LuaJIT). OpenResty embeds a Lua VM via ngx_http_lua_module , allowing Lua scripts to run directly in the Nginx event loop, providing fine‑grained request handling, access control, and caching without leaving the server process.
Example OpenResty configuration and Lua code are provided:
location / {
content_by_lua_file $document_root/pub/index.lua;
}
location ~ .php$ {
root $rs_root;
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
local function call_php(request)
local index = '/index.php'
local rs = ngx.location.capture(index,{args = ngx.var.args, always_forward_body = true})
if rs.status == 302 then
redirect(rs.header.Location)
end
return rs
endBy using Lua for routing and early request processing, many tasks can be handled inside Nginx, reducing the load on PHP‑FPM and improving overall throughput. The article concludes with a forward‑looking view that combining Nginx, PHP‑FPM, and OpenResty offers a flexible, high‑performance platform for modern web services.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.