Open In App

What to do when load on a Linux based web server goes high?

Improve
Improve
Like Article
Like
Save
Share
Report

A high-load server leads us to slow response time, and decreased performance and sometimes the system can also crash. In this article, we will discuss what to do when the load on our Linux-based web server goes high.

What is Server load?

It is important to understand what the server load is before we discuss the solution. Basically, it is the amount of work being done by a server. It is calculated by measuring the number of processes running and the amount of CPU and memory resources in use. In general, a server load of 1.00 indicates that the server is fully loaded and cannot handle any additional requests.

A few things that can cause the high server load are:

  • High traffic
  • inefficient database
  • Misconfigured software
  • Resource-intensive tasks

What to do when the load Goes High?

1) We can use `top` command to monitor the system load in real time. And to sort processed by CPU usage press `Shift + P`, and to sort by the memory usage press `Shist + M`.

2) We can also use HAProxy which is popular to balance load among different servers.

  • Step 1: Download haproxy. `sudo dnf install haproxy`
  • Step 2: Start service. `sudo systemctl start haproxy`
  • Step 3: Enable services. `sudo systemctl enable haproxy`
  • Step 4: Configure the file. `/etc/haproxy/haproxy.cfg`
     

global
  # …
defaults
  mode http
  balance roundrobin
  option httpchk
  option forwardfor
  timeout connect 5s
  timeout client 30s
  timeout server 30s
frontend web
  bind *:80
  default_backend servers
backend servers
  server server1 192.168.1.10:80 check
  server server2 192.168.1.11:80 check
  server server3 192.168.1.12:80 check

This is basically `roundrobin` algorithm which evenly distributes requests between the servers `server1`, `server2` and `server3`. The `httpchk` is used to check whether the servers are healthy or not by sending an HTTP request.

3) We must know about uptime commands. If your website is loading slowly, login into your web server console and run uptime to know the load on your server.

>> uptime

4) If uptime shows high numbers like 100 or above 100, you must do something immediately, otherwise, the load might keep on increasing and the server will go down. 

5) Run the following command to log in as a super user.

>> sudo su

6) Go to the www directory

>> cd /var/www/html

7) If the load is too high (The server is about to die), disable index.php (This will make your page inaccessible for a moment, but will decrease load as there won’t be new requests.

>> chmod 000 index.php

8) Check the top load causing IPs. People often run scripts to download content or get malicious access. The below command (On Centos) tells you the top 1000 visiting IPs.

>> tail -1000 /var/log/httpd/access_log | awk ‘{print $1}’ | sort | uniq -c |sort -n

A similar command for Ubuntu is.

>> tail -1000 /var/log/apache2/access.log | awk ‘{print $1}’ | sort | uniq -c |sort -n

9) create a backup of .htaccess file

>> cp .htaccess back.htaccess

10) Block IP addresses in .htaccess if you find some particular IPs hitting your server

>> vim .htaccess

11) Monitor server load

>> watch -n 2 uptime

12) If you did not find any IP causing problems, you might want to check disk space and free memory. Check available disk space

>> df -h

Check free memory.

>> free -m

13) If none of the above step’s help, then something might be wrong in your server code. Last but not least, check processes running on your server and try to debug it.

>> ps aux

Please make sure that you are not running heavy code (plugins) on every page load request. Do a lot of caching to avoid frequent DB or Network connections on a page request. If nothing helps, then your page might have become popular, and you need to upgrade your hosting plan.


Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads