Nginx – php-fpm: very high server load

cpu-usagenginxPHPphp-fpm

Since today my webserver (nginx + php-fpm + mysql on a VPS) is very slow.

htop says:

 1  [|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]     Tasks: 63 total, 13 running
  2  [|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]     Load average: 11.67 10.95 6.95
  3  [|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]     Uptime: 00:18:40
  4  [|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]
  5  [|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]
  6  [|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]
  Mem[|||||||||||                                                137/1280MB]
  Swp[                                                                0/0MB]

  PID USER     PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 6802 www       20   0 76232 12320  5716 R 27.0  0.9  0:06.48 php-fpm: pool www
 7048 www       20   0 75200 12136  5700 R 52.0  0.9  0:03.64 php-fpm: pool www
 6699 www       20   0 74176 11124  5700 R 27.0  0.8  0:07.36 php-fpm: pool www
 7029 www       20   0 73668 10380  5676 R 42.0  0.8  0:03.52 php-fpm: pool www
 6995 www       20   0 76228 12456  5644 R 42.0  1.0  0:03.98 php-fpm: pool www
 6858 www       20   0 74172 10684  5620 R 35.0  0.8  0:05.52 php-fpm: pool www
 6998 www       20   0 75200 12072  5620 R 37.0  0.9  0:03.95 php-fpm: pool www
 7098 www       20   0 75200 12052  5616 R 42.0  0.9  0:02.33 php-fpm: pool www
 7093 www       20   0 76228 12496  5612 R 37.0  1.0  0:03.02 php-fpm: pool www
 7226 www       20   0 74692 11080  5588 R 32.0  0.8  0:00.66 php-fpm: pool www

CPU: (cat /proc/cpuinfo | grep model)

model           : 44
model name      : Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz

I think a load of 10 and 100% cpu usage is not normal… How can i fix that?

Edit:
I think it could be because of mysql. Here is the PhpMyAdmin Process List:

Show Full Queries   ID  User    Host    Database    Command     Time    Status  SQL query
Kill    3567    bs  localhost   bs  Sleep   2   ---     ---
Kill    3575    bs  localhost   bs  Sleep   1   ---     ---
Kill    3576    bs  localhost   bs  Sleep   1   ---     ---
Kill    3578    bs  localhost   bs  Sleep   0   ---     ---
Kill    3579    bs  localhost   bs  Sleep   0   ---     ---
Kill    3580    bs  localhost   bs  Query   0   Writing to net  SELECT count( id )
FROM messages
WHERE uid_to =345
AND unread =1
Kill    3581    bs  localhost   bs  Sleep   0   ---     ---
Kill    3582    unauthenticated user    connecting host     None    Connect         login   ---
Kill    3583    phpmyadmin  localhost   None    Sleep   0   ---     ---
Kill    3584    root    localhost   mysql   Query   0   ---     SHOW PROCESSLIST

As you see, there is only one thread used, but Google Analytics says:

Right now
429
active visitors on site 

I connect to Mysql per PDO. How can I get more threads?

Best Answer

Here is a link to profile your code: StackOverflow: Simplest way to profile a PHP script