why exactly is nginx so much faster than the typical Apache setup of the prefork MPM and mod_php? The technical explanation is that nginx is a non-blocking event based architecture while Apache is a blocking process based architecture. To simplify it heavily the theory is like this:
Apache "Prefork Processes":
Receive PHP request, send it to a process.
Process receives the request and pass it to PHP.
Receive an image request, see process is busy.
Process finishes PHP request, returns output.
Process gets image requests and returns the image.
While the process is handling the request it is not capable of serving another request, this means the amount of requests you can do simultaneously is directly proportional to the amount of processes you have running.
Apache + PHP setup has the PHP binary embedded directly into the Apache processes. This means Apache can talk to PHP incredibly fast and without much overhead, but it also means that the Apache process is going to be 25-50MB in size. Not just for requests for PHP requests, but also for all static file requests. This is because the processes keep PHP embedded at all times due to cost of spawning new processes. This effectively means you will be limited by the amount of memory you have as you can only run a small amount of processes and a lot of image requests can quickly make you hit your maximum amount of processes.
Nginx "Event Based Processing:"
Receive request, trigger events in a process.
The process handles all the events and returns the output
Nginx
Nginx, on the other hand, requires the use of PHP-FPM if you want to run a PHP application. It
doesn’t have a module to load in PHP like Apache can. I always use Nginx, as it does everything I
need with a simpler configuration, and better overall performance.
Process Management
PHP-FPM’s master process creates child processes to handle all PHP requests. Processes are
expensive to create and manage. How we treat them is important.
PHP-FPM is an implementation of FastCGI, which uses “persistent processes”. Rather than killing
and re-creating a process on each request, FPM will re-use processes.
This is much more efficient than Apache’s mod_php, which requires Apache to create and destroy a
process on every request.
nginx –s stop Stops the daemon immediately (using the TERM signal)
nginx –s quit Stops the daemon gracefully (using the QUIT signal)
nginx –s reopen Reopens the log files
nginx –s reload Reloads the configuration
Apache "Prefork Processes":
Receive PHP request, send it to a process.
Process receives the request and pass it to PHP.
Receive an image request, see process is busy.
Process finishes PHP request, returns output.
Process gets image requests and returns the image.
While the process is handling the request it is not capable of serving another request, this means the amount of requests you can do simultaneously is directly proportional to the amount of processes you have running.
Apache + PHP setup has the PHP binary embedded directly into the Apache processes. This means Apache can talk to PHP incredibly fast and without much overhead, but it also means that the Apache process is going to be 25-50MB in size. Not just for requests for PHP requests, but also for all static file requests. This is because the processes keep PHP embedded at all times due to cost of spawning new processes. This effectively means you will be limited by the amount of memory you have as you can only run a small amount of processes and a lot of image requests can quickly make you hit your maximum amount of processes.
Nginx "Event Based Processing:"
Receive request, trigger events in a process.
The process handles all the events and returns the output
Nginx
Nginx, on the other hand, requires the use of PHP-FPM if you want to run a PHP application. It
doesn’t have a module to load in PHP like Apache can. I always use Nginx, as it does everything I
need with a simpler configuration, and better overall performance.
Process Management
PHP-FPM’s master process creates child processes to handle all PHP requests. Processes are
expensive to create and manage. How we treat them is important.
PHP-FPM is an implementation of FastCGI, which uses “persistent processes”. Rather than killing
and re-creating a process on each request, FPM will re-use processes.
This is much more efficient than Apache’s mod_php, which requires Apache to create and destroy a
process on every request.
nginx –s stop Stops the daemon immediately (using the TERM signal)
nginx –s quit Stops the daemon gracefully (using the QUIT signal)
nginx –s reopen Reopens the log files
nginx –s reload Reloads the configuration
No comments:
Post a Comment