High Apache CPU usage, but low nginx – Configured correctly?

pixelkicks asked:

We’ve just moved a website of ours over to a brand new high-spec Linux server (1x Intel Xeon E3-1230 v2 @ 3.30GHz, 8GB DDR3 ECC, 2x 128GB SATA SSD RAID1).

The server has been configured to use nginx but we’re not sure if its working correctly. The site always loads very fast to us (http://www.onedirection.net), but Plesk often sends us reports that the Apache CPU usage percentage reaches high leves, yet when we look at the nginx percentage it’s always very low.

We’ve come from a Windows background so are very new to Linux, but shouldn’t nginx run INSTEAD of apache?

Here’s a screenshot from Plesk showing the CPU usage:

The website gets around 20,000 visitors per day, and we use W3 Total Cache to get it running as fast as possible. MySQL has been optimised well.

Memory usage is only running at 2GB of the 8GB.

Does this look right? How can we tell that nginx is doing most of the work?

My answer:

Apache’s “high” CPU usage here is not actually attributable to Apache, but to PHP, which is actually loaded as a shared object within the Apache executable. Thus, the CPU time taken up by your WordPress and any other running PHP scripts gets attributed to Apache, since PHP runs within Apache’s process.

In this scenario there’s no real need to have Apache at all. A typical nginx setup would have nginx as the origin server and php-fpm (which runs PHP as separate processes) running the PHP scripts. Sample configurations for nginx and php-fpm are given on WordPress’s own wiki, and W3 Total Cache will generate its own configuration snippets for nginx.

Using an nginx + php-fpm configuration also gives you an advantage in that it’s easier to pinpoint slow PHP scripts, as the php-fpm SAPI contains slow script logging ability (and no other PHP SAPI does).

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.