Backend Development 9 min read

LNMP Server Architecture Troubleshooting and Optimization: Nginx and PHP‑FPM Tuning

This article walks through a real‑world LNMP case where slow backend responses and 502 errors were diagnosed and resolved by analyzing Nginx logs, increasing file descriptor limits, adjusting FastCGI timeouts, and fine‑tuning PHP‑FPM settings, followed by performance testing with ApacheBench.

php中文网 Courses
php中文网 Courses
php中文网 Courses
LNMP Server Architecture Troubleshooting and Optimization: Nginx and PHP‑FPM Tuning

The article begins with an overview of common enterprise web server stacks (LNMP/LAMP) and notes the industry trend of replacing Apache with Nginx for its superior performance and built‑in high‑availability features.

A specific incident is described: backend users experienced slow responses and occasional 502 errors. Initial checks of network connectivity proved insufficient, leading to a deeper investigation of the LNMP stack.

Step 1 – Nginx diagnostics: The error log was examined with $ cat /usr/local/nginx/logs/error.log | grep error , which showed no issues. Access logs were then inspected using $ cat /var/log/access_nging.log | grep error , revealing multiple 502 entries.

Step 2 – Nginx file‑descriptor limit: The default worker_rlimit_nofile of 4096 was increased to 51200 in vim /usr/local/nginx/conf/nginx.conf and the worker connections were set to the same value. Nginx was reloaded with service nginx reload .

Step 3 – System limits: The OS limit was checked via ulimit –n (still 4096). The file vim /etc/security/limits.conf was edited to set: soft nofile 65535 hard nofile 65535 soft nproc 65535 hard nproc 65535 These values exceed the Nginx limits, ensuring the kernel permits the higher descriptor count.

Step 4 – FastCGI timeout: FastCGI parameters (connect, send, read) were increased to 300 seconds in nginx.conf , then Nginx was reloaded again.

After these changes, the site became reachable, though still slightly slow, prompting a focus on PHP‑FPM.

Step 5 – PHP‑FPM log inspection: The PHP log was checked with tail -n 100 /usr/local/php/var/log/php-fpm.log , revealing a warning: WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers) .

Step 6 – PHP‑FPM parameter tuning: The article explains the meaning of dynamic vs. static modes and the relevant parameters (pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers). For an 8 GB server, a static mode with a max of 125‑150 children was chosen, and PHP‑FPM was reloaded via service php-fpm relod . The number of PHP processes was verified with netstat -anpo | grep php-fpm | wc -l , returning 128 .

Step 7 – Result verification: After reloading, page load times improved markedly, the warning disappeared from the logs, and backend access stabilized.

Step 8 – Load testing: Performance was measured using ApacheBench: ab -n 1000000 -c 10000 (one PHP file). The article explains the meaning of the -n (total requests) and -c (concurrent users) options and encourages iterative testing to locate bottlenecks.

Overall, the case demonstrates a systematic approach to diagnosing and optimizing an LNMP server stack, covering both Nginx and PHP‑FPM configuration, OS limits, and load‑testing techniques.

performance tuningLinuxNginxserver optimizationPHP-FPMLNMP
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.