Demystifying Nginx and PHP-FPM For PHP Developers
Demystifying Nginx and PHP-FPM For PHP Developers
medium.com
14-17 minutes
Introduction
1 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Although the PHP core includes a built-in server that you can use
for development purposes while working on your local
environment, it is not suitable for production environments due to
its single-threaded nature that limits it to execute one single
request at a time, among others lacking capabilities related to
security, caching, scaling, and so on. As a result, PHP relies on an
additional component, PHP-FPM, for managing multiple processes
and ultimately on Nginx to efficiently serve web applications.
2 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Tuning PHP-FPM
The life cycle of a request starts when it arrives at the web server
(Nginx). The web server forwards the request to PHP-FPM which
assigns an available worker to handle it. The worker processes the
request, executing the PHP script, and generates a response,
3 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
• Idle: In this state, the worker is not processing any request and is
waiting for new requests to arrive. The worker is available to take
on new tasks as they come in.
4 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
On-demand Strategy
Definition
5 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Configuration
Considerations
Static Strategy
Definition
6 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Configuration
Considerations
Dynamic Strategy
Definition
7 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Configuration
Considerations
8 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Tuning Nginx
Worker Processes
9 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Worker Connections
10 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Monitoring PHP-FPM
PHP-FPM provides a built-in status page that can help you monitor
the performance and health of your PHP-FPM pools. By enabling
the status page in your PHP-FPM configuration, you can access
real-time information about your PHP-FPM pools, such as the
number of active processes, idle processes, and requests in the
queue.
Monitoring Nginx
Nginx also includes a built-in status module that can help you
monitor the performance and health of your Nginx server. By
enabling the status module in your Nginx configuration, you can
access real-time information about your Nginx server, such as the
number of active connections, accepted connections, and handled
requests.
Troubleshooting
When issues arise, it’s essential to know where to look for relevant
information. Both PHP-FPM and Nginx generate log files that can
help you identify and resolve problems.
11 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Nginx log files, including access and error logs, can be found in
the /var/log/nginx directory or specified in your Nginx configuration
file. Access logs record all incoming requests, while error logs
contain information about issues related to the Nginx server.
Analyzing these logs can help you identify issues with your server
configuration, resource usage, or performance bottlenecks.
Conclusion
Interactive Playground
12 of 13 6/6/23, 13:06
Demystifying Nginx and PHP-FPM for PHP Developers |... about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2...
Feel free to try out different configuration values and observe their
impact on your server’s performance and resource usage. This
hands-on approach will further strengthen your understanding of
Nginx and PHP-FPM and enable you to better optimize your PHP
applications.
https://github.com/mgonzalezbaile/ngingx-phpfm-playground
13 of 13 6/6/23, 13:06