Nginx shared hosting wordpress
This is a guest post written Austin W. Gunter, the Brand Ambassador of WP Engine’s Managed WordPress Hosting Platform .
WordPress’s development has been intertwined with Apache since the beginning. WordPress was built to be served on Apache. Recently Nginx has risen to prominence to serve traffic from 11.4% of all known traffic.
Automattic moved WordPress.com over to Nginx, and managed WordPress hosts like WP Engine, and ZippyKid have adopted the technology as well. WordPress.com has publicly stated they have thousands of servers serving hundreds of thousands of requests per second. I spend most of my days talking about complicated server technology, but I hadn’t found a good resource to actually explain, in conversational terms, why Nginx is cool, why people are using it, and what the tradeoffs are between it and Apache. This post is intended to explain what Nginx is at a high level, not provide a “how to” for folks who may want to install it themselves.
First off, Nginx and Apache are not equal, but what makes them different? What makes Nginx suited to handle high performance web hosting tasks – such as serving an onslaught of concurrent requests when a post makes the front page of HackerNews or Reddit? What do you still need Apache for?
Nginx was developed, in part, to solve the C10k problem, or optimizing web server software to handle a large number of clients at the same time. C10k stands for concurrent ten thousand connections. When a blog post hits the front page of HackerNews or Reddit, thousands of visitors can arrive at your site at once. Nginx is event-based, and is fantastic at responding to concurrent requests.
Nginx is designed to handle thousands of concurrent connections with low CPU usage. It is a non-blocking event-driven web server, using a single process (or a small number of processes) to handle connections using evented I/O. Apache has several Multi-Processing Modules (“MPM”), but for most Linux systems, Apache’s default is the “prefork” MPM. Apache recommends using the “worker” MPM unless you’re using PHP – since PHP isn’t thread safe. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time. Nginx’s event-based” design allows it to serve data quickly, and with many concurrent connections, but we sacrifice Apache’s ability to process PHP
Additionally, Apache is limited by how many threads it has CPU to run, and Apache processes are costly. At 50-300MB per process, after the of concurrent processes, default is 256, Apache’s performance will decrease. Nginx, on the other hand, can handle 10,000+ concurrent connections using about 2MB of memory, never spawning new threads or processes. That’s absurdly scalable.
So how do we translate WordPress PHP with Nginx? Apache often bundles PHP with its processes, but to process PHP, Nginx needs to be paired with an external process like FastCGI, PHP-FPM, or HTTP. This can be done fairly easily. What’s more, by separating processes we can actually see more speed gains with Nginx. It will forward requests to process PHP externally, and send the response back to the browser.
Additionally, these can be configured to process different requests on various servers. This is a big deal for web host or sysadmin, because it means they can specify which requests go to which server, reducing request backlog. It also means that server and application processes can be restarted separately.
WordPress.com makes heavy use of Nginx’s ability to proxy_cache. You can program Nginx to send requests to the backend, but on the way back out have it hold the request on disk for “X seconds” so that the next requests can serve the page without having to go back to Apache for each new user.
3rd Party Modules
With Nginx gaining prominence, we’re seeing developers start to create cool modules we can compile into Nginx that add functionality we might need. As with any open-source project, these modules will likely be a key source of innovation on the web server.
Those are some huge advantages. But it’s important to note that Nginx isn’t an appropriate solution for shared hosting solutions, and it’s certainly not necessary for everyday WordPress blogs that don’t see high levels of traffic. Under minimal traffic, visitors may not notice any speed difference between Apache and Nginx. You want to serve WordPress off of Nginx if you expect to someday experience traffic spikes, and want to keep your site up and loading pages at sub 1.5seconds. Under load Nginx’s event-based processing, ability to serve statics quickly, and proxy_cache requests give it the ability to serve tens of thousands of connections on 2MB of CPU. That same traffic would cause Apache to commit harakiri.
Because Nginx is so scalable, Automattic, WordPress.com, and Managed WordPress hosts like WP Engine or ZippyKid have adopted the technology, usually along with Apache to serve PHP, and are using it to provide massively scalable hosting for tens of thousands of domains. Apache is still the place to process complex requests, but it cannot keep up with the scalability of Nginx.
What is your site running on? Have you considered installing Nginx to serve WordPress? Let me know in the comments what you’re using.
Switched to Nginx a few weeks ago after growing tired of getting emails from my host saying I’d gone over my RAM allocation. Spent hours trying to configure my site to cope with heavy traffic on Apache but no amount of caching & .htaccess tweaks made a difference. Switched to Nginx, created a basic .conf and haven’t looked back. RAM usage is under control and the site is more responsive. Brilliant.
I switched to Nginx a one year ago. I was very interessted into the project and did not like Lighttpd.
Turns out, Nginx is just fantiastic, and is definitely the way to go.
Routing requests with Apache feeld kinda stupid to me nowadays…
I see. So that’s why there are some sites (well actually tons of them) even though they are running on Apache, still performs better than those who are with NGIX.
Oh, by the way, what about LiteSpeed? Any comment of wordpress running on LiteSpeed? LiteSpeed claims that they are 6x faster than Apache
I don’t think Nginx performs better than Apache, not in wordpress website. PHP and Apache are like brothers and many scripts are written with Apache as the first choice webserver, the only problem is while Nginx is ‘modern’ and handles traffic burst really well, Apache remains ‘ancient’ and always target on all round performance. The issue of traffic burst can be handled with load balancing. I like Apache because of it’s all round performances. WordPress does not fail to say boldly that they have nothing against Nginx but Apache still remains the first choice – codex.wordpress.org/Nginx
Have you run any performance benchmarking to see if Nginx actually performs better or worse in your environment? It’s one thing to think it and feel it, but it’s another thing to test it.
We use Nginx here on WPForce.com and it runs really well, much better than Apache ever did for us.
Similarly, both WordPress.org and WordPress.com run on Nginx — WordPress.com being the largest single installation of WordPress on the Internet.
So I recently migrated from one host to another. Previous host had my WP site running on Apache. When I set up at new host, the default was Fast-CGI (and nginx I presume) and didn’t notice or pay attention to the difference.
Everything was running fine, and fast, on the new vps, but after a few days I began to encounter “502 bad gateway error – nginx” errors. It happens when I am posting a blog post, and click on “preview.” I can still publish that post, but when I do there are lots of pieces of the page broken (various php calls, etc.) but the post is there and in tact.
I can restore the site back to a previous point and that fixes the issue for a few posts (or day), and then the problem returns.
I did add 3 new plugins when migrating (security, adrotate and super cache). I have tried removing the security and adrotate plugins to see if that’s the issue, and the problem persists. the only one I haven’t removed is super cache.
any ideas on what could be causing these 502 bad gateway errors? Never had this when running on apache.