Skip to content

How to Enable KeepAlive in Apache

This article will help you to Enable KeepAlive in Apache web server on CentOS/Redhat system to increate pagespeed for you web pages.

Category
Web Servers
Covers
Apache
On this page

When you access a website, your browser sends a request to the webserver to load the page. The web server responds by sending back the requested content, and then the connection between the browser and the server is closed. However, if your browser needs to load additional resources (such as images, stylesheets, or JavaScript files), it has to open a new connection to the server for each resource. This can cause delays in loading the page and increase the load on the server.

To avoid this problem, Apache provides a feature called KeepAlive, which allows the browser to keep the connection open after it receives a response from the server. This means that the browser can make additional requests over the same connection, which can help to reduce the latency of loading resources and improve the performance of your website.

Steps to Enable KeepAlive in Apache

Step 1: Edit Apache Configuration File

To enable KeepAlive in Apache, you need to modify the configuration file for your website. The location of this file depends on your operating system and the installation of Apache, but it is usually located at /etc/httpd/conf/httpd.conf or /etc/apache2/apache2.conf.

  • RHEL-based systems:
    sudo nano /etc/httpd/conf/httpd.conf
    
  • Debian-based systems:
    sudo nano /etc/apache2/apache2.conf
    

Step 2: Update KeepAlive Configuration

Once you have located the configuration file, open it with a text editor and look for the following lines:


# KeepAlive: Controls whether persistent connections (keep-alive) are allowed.
# Persistent connections improve the latency and network efficiency, especially for websites with lots of small resources.
KeepAlive On

# MaxKeepAliveRequests: Limits the number of requests allowed per connection when KeepAlive is on.
# Setting this to a high number can improve performance for clients that are requesting many resources, but it can also consume more server resources.
MaxKeepAliveRequests 100

# KeepAliveTimeout: Number of seconds Apache will wait for a subsequent request before closing the connection.
# If set too high, it can lead to resource wastage, while too low might lead to poor performance due to frequent reconnections.
KeepAliveTimeout 15

By default, KeepAlive is disabled in Apache. To enable it, change the KeepAlive Off line to KeepAlive On. This will allow the browser to keep the connection open after receiving a response from the server.

Step 3: Restart Apache Service

Once you have made these changes, save the configuration file and restart Apache. On most systems, you can do this by running the following command:

  • RHEL-based systems:
    sudo systemctl restart httpd.service
    
  • Debian-based systems:
    sudo systemctl restart apache2.service
    

Conclusion

Enabling KeepAlive in Apache can help to improve the performance of your website by reducing the latency of loading resources and reducing the load on the server. By allowing the browser to keep the connection open after receiving a response from the server, you can avoid the delays and overhead of establishing new connections for each resource. By adjusting the MaxKeepAliveRequests and KeepAliveTimeout directives, you can further optimize the performance of your website based on the needs of your users.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

Leave a Reply

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


Back to top