Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kfirba's avatar
Level 50

Learning Nginx

Hello.

I've recently decided that I want to migrate to Nginx from Apache.

If I'm not mistaken, I've understood that there are some major changes in Nginx 1.6.1+ compared to earlier versions. I wanted to know if there are any recommended books on Nginx 1.6+ so an almost total beginner can learn from and be able at the end to install, monitor, maintain and modify nginx on his server.

Any recommendation is welcome :)

P.S. When hosting my website, which OS should I host it on? Ubuntu? Debian? (I understood that Ubuntu is a fork of Debian or something like that).

0 likes
11 replies
bashy's avatar

It's pretty easy to learn it but Nginx works in a different way. Unlike Apache where it processes the request, Nginx has to pass it on to PHP (PHP-FPM). Normally Nginx is used with an Apache backend when dealing with PHP requests. Nginx was good for serving static files but in time it has served as a standalone web server.

I need to do more posts on Nginx but I've put some things up which you can learn from on my blog: https://bashy.im/blog

I personally use Debian because I prefer their release cycle and packages but there's not much in them. People just use Ubuntu because everyone else does. Most don't even know it's based off Debian. You can see which version via cat /etc/debian_version.

1 like
kfirba's avatar
Level 50

@bashy Thanks for the reply. As far as I know, Apache can run PHP without passing it to a service (mod_php). That's what made it so accessible.
However, it is recommended to pass PHP request from Apache to PHP-FPM, a FactCGI implementation, for performance boost when dealing with many requests.

Nginx doesn't have any built-in mod to run PHP so we have to pass the request to FastCGI (PHP-FPM). I've never heard that there is a need to run Nginx along with Apache.

I've seen your blog. There are some really nice articles, however, I'm looking for a much more in-depth book. Something where I can start as a total beginner and by the end of the book be decent at a point where I can install, modify, maintain and upgrade my web server by myself. Do you happen to know one? Or where did you learn Nginx?

MrPhilbert's avatar

I tried out Nginx for the first time a couple years ago on a vps from Digital Ocean. I set up two servers, one with the normal lamp stack, the other with Nginx and php-fpm. I Set up identical Drupal sites on each (yeah, I know) with caching enabled. Man, what a difference! The Nginx version loaded in an average of around 300ms and the Apache version loaded in around 1.5 seconds. It is also extremely efficient. When I run "top" on Ubuntu, it shows Nginx at only 4 megs of ram. It also works great as a proxy to node. Node runs on a specific port and Nginx serves only to proxy a domain name to that port. Nginx is now my go to server. One thing Nginx does not do is interpret .htaccess files. The .htaccess instructions have to be incorporated into your Nginx server configurations (instructions for Laravel are on the Laravel site). There are also lot's of ready made configurations on Github (lookup "perugio" for example).

I read the book "Mastering Nginx" and it really opened my eyes about what can be done with Nginx. I am also now experimenting with Nginx and uWSGI for serving multiple languages (php included) and it looks very promising.

kfirba's avatar
Level 50

@MrPhilbert Thanks for the reply. Is the book up-to-date? The demonstration there will work even with nginx 1.6+? Also, I'm not an experienced webmaster, will I still be able to read and understand the book?

@bashy I see :o.

But what's the difference if I pass the PHP request to Apache or a FactCGI?

bashy's avatar

I actually use 1.7.10 mainline, to me it's pointless using 1.6 unless you're really worried. Nginx website even says to use 1.7 mainline http://nginx.com/blog/nginx-1-6-1-7-released/

Well, it's not always best to use Nginx to serve PHP but it really depends on what you're doing.

MrPhilbert's avatar

Nginx doesn't serve php. Apache doesn't either. At the very basic level, they are similar to the router in Laravel, Angular.js and many other frameworks. Similar to the way you use the before function in Laravel Routes to call an auth utility etc., these servers can call a service executable such as php, java or python to handle file requests based on extension. Nginx and Apache do it quite differently though. Nginx uses something called an event model with only one process. Apache (with a few exceptions) creates a new thread for each task. I explained this at a conference a few months ago as this: Imagine you are tasked with managing a huge Whackamole event. There are 100 moles popping up everywhere. As the manager, you have two choices of how to handle this.

  1. Hire 20 people (say at 10.00 per hour each). There sole job is to watch only 5 holes each. If a mole pops up whack it. If no moles pop up, you still have to pay each one (you know about labor law right?).
  2. Hire the world renowned "Evil Eye Mole Whacker". He charges 30.00 and hour but his claim to fame is "No mole has ever gotten past me". You can bet that he will be very busy but, sure to his reputation, no mole will ever see the light of day on his watch.

Evented models like Nginx and Javascript have a very good place where there sole job is to just listen for things and call a process or function to do its thing. You might ask "Why isn't everything event based then?". Well, if you have a complicated application with many moving parts with many inter-dependencies, threading can help keep a little order in the game.

I do find it a little amusing when I see performance "comparisons" of things like node.js, php, asp.net, java etc. Everyone sets up one operation such as a Fibonacci or factorial calculation. They like to quote reqests per second on the evented models as being so much higher. There are not a lot of real world uses for Fibonacci though. Throw in a little complication like authentication and maybe a many to many relationship however and the picture changes very quickly.

MrPhilbert's avatar

Really bashy? I get that you know Nginx. In fact I would like to inquire about your experience with 1.7. I have been using 1.4 and wonder if there is any benefit to an upgrade. As for "We know", I thought kfirba was the original questioner. If you and alenabdula would like to be known as the only ones that are allowed to give advice, please let Jeffrey know. I'm sure he would appreciate it.

I do however appreciate any collaborative expertise you would like to share as we are all learning not only from Jeffrey but from each other.

bashy's avatar

That was for "Nginx doesn't serve php. Apache doesn't either.". It's been said in the thread already :) the OP has that understanding. I did read your reply fully by the way.

1.7 has a few from 1.6, SPDY 1.3 is only in 1.7? It's mainly a few features that have been added.

Please or to participate in this conversation.