How to Fix High CPU Usage on a Linux VPS

A Linux Virtual Private Server (VPS) has serious consequences if it’s high on cpu usage. It slows down website response time, degrades application functionality, and can even take down the stability of the server. If it is taking long, is not responding at all, or if it is crashing often, then it may be experiencing high cpu usage. Whether you are operating a WordPress website, an AI Automation server, a server with Docker, or a database application, knowing how to keep CPU usage low on an Ubuntu server is a prerequisite to optimal functionality.

To resolve high cpu usage on Linux VPS, you must monitor, perform optimization, and complete preventive maintenance. After analyzing the resource-gutting processes, closing off the server to security threats, and adjusting the configurations, you will likely restore performance and prevent additional increases. If the optimized attempt is unsuccessful, then the best route for assurance of the VPS staying reliable and running smoothly is upgrading the resources.

This guide will walk you through how to fix high CPU usage on a Linux VPS.

Understanding High CPU Usage

Your websites will load more slowly when there is high CPU usage on your Linux VPS if you use CPU-intensive applications, and may even go offline unexpectedly. Services like Apache HTTP Server, NGINX, and databases like MySQL that consume high CPU resources are usually a sign of an underlying trouble in your CPU resource usage, performance, or configuration. Learning how to diagnose, implement, and maintain high CPU usage issues in your VPS is crucial.

First, you need to confirm that your server is indeed overloaded before you start troubleshooting. CPU usage causes an issue when it:

  • Persists above 80 – 90 % for an extended period of time
  • The load average is greater than the number of CPU cores
  • SSH is lagging
  • The website is lagging

You can find the CPU load average and the load average with the uptime command. The command shows how heavily your system is loaded over the last 1, 5, and 15-minute intervals.

uptime

If your VPS is a 2 CPU core VPS and it is consistently above a 2.0 load average, then your system is under stress.

Solution 1: Identify the Process Causing High CPU

Find out which application is using the high CPU. This is the first step before you implement any fixes.

Monitor Real-Time CPU Usage with top

The top command shows real-time CPU usage and the active processes. This command helps determine which program is using the highest CPU resources.

top

Look for the %CPU column and PID.

Monitor Real-Time CPU Usage with top

To sort processes in order of CPU usage, press the letter P.

Use htop for Better Visualization

htop has a more visually appealing, color-coded, standardized interface, which makes identifying and controlling processes much easier. If htop is not available, install it: 

sudo apt install htop -y
install htop

After installation, you can run it using the following command: 

htop
Use htop for Better Visualization

This helps visually detect and manage CPU usage spikes, and lets you kill processes.

Solution 2: Restart or Stop Problematic Services

Not all CPU spikes are a result of high demand or usage. Temporary glitches, infinite loops, and services that get stuck can also cause spikes.

Kill a Specific Process

If a process is stuck or misbehaving, eliminate it. This helps to free up CPU resources. 

sudo kill -9 PID
Kill a Specific Process to fix high cpu usage

Replace PID with the actual process ID. You can obtain the process ID using top or htop. 

Restart a System Service

Restarting a service helps ensure that the service is reloaded cleanly, which helps make sure that the service is not mishandling its functions. To restart Apache, execute the following command:

sudo systemctl restart apache2
Restart a System Service

Restart NGINX using the following systemctl command:

sudo systemctl restart nginx
restart nginx

Solution 3: Optimize Web Server Configuration

Web servers are the main cause of the high CPU usage. Apache may also be configured to accommodate an overwhelming number of simultaneous requests, leading to an overload of your VPS.

Apache Optimization

To open the configuration file, use the following command:

sudo nano /etc/apache2/apache2.conf

And apply the following settings in the “.conf” file:

KeepAlive On
MaxRequestWorkers 50
KeepAliveTimeout 2

To restart Apache, use the following command:

sudo systemctl restart apache2
restart apache

This configuration limits the overload of worker processes.

NGINX Optimization

When configured improperly, the workers may cause the CPU to be underutilized. To edit the configuration, use this command:

sudo nano /etc/nginx/nginx.conf

Apply the following settings:

worker_processes auto;
worker_connections 1024;

To restart, use the following command:

sudo systemctl restart nginx
restart nginx

This configuration helps Nginx to utilize the CPU optimally. 

Solution 4: Optimize Database Performance

A slow database query can frequently cause a CPU to spike.

Enable Slow Query Log

A slow query can cause the CPU to scan a very large dataset. To edit MySQL configuration, run the following command:

sudo nano /etc/mysql/my.cnf
Enable Slow Query Log to fix high cpu usage

Now, add the following configuration:

slow_query_log = 1
long_query_time = 2

Restart MySQL Services

To restart MySQL, run the command below:

sudo systemctl restart mysql
Restart MySQL Services

This will help you identify inefficient queries.

Solution 5: Limit Docker Container High CPU Usage

If you have Docker containers running (like OpenClaw, WordPress, Node apps), they are likely to consume an unlimited amount of CPU.

Add CPU Limits

Containers without a CPU limit can overwhelm the host system. To prevent this, edit the docker-compose.yml file:

deploy:
  resources:
    limits:
      cpus: '1.0'
Add CPU Limits

Monitor Container Usage

Monitoring shows which containers are the heaviest users of resources.

docker stats
Monitor Container Usage

Solution 6: Disable Unnecessary Services

Unneeded services consume CPU cycles. To discover superfluous background services, use the following command.

systemctl list-unit-files --type=service
identify unnecessary services

Now disable the unused services. This is an operation that lessens the load on background CPU usage.

sudo systemctl disable service-name
disable unused services to avoid high cpu usage

Solution 7: Enable Swap Space

When physical RAM is insufficient, the system may begin excessive swapping. This can lead to memory thrashing, where the CPU spends most of its time handling page faults instead of executing processes efficiently. To reduce memory pressure, you can enable swap space. Swap acts as virtual memory and helps prevent system crashes when RAM is exhausted, though it is much slower than physical memory.

Create Swap File

To create swap memory, you can use either the fallocate or dd method. This process will create a 2GB swap file on your server.

sudo fallocate -l 2G /swapfile
Create Swap File

Secure Swap File

To limit access to the swap file, it will be necessary to secure it. It is important to ensure that only the root user can access the file for reading and writing.

sudo chmod 600 /swapfile
change permissions

Format the file to use it as swap memory. This changes its status, allowing the system to use it for overflow memory. Let’s create a swap:

sudo mkswap /swapfile
Secure Swap File

Run the command below to activate the swap file and check its status.

sudo swapon /swapfile

Permanent Swap File

Configure your swap file in “/etc/fstab” to ensure the system activates it after a reboot. Let’s make it permanent:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
configure fstab file to avoid high cpu usage

Solution 8: Update System Packages

Outdated software can contain bugs that affect performance. Always update your software to ensure system stability.

sudo apt update && sudo apt upgrade
Update System Packages to avoid high cpu usage

Solution 9: Upgrade VPS Resources

If high CPU usage persists even after optimization, consider the following reasons:

  • The system may fail to handle the incoming traffic load.
  • An increased Artificial Intelligence workload may require more CPU cores.
  • Finally, databases may require more RAM.

So, increase your VPS specifications for better long-term results.

Conclusion

You can resolve excessive CPU usage on a Linux VPS by identifying the exact process causing it using the top or htop command, killing or restarting problematic services, optimizing the web server and database, disabling unnecessary background services, scanning for malware, enabling swap space, updating system packages, and improving caching. Upon completing these optimizations, if the load average is still greater than the number of your CPU cores, the most effective long-term strategy is to raise your VPS specifications.

FAQs

What are the reasons for high CPU usage on a Linux VPS?
How can I check which process is using the CPU the most?
How do I check CPU usage on my Linux VPS?
Can malware cause high CPU usage on a VPS?
How can I reduce CPU usage during website traffic spikes?

Related Post

Mastering Process Management with Linux ps au...

In Linux systems, processes are the backbone of system ...

How to Install and Deploy Kubernetes on AlmaL...

Kubernetes, often abbreviated as K8s, is an open-source...

Unlocking the power of Linux Pipe Command

The pipe command symbolized as the bar (“|”) enable...

Understanding and Using the tar Command for F...

Managing and transferring files efficiently is a key pa...

How to Install Visual Studio Code on Linux

Visual Studio Code (VS Code) is a widely used code edit...

How to Install Python on Ubuntu

As a favorite among novice and intermediate developers,...

Leave a Comment