18 Oct 2019

[FIXED] Too many open files error on LiteSpeed Web Server

Website is crashing on traffic spike? Website facing 503 Service Unavailable error? Webserver throwing the following errors in the logs.

[ERROR] [*:443] HttpListener::acceptConnection(): Accept failed:Too many open files!

/usr/local/lsws/cachedata/priv/1/a/6/1a68a623c1d9ccbc: open() failed: Too many open files

Such errors occur when open limits on the server are exhausted by the web server and other services. “Too many open files” errors happen when a process needs to open more files than it is allowed by the operating system.

We will increase these limits on the server by following few steps discussed below.

Procedure

  1. Login to the root user of the server.
  2. Edit /etc/security/limits.conf file and append the file using the following lines and save it.
    * soft nproc 327680
    * hard nproc 655350
    * soft nofile 327680
    * hard nofile 655350
    root soft nproc 327680
    root hard nproc 655350
    root soft nofile 327680
    root hard nofile 655350
    @someuser soft nproc 327680
    @someuser hard nproc 655350
    @someuser soft nofile 327680
    @someuser hard nofile 655350
    Note: Here @someuser is the Linux user on which your website is making requests. Generally, it will be the Linux user where your website files are stored.
  3. Similarly, Edit /etc/sysctl.conf file and append the file using the following lines and save it.
    fs.file-max = 655350
    fs.nr_open = 655350
    net.core.netdev_max_backlog = 131070
    net.core.somaxconn = 655350
    net.ipv4.tcp_max_syn_backlog = 3240000
    net.ipv4.tcp_max_tw_buckets = 1440000
    net.ipv4.tcp_window_scaling = 1
    vm.swappiness = 30
  4. Run the following command to apply the changes.
    systctl -p
  5. Create a conf file /etc/systemd/system/lsws.service.d/override.conf , with content to increase limit for process launched by systemd.
    [Service]
    LimitNOFILE=5000000
  6. Reload daemons and lsws service
    systemctl daemon-reload && systemctl restart lsws

You can raise the limits as per your requirement as it depends on the traffic count and how much busy the server is.