Report this

What is the reason for this report?

How to optimize server and WordPress to reduce CPU and RAM resource consumption

Posted on August 2, 2023

Hello,

My client’s website is encountering excessive consumption of CPU and RAM resources on a regular basis.

I have included specs and helpful information below.

Server specs:

  • 2GB of RAM
  • 1 Premium AMD vCPU
  • 25GB of SSD storage
  • LEMP stack using Ubuntu 22.04, PHP-FPM, and MySQL 8.0

The server is running a single website which is powered by WordPress. WordPress is running a pre-built theme and various plugins. JetPack Boost, Redis Object Cache, and WP Optimize are installed and configured to handle caching and optimizations.

DNS for the server is handled by CloudFlare. CloudFlare Caching is set to respect existing headers set on the server.

The server is actively consuming up to 80% or 90% of memory at any given moment. I know this because I have set DO resource alerts to be sent out when the server uses more than 70% of CPU or RAM. I also have Netdata installed on the server for realtime resource monitoring.

The website receives an average of 350 users per day according to JetPack stats.

I have followed some suggestions and added the following values to the server’s my.cnf file:

**[mysqld]**
* default-storage-engine = InnoDB
* performance_schema = off
* innodb_buffer_pool_size = 256M
* key_buffer_size = 64M
* table_open_cache = 1024
* max_connections = 200
* tmp_table_size = 64M
* max_heap_table_size = 64M
* thread_cache_size = 16
* slow_query_log = 1
* slow_query_log_file = /var/log/mysql/mysql-slow.log
* long_query_time = 2

I have used WP Optimize to convert the database and all of the tables to use InnoDB from MyISAM.

The plugin Index WP MySQL was installed to add high performance keys to the tables that did not have them.

The following Nginx configuration file is setup:

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MY_CACHE:100m inactive=60m;

fastcgi_cache_key "$scheme$request_method$host$request_uri";

limit_req_zone $binary_remote_addr zone=php:10m rate=2r/s;

server {
  server_name wanderwoman.ca www.wanderwoman.ca;
  root /var/www/wanderwoman.ca;
  index index.html index.htm index.php;
  access_log /var/log/nginx/wanderwoman.ca.access.log;
  error_log /var/log/nginx/wanderwoman.ca.error.log;
  location = /favicon.ico { log_not_found off; access_log off; }
  location = /robots.txt { log_not_found off; access_log off; allow all; }
  location ~* \.(png|jpg|jpeg|gif|webp|svg|ico|woff|woff2|ttf)$ {
    expires 365d;
    etag off;
    if_modified_since off;
    add_header Cache-Control "public, no-transform";
  }

  location ~* \.(js|css|pdf)$ {
    expires 30d;
    etag off;
    if_modified_since off;
    add_header Cache-Control "public, no-transform";
  }

  location ~* \.(html)$ {
    etag on;
    add_header Cache-Control "no-cache";
  }

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_read_timeout 240;
    fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    include fastcgi_params;
    fastcgi_cache MY_CACHE;
    fastcgi_cache_valid 200 60m;
    add_header X-Cache $upstream_cache_status;
    limit_req zone=php burst=10;
  }

  location ~ /\.ht {
    deny all;
  }

  location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php|js|swf)$ {
    deny all;
  }

  location ~* wp-config.php {
    deny all;
  }

  location ~ \.user\.ini$ {
    deny all;
  }
  
**fastcgi-php.conf file:**

fastcgi_split_path_info ^(.+?\.php)(/.*)$;

try_files $fastcgi_script_name =404;

set $path_info $fastcgi_path_info;

fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;

include fastcgi.conf;

PHP memory limit is set to 256MB.

What steps could I take to optimize the resources on the server and make the website perform better?

Cheers,



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

It sounds like you’ve already done some very good work to optimize most of the standard things.

There are a few other things that I could suggest:

  • Add a swap file so that you could have some extra memory buffer:

https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04

  • Consider removing any plugins that are not critical for your site. for example, I know that Jetpack could sometimes increase the memory consumption of your website and considering that you only have 2GB of RAM that might be an issue. Removing any other plugins that are not used could also be beneficial.

  • Once you’ve added the swap file, follow the steps form this answer here on how to use the MySQL tuner script to analyze your MySQL database and make the recommended changes:

https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

In some cases, if you allocate too much resources to MySQL it could explain the 90% memory consumption.

I would also recommend using the htop command to see which service exactly is consuming the majority of the resources:

https://www.digitalocean.com/community/questions/how-to-find-the-processes-that-are-consuming-the-most-server-resources

Let me know how it goes!

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.