Enable Keep Alive On Apache Server

Page content

How to enable keep alive header on Apache Server (Ubuntu 22.04 or Centos7)

The Apache server powers the largest number of websites online. It is an open source web server and popular because of its modular structure. There are several modules and other features in Apache that can be used to improve server performance and speed.

The Keep Alive header, a general type header, is used to hint at how the header may be used to set a timeout and maximum number of requests.

This header can also be used to allow a single http connection to stay open for multiple requests or responses. BY default after each request the http connection is closed. However, Keep alive keeps it open and helps improve performance and speed.

The Keep Alive header is also known as HTTP persistent connection. If you are using the Apache server for your website, it is recommended to enable keep alive for improved performance and speed.

In this post, we will show you how to enable HTTP Keep Alive on your server in Ubuntu or Centos 7.

Keep Alive is not enabled by default on the Apache server and therefore, you will need to change the server configuration to enable Keep Alive.

Enabling Keep ALive will help you save a lot of time when a user connects to your website and will improve performance. Servers create a new TCP connection for each request resulting in the need for authentication and several handoffs with each request. With every page request there are multiple more requests associated and with several concurrent users, it can become a time taking process. You can enable the Keep Alive header which will create one TCP connection at the first request and then use it to serve all future requests from the same user and save you a lot of time.

If you have SSH access to your server, you can directly edit the apache configuration file and make changes to enable Keep Alive. Otherwise, you can use the .htaccess file to enable keep alive.

Enable Keep Alive in Apache Server Configuration File

You can edit the apache2.conf file, if you are using Ubuntu and httpd.conf if using Centos 7.

In the case of Ubuntu 22.04, open the Apache configuration file:

sudo nano /etc/apache2/apache2.conf

If you are using Centos 7,

sudo nano /etc/httpd/conf/httpd.conf

Scroll down and then make the changes:

#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15

You can set MaxKeepAliveRequests to 0 for unlimited but it is not recommended.

Enable Keep Alive using .htaccess

You can also enable the Keep Alive header using the .htaccess file. However, you will need to create a .htaccess file if you do not have one inside your root folder. Edit the .htaccess file and then add the following:

<IfModule mod_headers.c>
    Header set Connection keep-alive
</IfModule>

Now, save and close the file. There are several online tools for checking headers that can be used to verify if the Keep Alive header is enabled on your server.

Suggested Reading

Enable mod_expires on Apache Server Ubuntu 22.04

Enable mod_headers on Apache Server Ubuntu 22.04

Install Apache Server on Ubuntu 22.04 LTS

Enable .htaccess file on Apache Server

Disable Directory Listing Apache Ubuntu 22.04