We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Apache vs nginx

Do you think it's worth switching from Apache to nginx just because of the performance improvement? Has anyone done some performance comparisons of Phalcon applications running with nginx and Apache server?



85.5k
Accepted
answer
edited Jan '16

performance boost comes from static files mostly ( css and js ), you can use free account of cloud flare and your server wont serve those files.

There are softwares like redis and varnish ( https://www.varnish-cache.org/ ) where you basically "skip" the apache and the nginx for most of the time.

Unless your server it right next to the limit, simply use what you are most confortable with. I dont think you will gain anything significant out of it.

There are some posts in the past, where a guys says that nginx is more stable than apache, well in my expirience for the last couple of years, i have had problems only with nginx, and never with apache, also in my previous company we were running only apache and never had issues with it.

Hope that helps, Cheers!

performance boost comes from static files mostly ( css and js ), you can use free account of cloud flare and your server wont serve those files.

I was actually planning to use Amazon CloudFront CDN to serve static content.

There are softwares like redis and varnish ( https://www.varnish-cache.org/ ) where you basically "skip" the apache and the nginx for most of the time.

I know about varnish cache, but also, as I understand, it's mostly suited for websites with a lot of static content.

Unless your server it right next to the limit, simply use what you are most confortable with. I dont think you will gain anything significant out of it.

There are some posts in the past, where a guys says that nginx is more stable than apache, well in my expirience for the last couple of years, i have had problems only with nginx, and never with apache, also in my previous company we were running only apache and never had issues with it.

I haven't experienced any problems with apache and I am pretty sure it should be more stable as it's much older project.

Thank you for your answer. Highly appreciated :)

edited Jan '16

I switched mostly because it has a far easier syntax and there are possibilities to add in very efficient modules. Also nginx releases much more often and depending on your timeline you might be able to get stuff like HTTP/2 sooner.

I am pretty sure that both Apache (mod_http2) and nginx (version 1.9.5) have at least experimental support for HTTP/2 now.

For smaller projects the choice is more of a question of taste... Aside from serving static content, nginx should be able to handle more concurrent connections on paper. But that also depends on your project... (I'm in favour of nginx btw, but thats not an objective argument :D)

edited Jan '16

Well, its not so huge diffrence. But better consider using fcgi or php-fpm. Also consider using worker mpm instead of prefork: https://codebucket.co.in/apache-prefork-or-worker/

I just like nginx more.

edited Jan '16

The performance difference between Apache and Nginx is a common misconception or myth that needs to be debunked. Running the PHP module in apache will typically perform better than through FastCGI in Nginx or Apache if your Apache server is tuned correctly. And by tuned correctly, mostly what I'm talking about is turning off AllowOverride. When Apache isn't dillegently searching every directory and subdirectory for .htaccess files, its extremely speedy in executing your PHP code. It just so happens that most people use these .htaccess files and are encouraged to do so by the apps and frameworks they are running. Then one day they hear that Nginx is faster and so they try running their app on Nginx and Ho Ho Ho! they get a speed boost. That boost didn't come from Nginx, that boost came from the fact that they had to find another solution for the functionality that was previously used through .htaccess overrides because Nginx doesn't have that feature. In fact, running PHP as a module in Apache is the fastest solution for running PHP code because it runs in the same process as apache, instead of offloading the work to another process and hence, getting thrown back in the CPU wait queue and waiting for a response.

The valid reason to choose Nginx is the flexibility of its configuration files. I've found Nginx's configuration api to be a joy to work with and the resulting configuration files turn out to be much more straightforward and concise than their Apache equivelant.

I will submit that the Asynchronous nature of Nginx is why people prefer using it to serve static files and it does a great job of it, but if you wanted the purely, most optimial setup, I would use Nginx as a reverse proxy to apache and host your static files with nginx and reverse proxy your php execution to apache. Although this type of setup is too complex to warrant such a small optimization.

Running the PHP module in apache will typically perform better than through FastCGI in Nginx or Apache

? this is not exactly true... imagine you have separate app server that does only PHP.... and nginx is just a entry point (web server). You have one machine doing the simple processing work and serving static content, and yet another machine doing complex tasks (application). The whole initial idea to develop nginx come from the fact that Apache was sluggish. We cannot say that purpose was defated.

Running the PHP module in apache will typically perform better than through FastCGI in Nginx or Apache

? this is not exactly true... imagine you have separate app server that does only PHP.... and nginx is just a entry point (web server). You have one machine doing the simple processing work and serving static content, and yet another machine doing complex tasks (application). The whole initial idea to develop nginx come from the fact that Apache was sluggish. We cannot say that purpose was defated.

I can't understand what you are trying to say. Perhaps if you try to structure your ideas in a more organized way, and make some coherant points, it will be easier to discuss the matter.