How to Increase Max Connections in Apache Server

Apache is a powerful web server used by millions of high traffic websites and blogs. Though it supports a large number of users, your users may sometimes get ‘max connection exceeded’ message. In such cases, you need to increase max connections in Apache server. In this article, we will learn how to increase max connections in Apache server.

Why Increase Max Connections

First of all let us look at some of the common reasons to increase max connections in Apache.

1. Default max connection – By default, Apache server supports a maximum of 256 concurrent users. If there are more requests hitting your website/blog, then Apache will return ‘max connections exceeded’ error message. In this case, you have no choice but to increase this limit.

2. High Traffic Sites – As your website grows, more users will hit your site simultaneously. This will increase the number of concurrent requests. By increasing max connections supported by your server, you can handle increased server load, and also make your site run faster. Otherwise, users will need to wait longer to see server response, and may even get ‘request timed out’ error messages.

3. Resource Consumption – More concurrent users means more RAM may be held up to process waiting requests. By increasing this limit, you can quickly process more requests, thereby freeing up RAM and CPU. These available resources can be used to process other services and tasks.

4. Cost – As your website traffic grows, your CPU and RAM consumption will increase. Often, this tempts system administrators to upgrade server CPU and memory, thereby increasing running costs. By increasing max connections, you can process more requests using same resources, delaying or even avoiding the need to upgrade your server CPU and RAM.

Choose the Right MPM

What is an MPM

MPM (Multi-Processing Module) is a core Apache module that determines how Apache server handles incoming traffic – using processes, or using threads. By choosing the right kind of MPM, you can easily enhance your server’s ability to process more requests with lesser resources.

Types of MPM

There are 3 main types of MPM – prefork, worker and event.

The prefork model is an old model where each process handles only 1 request at a time. It is non-threaded and resource intensive compared to other MPMs but more stable.

The worker model is a hybrid model that uses multiple threads and multiple processes. In fact, it can handle multiple requests using a single process, using multiple child threads in each process, and using one thread to handle one request. It gives better performance than prefork MPM.

Lastly, the even MPM gives best performance and is used for high traffic sites. Like, worker MPM it is also a hybrid MPM. But it separates connection handling from request processing, using dedicated threads.

By using the right MPM model, you can easily improve your website performance.

How to Increase Max Connections in Apache Server

Now let us look at the steps to increase max concurrent connections in Apache server.

1. Determine MPM

First, you need to determine the MPM system used by your Apache server. You can do this by running the following command.

apachectl -M | grep mpm
OR
httpd -V | grep MPM

2. Open MPM Config File

Depending on the type of MPM used by your Apache server, you need to open its configuration file in text editor.

vi /etc/httpd/conf.modules.d/00-mpm.conf (CentOS/RHEL)
OR
vi /etc/apache2/mods-available/ (Debian/Ubuntu).

3. Configure MPM parameters

Every MPM system depends on select parameter values. To increase concurrent connections, you need to increase MaxClients value for prefork systems and MaxRequestWorkers in worker or event systems. Here is an example of configuration for prefork systems.

<IfModule prefork.c>
StartServers 1
MinSpareServers 1
MaxSpareServers 3
MaxClients 256 # Increase this value
MaxRequestsPerChild 0
</IfModule>

Here is an example for worker or event configurations.

<IfModule worker.c> # or event.c
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 1000 # Increase this value
ServerLimit 40
MaxConnectionsPerChild 0
</IfModule>

Let us understand these parameters in detail.

ServerLimit – Sets the maximum number of apache processes that can be running.
StartServers – Number of processes that begin initially when starting the apache daemon.
MinSpareThreads/MaxSpareThreads – Regulates number of idle threads allowed to exist without being killed. You can only set the starting values of these parameters. They are controlled by Apache server over time.
MinSpareServers/MaxSpareServers – Regulates number of idle servers allowed to exist without being killed.
ThreadsPerChild – Number of threads allowed per process
ThreadLimit – Max number of threads allowed. ThreadsPerChild can have a maximum value equal to this parameter. So if you increase ThreadsPerChild by a lot, then you may need to increase the value of ThreadLimit also.
MaxRequestWorkers – Sets number of concurrent connections supported by your Apache server. You can use this parameter along with ThreadsPerChild to get server limit. Even in this case, it is only a theoretical limit since some resources will always be reserved during running of server.
MaxConnectionsPerChild – Sets the number of connections that a process can handle during its lifetime before being killed. If you set it to 0, then the lifetime will be infinite.

4. Restart Apache Server

Restart Apache server to apply changes.

sudo systemctl restart apache2 # Debian/Ubuntu
OR
sudo systemctl restart httpd # CentOS/RHEL

Points to Remember

There are a few things to remember while increasing the MPM worker processes.

1. If you increase the number of MaxClients or MaxRequestWorkers then it will take up more CPU and RAM.

2. Keep an eye on server activity to see if it is able to handle increased load.

3. Avoid increasing these values by a lot. Otherwise, they will overwhelm your system. It is better to gradually increase them in small increments.

4. If you are keeping too many open connections on your server, then you may need to modify your system’s file descriptors to handle these open files.

5. You may also use caching to reduce higher server load caused due to more concurrent connections.

Conclusion

In this article, we have learnt how to increase maximum concurrent connections in Apache server. Even if you do not get the ‘max connection exceeded’ message on your website, it is always good to optimize your server to perform more using less RAM, thereby reducing server resources and costs. You can customize the above solution as per your requirement.

Also read:
How to Enable GZIP Compression in Apache Server
How to Upgrade Apache Server in CentOS/Redhat
How to Block Image Hotlinking in Apache Server

Leave a Reply

Your email address will not be published. Required fields are marked *