3254effab3547d0768151e1aa14cf94d56927528
By:
irakliGM

100% CPU Usage on Wordpress Website

September 28, 2016 740 views
Nginx WordPress PHP Ubuntu

Hi there. I have $5 droplet, running a Wordpress website for flash games. When the online users hit 30+ CPU usage is 98.5% almost always. I do not use any resourceful plugins, besides WP Super cache. I have followed all of the tutorials published on digital ocean how to run a Wordpress website. I ran top command to see what was the problem and here is the result: Link Maybe my nginx server block is not configured properly? Here it is:

server {
       listen         80;
       server_name    www.gungameshub.com gungameshub.com;
       return         301 https://$server_name$request_uri;
}


server {
        listen 443 default_server ssl;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html;
        index index.php index.html index.htm;
        server_name gungameshub.com www.gungamehub.com;

        ssl on;

        ssl_certificate www.gungameshub.com.chained.crt;
        ssl_certificate_key www.gungameshub.com.key;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

        include /etc/nginx/block.conf;


        location /xmlrpc.php {
                deny all;
        }
        set $cache_uri $request_uri;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
                set $cache_uri 'null cache';
        }
        if ($query_string != "") {
                set $cache_uri 'null cache';
        }

        # Don't cache uris containing the following segments
        if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locat$
                set $cache_uri 'null cache';
        }

        # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
                set $cache_uri 'null cache';
        }

        # Use cached or actual file if they exists, otherwise pass request to WordPress
        location / {
                try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
        }

        location /articles/ {
                try_files /articles/wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /articles/index.php?$args;
        }

        location /m/ {
                try_files /m/wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /m/index.php?$args;
        }


        # Feed
        location ~* \.(?:rss|atom)$ {
          expires 1h;
          add_header Cache-Control "public";
        }

        # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
          expires 30d;
          access_log off;
          add_header Pragma public;
          add_header Cache-Control "public";
        }

        # CSS and Javascript
        location ~* \.(?:css|js)$ {
          expires 1y;
          access_log off;
          add_header Pragma public;
          add_header Cache-Control "public";
        }
        location = /mobile/ {
                return 301 /m/;
        }

        location = /blog {
                return 301 /articles/;
        }

        location = /blog/ {
                return 301 /articles/;
        }

        location = /mobile {
                return 301 /m/;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
        }
}

4 Answers

do you have php-opcache installed ?

In addition to disabling all plugins (other than WP Super Cache), you probably want to switch to PHP 7. From the screenshot you sent, you're using PHP 5. There were huge speed improvements made in PHP 7 that result in PHP 7 needing much less CPU.

After looking at /var/log/nginx/error.log I found out, that timthumb triggered tons of php errors due to not-existence of cache folder. Fixed it, fixed the problem.

Have another answer? Share your knowledge.