Nginx + PHP = VERY High CPU Usage

Hello All,

I was using APACHE and I decided to migrate to nginx.
I researched and nginx is supposed to have a completely superior
performance for webpages that have lot of simultaneous requests, such as
real time pages sending AJAX requests every second.

My application is based on MYSQL + PHP + AJAX + JAVASCRIPT.

After I installed the NGINX, I started testing it, and noticed that
something is strange due the HIGH-CPU usage of PHP-CGI process.

The curious is that with around 6-8 Tabs open on Firefox, I can reach
around 80% of CPU usage. that is completely high, even with this test
machine that have a cpu of only 1.8ghz. But talking about only 8 web
pages open, reach this amount of CPU usage, I think that something is
wrong.

Anyone can please advise me?

[size=x-small]last pid: 39019; load averages: 0.98, 0.22, 0.08

up 4+16:53:45 17:03:29
51 processes: 2 running, 49 sleeping
CPU: 84.8% user, 0.0% nice, 14.4% system, 0.4% interrupt, 0.4% idle
Mem: 46M Active, 204M Inact, 83M Wired, 128K Cache, 60M Buf, 157M Free
Swap: 991M Total, 991M Free

PID USERNAME THR PRI NICE SIZE RES STATE TIME WCPU COMMAND
39017 www 1 70 0 18908K 10932K accept 0:04 17.09% php-cgi
39009 www 1 72 0 18908K 11396K accept 0:05 16.26% php-cgi
39008 www 1 68 0 18908K 11428K accept 0:05 15.67% php-cgi
39019 www 1 66 0 18908K 10932K accept 0:03 15.48% php-cgi
39018 www 1 64 0 21980K 13100K RUN 0:03 13.96% php-cgi
954 mysql 14 44 0 63332K 30672K sbwait 5:55 0.00% mysqld
1150 daemon 6 44 0 13460K 6716K ucond 0:34 0.00%
memcached
1617 nobody 1 44 0 5348K 4180K kqread 0:12 0.00% nginx
1009 root 1 44 0 6080K 3476K select 0:11 0.00%
sendmail
1020 root 1 44 0 3376K 1372K nanslp 0:04 0.00% cron
1001 root 1 44 0 6704K 3744K select 0:02 0.00% sshd
642 root 1 44 0 3348K 1336K select 0:02 0.00% syslogd
38189 rafael 1 44 0 9428K 4552K select 0:01 0.00% sshd
1013 smmsp 1 44 0 6080K 3332K pause 0:00 0.00%
sendmail
783 www 1 73 0 17884K 8412K wait 0:00 0.00% php-cgi
812 nobody 1 44 0 3292K 1296K select 0:00 0.00% noip2
38186 root 1 44 0 9428K 4488K sbwait 0:00 0.00% sshd
1487 root 1 44 0 5348K 3636K pause 0:00 0.00% nginx
38193 root 1 44 0 4564K 2404K wait 0:00 0.00% bash
39016 root 1 44 0 3684K 1956K RUN 0:00 0.00% top
823 root 1 76 0 3628K 1504K wait 0:00 0.00% sh
38192 rafael 1 44 0 3808K 1724K wait 0:00 0.00% su
38190 rafael 1 45 0 3628K 1612K wait 0:00 0.00% sh
1084 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
508 root 1 44 0 1888K 584K select 0:00 0.00% devd
1086 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
1085 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
1089 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
1087 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
1090 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
1091 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
1088 root 1 76 0 3348K 1156K ttyin 0:00 0.00% getty
120 root 1 76 0 1540K 844K pause 0:00 0.00%
adjkerntz[/size]

Posted at Nginx Forum:

Are you using APC cache? You may need to adjust the APC config to get
it to work under php-cgi? Just a guess…

I’m not using APC because I’m using PHP-MEMCACHE.

Do you think that would be necessary the APC?

By the way this is my configure

[size=x-small]Configure Command ‘./configure’ ‘–with-gdbm’
‘–with-mysql=/usr/local/mysql/’
‘–with-mcrypt=/usr/ports/security/mcrypt’
‘–with-gd=/usr/ports/graphics/gd/work/gd-2.0.35/’ ‘–with-png-dir=/usr’
‘–with-zlib-dir=/usr’ ‘–with-xpm-dir=/usr’ ‘–with-freetype-dir=/usr’
‘–with-jpeg-dir=/usr’ ‘–with-layout=GNU’ ‘–localstatedir=/var’
‘–with-config-file-scan-dir=/usr/local/etc/php’ ‘–enable-libxml’
‘–with-libxml-dir=/usr/local’ ‘–with-pcre-regex=/usr/local’
‘–with-zlib-dir=/usr’ ‘–with-regex=php’ ‘–with-zend-vm=CALL’
‘–prefix=/usr/local’ ‘–mandir=/usr/local/man’
‘–infodir=/usr/local/info/’ ‘–build=i386-portbld-freebsd8.1’
‘–enable-mbstring’ ‘–enable-fastcgi’ ‘–enable-soap’ ‘–with-curl’
‘–with-mcrypt’ ‘–enable-zip’ ‘–with-bz2’
Server API CGI/FastCGI[/size]

On apache the production server is running nice, with mod_fastcgi.

Looking forward to figure out that =)

Thanks for your reply!

Posted at Nginx Forum:

On Fri, Dec 10, 2010 at 11:35 AM, hasson [email protected] wrote:

I’m not using APC because I’m using PHP-MEMCACHE.

Do you think that would be necessary the APC?

APC will complement memcache. You can cache your application data in
memcache and cache your PHP op code in APC.

‘–with-libxml-dir=/usr/local’ ‘–with-pcre-regex=/usr/local’
‘–with-zlib-dir=/usr’ ‘–with-regex=php’ ‘–with-zend-vm=CALL’
‘–prefix=/usr/local’ ‘–mandir=/usr/local/man’
‘–infodir=/usr/local/info/’ ‘–build=i386-portbld-freebsd8.1’
‘–enable-mbstring’ ‘–enable-fastcgi’ ‘–enable-soap’ ‘–with-curl’
‘–with-mcrypt’ ‘–enable-zip’ ‘–with-bz2’
Server API CGI/FastCGI[/size]

On apache the production server is running nice, with mod_fastcgi.

Weird. There must be something different about the way the php-cgi
daemons are spawned.

Looking forward to figure out that =)

Maybe you can try running strace on the php-cgi process and see what
it’s doing.

On 12/10/2010 08:35 PM, hasson wrote:

I’m not using APC because I’m using PHP-MEMCACHE.

Memcache doesn’t replace the opcode caching functions of APC.

Do you think that would be necessary the APC?

Without APC your scripts always have to be compiled for every request
which
is almost never what you want. Using APC isn’t “necessary” per se but
very
strongly recommended.

Regards,
Dennis

On Fri, 2010-12-10 at 14:05 -0500, hasson wrote:

Hello All,

Hi hasson

I was using APACHE and I decided to migrate to nginx.
I researched and nginx is supposed to have a completely superior
performance for webpages that have lot of simultaneous requests, such as
real time pages sending AJAX requests every second.

nginx will have better performance mostly on static content

wrong.

Anyone can please advise me?

try eaccelerator


Maxime Ducharme
Systems Architect

Hello all!

I’m very happy to post that APC module REALLY make the difference!!!

Now the CPU usage is as supposed to be :slight_smile:

If you are using nginx+php you SHOULD use APC!

Thanks for the TIP =D

Posted at Nginx Forum:

You should also use php-fpm.