How to Configure KeepAlive Settings for Apache running on an Ubuntu 22.04 VPS

Apache KeepAlive directive is used to control how connections are made on your web server. When a user’s visit your website, they establish many different connections in one session. This can slow down the response of your server when SSL is enabled.

To circumvent the problem, Apache has a very powerful feature called KeepAlive, which allows your website to serve different files without re-establishing the connection. This is one of the most useful features you can use to optimize the speed of your website.

In this guide, we will show you how you can fine-tune the KeepAlive settings from your Apache web server.

 

Prerequisite

  • VPS plan running Ubuntu 22.04 server
  • A non-root user with sudo privilege
  • Apache web server
Note
Special note: If you haven’t yet purchased a hosting plan, we recommend that you consult with HostAdvice’s expert and user reviews before doing so. Go to HostAdvice’s  best VPS hosting or best Linux Hosting to find the best rated web hosts in these categories.

 

Step 1: Make a copy of the default Apache Configuration file

You can find the Apache configuration file under the path ‘/etc/apache2/apache2.conf’ directory. Before we edit the file, we need to make a backup copy with the command below:

$ sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bk

How to Configure KeepAlive Settings for Apache running on an Ubuntu 22.04 VPS

 

Step 2: Edit the Configuration file

We can now open the default configuration file for editing using a nano editor by typing:

$ sudo nano /etc/apache2/apache2.conf

 

Step 3: Changing the KeepAlive directives

Scroll down on the page until you see the below directives:KeepAlive On: When set to ‘On’ Apache will allow persistent connections. This means more than one connection will be allowed per request. You should set this value to ‘On’ if it was set to ‘Off’ to activate KeepAlive on your server.

MaxKeepAliveRequests: This represents the maximum number of connections that should be allowed on your web server when KeepAlive is enabled.

You can set the value to ‘0’ for unlimited connections but this is not recommended. The default value is ‘100’ and this can work for most websites but you can keep this number high depending on the number of users visiting your website.

KeepAliveTimeout: This directive represents the number of seconds to wait for another request from the same client using the same connection. The default value is ‘5’ seconds.

Setting a high value on this directive may lead to a lot of idle connections and can degrade the performance of your server. So only adjust this value when users experience a lot of aborted connections when browsers try to establish connections to closed sessions.

 

How to Configure KeepAlive Settings for Apache running on an Ubuntu 22.04 VPS

 

Remember to press ‘CTRL + O’, and Enter to save the changes. Press ‘Ctrl + X’ to exit fron this screen. Then, you need to restart Apache for the changes to take effect using the command below:

$ sudo systemctl restart apache2
$ sudo systemctl status apache2

 

How to Configure KeepAlive Settings for Apache running on an Ubuntu 22.04 VPS

 

Benefits of KeepAlive

Supporting multiple TCP requests from the same connection optimizes your website load times. This is a big plus for your visitors and search engines. Your server does not have to repeatedly close and open files

Another great advantage is improved memory usage on your server. Remember, https requests are resource intensive and if left to function by default, they can greatly affect your VPS server performance.

 

Conclusion

That’s all when it comes to tweaking KeepAlive settings on your Apache web server. Remember, all modern web browsers will request KeepAlive connections and you should make sure that your server supports this.

In addition, most web users dislike slow loading websites and enabling KeepAlive will mean more hits and this may translate to more business opportunities directly from your website.

How to Change Common PHP Settings on an Ubuntu 18.04 VPS or Dedicated Server

This article shows you how to change PHP settings on your Linux Ubuntu 18.04 vir
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
4 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Host Multiple Websites on an Ubuntu 18.04 VPS or Dedicated Server

This article will show you how to host two or more websites on a single Virtual
5 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install Apache Cassandra on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial will help you install and configure Apache Cassandra (an opensourc
3 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.