How to Reduce High CPU Usage on VPS | Simple Solutions To Fix High CPU Usage on Website

When it comes to hosting your website on VPS (Virtual Private Server), high CPU usage is a major concern because it will worsen the performance of your website. Sometimes adding an extra core might fix your problem. But if the problem lies somewhere else, the additional core will not be helpful.How to Reduce High CPU Usage on VPS

One of my websites that I had developed from scratch had high CPU usage problem. I wasted a few hundred dollars to upgrade my VPS and finally, I found some ways to fix the issue. In this post, I am going to share my experience and it may work for you.

If you have developed your website in PHP and MySQL, there is a high chance that MySQL and poor disk IO are triggering high CPU usage. You can SSH to VPS and see the resource hungry processes by running the “top” command. The attached figure is a screen dump of the “top” command. You can see the load average is below 0.1. So far it has not touched 0.20 even during the peak time.

Here are the things that you should try to fix high CPU usage problem.

  1. Use SSD (Solid state drives) disk: If your hosting company is still providing HDD ( Hard Disk Drives), it’s the right time to divorce. There are many good hosting companies that provide SSD disk (check this list); go for one of them. Before buying VPS, ask the provider about disk IO speed. If disk IO is less than 100MB/s, avoid the provider. If you want to get the best out of the MySQL server, always use SSD disk.
  2. Host Static files on a separate VPS: You will find several articles on the internet that advise creating a subdomain to host static contents (e.g. images/CSS/js). If you created a subdomain to host the static contents but you are using the same VPS to host them, you will not get the maximum benefit. If your web pages have lots of images, you must host subdomain on a separate VPS. Also, you can use CDN like Cloudflare to deliver your static contents.
  3. DO NOT close MySQL connections very often and reuse MySQL connections: Opening and closing MySQL connections are the costliest operations. Check your PHP codes and make sure that you are reusing the open connections. The latest version of MySQL closes open connection once the script finishes. Relying on the system to close the open connections is also not a bad idea, however, there might be some security issue. I am not an expert in that area, so I cannot say anything about major vulnerabilities if you are relying on the system to close the connections. Reusing the open collections will give the CPU some time to breathe.
  4. Migrate to PHP7: If you are still using PHP5, you are missing the enhanced performance of PHP. There are several benchmarks (you can google it) that showed that PHP7 is at least 30% faster than PHP5.
  5. Stop generating access log: If you have installed some software on your VPS to generate the access log files, you should stop it immediately because it’s sheer wastage of IO. You can use google analytics to get the details of your traffic.
  6. Install caching software and enable preload: If you are using WordPress and have not yet installed any caching plugin, you must install one that supports preload. WP Fastest Cache is my favorite caching plugin.
  7. Avoid Cheap VPS providers: If some hosting companies are selling cheap VPS, it is 100% true that they are overselling the nodes. There is no other way to give cheap VPS.
  8. Avoid OpenVZ and go for KVM: Although KVM plans are costlier than OpenVZ plans, you should always buy KVM VPS. There are many providers who will give you a discount if you pay them annually. You can check the list of some providers I have mentioned in option 1.

If you try all these options, hopefully, the server load will come down. I was able to bring my CPU usage down from 0.80 to 0.10 during the peak time.

Similar Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.