Question

How to optimize server and WordPress to consume less CPU and RAM resources

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;

The 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,


Submit an answer


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!

Sign In or Sign Up to Answer

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.

KFSys
Site Moderator
Site Moderator badge
September 6, 2023

Heya,

Optimizing a server and WordPress installation can significantly improve performance and reduce resource consumption. Here are steps and tips to achieve this:

1. Server-Level Optimizations:

  1. Choose the Right Server Size: Ensure your server (or droplet, if you’re using DigitalOcean) is appropriately sized. Sometimes undersized servers can lead to resource issues.

  2. Server Software: Use lightweight server software. Nginx is generally considered lighter and faster than Apache.

  3. Use PHP-FPM: PHP-FPM is a PHP FastCGI Process Manager, which can improve PHP’s performance.

  4. Update PHP Version: Newer PHP versions (e.g., PHP 7.4, 8.0) are much faster and more efficient than older ones.

  5. Optimize MySQL:

    • Adjust the my.cnf or my.ini configuration file. Tools like MySQLTuner can guide on possible optimizations.
    • Ensure your tables are optimized. You can use mysqlcheck or phpMyAdmin to do this.
    • Regularly back up and then clear out old logs.
  6. Use Local DNS Caching: Install and configure a local DNS cache using something like dnsmasq.

2. WordPress-Level Optimizations:

  1. Caching Plugins: Install a caching plugin. Some popular options include:

    • W3 Total Cache
    • WP Super Cache
    • WP Rocket (premium)
  2. Object Caching: Consider using Redis or Memcached for object caching. Some plugins, like Redis Object Cache, can help integrate Redis with WordPress.

  3. Optimize Images:

    • Use compressed image formats (like WebP).
    • Use plugins like ShortPixel, Smush, or Imagify to automatically compress and resize images.
  4. Limit Plugins:

    • Deactivate and delete unnecessary plugins.
    • For plugins that are used sporadically, consider activating them only when needed.
  5. Optimize Themes: Use lightweight and well-coded themes. Child themes can sometimes introduce overhead, so ensure they’re optimized.

  6. Limit Post Revisions: Limit the number of post revisions stored in the database by adding define('WP_POST_REVISIONS', 3); to your wp-config.php file.

  7. Use Content Delivery Network (CDN): Implement a CDN like Cloudflare, KeyCDN, or StackPath to offload the serving of static resources.

  8. Database Cleanup: Use plugins like WP-Optimize or WP-Sweep to clean up old revisions, transient data, and other unnecessary database content.

  9. Lazy Load Media: Consider using lazy loading for images and videos. This means media items are only loaded when they are visible in the viewer’s browser.

  10. Disable WP-Cron: By default, WordPress runs a pseudo-cron job on every page load to check for scheduled posts or updates. If your site doesn’t have frequent posts, consider disabling it and setting up a real cron job.

  11. Limit Heartbeat API: The WordPress Heartbeat API can generate requests that consume server resources. Consider using a plugin like “Heartbeat Control” to manage or limit these requests.

3. Monitoring and Profiling:

  • Server Monitoring: Tools like htop, top, vmstat, and iostat can help monitor server resources. This helps in identifying any bottlenecks or processes consuming excessive resources.

  • WordPress Profiling: Plugins like Query Monitor can help you identify slow queries or plugins/themes that are consuming resources.

By implementing the above optimizations and continuously monitoring the performance, you should be able to reduce the CPU and RAM resource consumption of your WordPress site.

Hi,

I recommend GZIP compression.

Because GZIP compression is a way to make website files smaller before they are sent to visitors’ browsers. It helps websites load faster and saves bandwidth. When a file is requested, the server compresses it and adds a special header to the response. The browser then knows how to decompress the file and show the original content to the user. This results in quicker page loading, better user experience, and even potential SEO benefits. Enabling gzip compression is simple, and most modern web servers and content management systems support it.

Kind regards, Farhad Malegam - Technical Director Sydney Digital Agency

Bobby Iliev
Site Moderator
Site Moderator badge
August 3, 2023

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel