Pankaj Shukla
Pankaj Shukla

Reputation: 11

Very high CPU consumption for Drupal7 + NginX and PHP-FPM

We are working on an application which is using Drupal 7 on (NginX + PHP-FPM). But during performance testing we have observed that Applications Server consuming very high CPU. Average CPU consumption was in the range of 90% (Max processor is consumed by PHP-FPM). Memory consumption is on very lower side.

We have tried with various Concurrent Users load like 25-50-100. But even with this User load we observed CPU consumption was ~90% due to which response time is very high.

We are using 4 Core CPU with RHEL (AWS EC2) OS. Application flow is Reverse Proxy (Nginx) -> Application Server (Drupal 7+NginX+PHP-FPM)

Below are the configuration changes I did-

nginx.conf:-

user              nginx;
worker_processes 4;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
worker_rlimit_nofile    20480;
events {
   use epoll;
   worker_connections  10240;
   multi_accept on;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay     on;
    server_tokens   off;
    keepalive_timeout  65;
    gzip  on;
...
}

virtual.conf:-

location ~ '\.php$|^/update.php' {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index  index.php;

    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

    fastcgi_buffer_size 128k;
    fastcgi_buffers 256 16k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    include fastcgi_params;
}

php-fpm.conf:-

emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 15s
daemonize = no

php-fpm/www.conf:-

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0664
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

Can someone please guide us how to resolve this issue?

Upvotes: 0

Views: 834

Answers (1)

datasage
datasage

Reputation: 19573

You can do the following to improve performance in a production install.

  1. Use PHP 5.5 or newer. (7 is better if your application supports it) 5.3 is no longer supported.
  2. Enable PHP optimizer (included with 5.5, may be a separate package) or use something like APC with older versions. This can provide a significant performance boost, allows compiled code to be cached and reused.
  3. Use caching. Options with this will vary depending on your application. Being able to store data and read data from memcached if it changes infrequently can be a performance boost. APCu (user cache of apc) can be used as an in memory cache. Some applications detect and use this if available.
  4. Add profiling. Look at something like newrelic here. It will show you where your application is spending most of its time.
  5. If your application does any code generation (like doctrine), make sure this configured to run manually on deployment and not on every request.

If you have done any custom development on drupal, you may want to check your code to make sure its not spending an excess amount of time doing things like database queries. Profiling can help identify these issues.

Depending how much you can leverage caching, you should be able to get quite a few more users on your instance.

Upvotes: 1

Related Questions