Why nginx is WordPress Hosting’s Best Friend
In my previous article I explored why PHP7 is the way to go for WordPress and in today’s article it’s the webserver’s turn.
A big part of a good hosting experience is how the webserver stack is configured. Having a fast stack is paramount to good WordPress performance & a great user experience. I’ve already discussed why PHP7 is such a good way to automatically improve WordPress performance but what about the actual webserver?
Today I’m going to explain: what is a webserver, what are the most popular ones and why nginx is so damn good!
The Webserver
One of the most important aspects of hosting is the actual webserver. The webserver is responsible for providing static content such as HTML and sending it to the client (which means, right into your browser). So the webserver will also be responsible for processing all the information that comes via the PHP processor and converting it to bits that travel through the network into your browser. The webserver stack works with the client / server model where the webserver is acting as a server (provider of content) and the client (you) the one that request it.
The most common webservers are Apache, IIS from Microsoft and of course nginx.
The webserver is able to handle both static & dynamic content that is sent to the client with some slightly differences. The server part loads into memory in what is called a worker process and it’s responsible of providing the content requested. Both Apache & nginx handle static and dynamic content. While Apache is able to handle both inside the same worker process, nginx does not as it needs an external processor to do it.
Why are Apache and nginx so popular? The simple answer is because they are Open Source and free. Together they make up almost 50% of all the traffic in the world, not an easy feat!
What is Apache?
The Apache HTTP server was created by Robert McCool in 1995 and it’s still under development under The Apache Software Foundation. This webserver has been the most popular choice since 1996. It’s very modular in design, has tons of documentation and it’s so popular that it’s the basis for all the major Web Hosting Control Panels. It’s even included in cPanel, the most used control panel in the world.
Of course it’s flexible, that’s the excuse that the vast majority of people using it will come up with when asked and generally, that’s true. It is also very extensible and since it can load modules dynamically (something nginx is still working on) it can be configured with ease.
Apache is able to handle static and dynamic content all by itself in the same worker so it’s perfect for it’s flexibility but (and there is always a but) it does so at the expense of performance.
Apache has been known for being somewhat slow but nothing came close to exposing this truth as when nginx became popular. The performance difference between them is something that cannot be overlooked. For this matter alone, most major companies are switching over to nginx. Even though Apache is still popular, things will surely change in the future as technology catches up and more and more software that were previously working only on apache start to include nginx.
What About Nginx?
Nginx comes from mother Russia. In 2002 a programmer named Igor Sysoev began working on nginx as he was profoundly concerned about the C10K problem. The what? To put it simply, optimizing network sockets to handle a large number of clients at the same time which was Apache’s main drawback and a real challenge for the modern web. The initial nginx release was made public in 2004 based on an events-driven architecture.
Not even Sysoev himself could see how much success nginx would have. The tremendous benefits of speed under nginx and the low resource utilization became evident as time passed. Nginx has a magnificent way of providing with static content but cannot handle dynamic content. This also proved to be a benefit because the dynamic content wasn’t bound to the same worker and thus, could work much faster.
Nginx excels at providing faster content to a much higher volume of connections than Apache and is currently the choice for hosting services that want to offer the best performance possible. It’s event-driven architecture and it’s asynchronous nature are the main points for it’s performance.
Initially nginx had one main drawback, it’s documentation. It had a rough start due to the programmer being Russian and the way he documented it’s functions. But as time has passed and more and more people use nginx the documentation has improved immensely.
The other drawback of nginx it’s in it’s lack of modular design which means it cannot process rules in realtime like Apache does with it’s .htaccess file and cannot load modules in realtime either. This is something that nginx is already working on but will still need time to match Apache’s flexibility.
The drawbacks nevertheless are quickly overshadow by the superb performance. This in itself has allowed nginx to become the popular choice for performance oriented hosting and it’s the basis for service as widely used as Steam, proving time and time again that it can consistently score higher than Apache in every way.
Why is Nginx the Preferred Choice for WordPress Hosting?
There are several points in which nginx is far better than Apache for hosting WordPress sites. I’ll try to summarize this into the following list.
Easy to Install
Nginx is a complete package, it requires very few external modules to work and the vast majority of the time, it only takes one line in linux to enable it.
apt-get install nginx
yum install nginx
It’s all it takes for nginx to be active as a service. The rest is in the configuration.
Can work as a proxy for Apache or any other webserver
Nginx can provide an instant performance increase to any Apache webserver configuration by running as a proxy without affecting the functionality of the server in any way.
Can work directly with a microcache or even fastcgi cache
Nginx is great thanks to it’s built in microcache and can even work with an fastcgi cache to speed up performance by a factor of 10. Nginx can also provide a tremendous increase in performance when working with a fastcgi cache and won’t even need an external caching mechanism such as memcache.
It’s incredibly fast for serving content
Nginx is lighting fast when dealing with static content and it’s very easy to configure for such purposes. A single line of code can enable access the most common files such as static content and improve it’s performance. Plus, if you know how to configure HttpProxyModule you can even achieve the same level of performance for Dynamic content without affecting the functionality of the webserver.
Almost 4 times more concurrent connections than Apache
If you’re considering hosting a very high demand website with tons of thousands of visits per day it would be good for you to know that nginx can process as much as four times the amount of concurrent connections as Apache, which means 2 things:
- Your website will be faster overall
- You will need fewer resources than Apache to do it.
This is due to the event-driven nature of nginx and it’s reponsible for the next benefit.
Very Lightweight
Nginx is so well crafted that it’s incredible lightweight on resources. This means it will use less CPU time to process each connection request and will do so using a lot less memory. This will allow you to host bigger sites using the same server that were previously unable to do so in Apache.
The secret of it’s success is in the engine itself. Nginx works as an event triggered webserver which means it will listen “events” on the worker and will respond only when needed. The ondemand argument inside nginx allows the service to spawn and remove listeners on demand. This helps reduce memory usage and increase efficiency. Apache tried to include an event mode in the mpm-event worker but it failed miserably in matching nginx speed because Apache wasn’t constructed as an event-driven webserver.
It can do Load Balance
Nginx can be configured for load balance, hence, increasing it’s rate of concurrent connections even more by spreading the load between several servers making it ideal for projects that have a very high load of concurrent connections.
What Does This All Mean?
It means that nginx is ideal for WordPress in almost every way. It means that you will reap the benefits of using it almost immediately after implementation. If you are a system administrator, installing nginx is a no-brainier. If on the contrary you want to design a project or host your website and want to host it elsewhere, chances are really high that if your hosting use nginx, it will work faster.
High quality hosting companies such as WP Engine and Flywheel already run on nginx. But if you’re going to go with a different hosting company, do your research or ask a support tech if the webserver stack includes nginx. I can guarantee you that if the service is a quality one it will have nginx included in some part of the stack.
Excellent article. Thanks for sharing with us. I have learned something new from your article. There are different web hosts in the market. A good host can lead your site to the next level. So a perfect host is the must.
So… nginx or nginx+apache?
nginx+redis cache+php7 will be a good combo for a wp site. 🙂