Apache KeepAlive directive is used to control how connections are made on your web server. When a users 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.
- A VPS plan running Ubuntu 18.04 server
- A non-root user with sudo privilege
- Apache web server
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
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.
Remember to press CTRL + X, Y and Enter to save the changes. Then, you need to restart Apache for the changes to take effect using the command below:
$ sudo systemctl restart apache2
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.
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 this is supported by your server.
Also, 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.