Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Nginx + php-fpm is suppose be much faster than Apache + mod-php

I'm having a PHP based web application running on Apache server which has considerable amount of php processing in back end. Since the overall performance is low I worked on improving the performance of the application. First I followed techniques such as client side caching, gzip enabling, js-css minifying which improved the performance upto a good extend.

In order to further improve the performance I wanted to try out server level improvement. So I tried to compare the application performance by hosting it on Apache and Nginx servers.

  • Nginx version - 1.0.15;
  • Apache version - 2.2.15;
  • php version - 5.4.38;

In Apache I user Apache + mod-php and in Nginx I used Nginx + php-fpm for this comparison. As most of the tutorials explained I configured the number of Nginx workers equal to the number of cores in my processor. I used jmeter to do the stress testing and following are the graphs I could generate out of it.

In all these graphs x-axis is each request I sent and y-axis is milliseconds for getting response for each request.

Access the login page

enter image description here

Submit the login page

enter image description here

Access home page

enter image description here

I could only perform the testing upto 100 concurrent users logged in within 1 second because it started dropping requests after then in both server setups.

There was a little improvement on performance in Nginx than Apache but it was not a major difference where it's worth to change all my server architecture from Apache to Nginx. And when I observe the server resource utilization also I didn't find much of difference between Nginx and Apache

When I went through other comparisons people have done, I found that they claim Nginx is much more faster in concurrent accesses such as following graph shows.

http://www.eschrade.com/wp-content/uploads/2014/01/event-mpm-nginx.gif

But I was unable to observe any major difference of performance in Nginx over Apache even with 100 concurrent access within 1 second.

Following are my questions.

  1. Is Nginx + php-fpm is suppose to do server operations much faster than Apache + mod-php due to efficient usage of memory and other resources ?
  2. Is Nginx only recommended to server static contend and not for heavy server operation sites ?
  3. Is there any better way of configuring Nginx to gain more performance improvement ?
like image 636
Thilanka Avatar asked Apr 02 '15 02:04

Thilanka


People also ask

How much faster is PHP-FPM?

Results. You can notice PHP-FPM made our test website almost 350% faster when it comes to loading times. Plus, it made the site twice as resource efficient as it was with mod_php. PHP-FPM, one of the newest way to use PHP in conjunction with a web server, is an alternative PHP FastCGI implementation.

Why is PHP-FPM faster?

PHP-FPM is faster than traditional CGI-based methods, such as SUPHP, in multi-user PHP environments. It does not overload a system's memory with PHP from Apache processes like ruid2+php-dso. This method is useful when a user receives extra traffic that requires resources to process it.

Does nginx require PHP-FPM?

PHP-FPM is installed and active for NGINX. And that's it, you've got NGINX up and running with PHP-FPM support. Remember, when you build your virtualhost configuration files, you'll need to make sure to include PHP support in those. For that, you can use the /etc/nginx/sites-available/default file as an example.

Is PHP-FPM better?

Conclusion. PHP-FPM is an efficient method on how to minimize the memory consumption and rise the performance for the websites with heavy traffic. It is significantly faster than traditional CGI-based methods in multi-user PHP environments.


1 Answers

I did a bit more research on this and found that Nginx will perform well with static resources and not with other dynamic content delivery such as php processing which needs to done with the help of external application such as php-fpm. So if your web application has a performance bottleneck on php processing then replacing Apache with Nginx will not be a solution.

Following article explains a details research done on comparing static contend delivery and php script result delivery using Apache + mod_php and Nginx + php-fpm.

http://blog.a2o.si/2009/06/24/apache-mod_php-compared-to-nginx-php-fpm/

Following are the conclusion points described in the above article.

Conclusion or “should you switch from Apache to Nginx?”

  • Short answer: I do not know.
  • Longer answers are here:

    1. If you host many websites and users utilize .htaccess files and change them frequently, then the answer is probably “no”. The cost of switching over to Nginx and converting all the configuration to new format usually reaches the cost of buying another server.
    2. If you have single application on multiple servers and most of the processing power is not consumed by serving static file content, the answer is also probably “no”.
    3. If you are mainly serving static content, the answer is obviously “yes”.
    4. If you are creating a fresh system for webhosting solution, the answer is probably “yes”, with the assumption that users will not miss the .htaccess functionality or it will be provided by other means
    5. If you are consolidating services with some virtualization technology, then answer is probably “yes”, as Nginx tends to have smaller memory footprint than Apache.
    6. If you are looking towards Nginx as your PHP server optimization, look again, but not at Nginx, at your application code.
like image 68
Thilanka Avatar answered Sep 18 '22 19:09

Thilanka