Optimizing Apache Performance – Part 1

Apache is an open-source web server implementation, and it is the most popular web server all over the world. Almost 70% of the web servers on the Internet are using Apache. While we can improve Apache server performance by adding additional hardware like RAM, CPU, etc., we can achieve the same result by customizing Apache configuration as well.

This post explains how to improve Apache performance without adding additional hardware resources to your system.

Note: this guide is done on an Ubuntu 14.04 server 14.04.

MaxKeepAliveRequests limits the number of requests allowed per connection. It is used to control persistent connections. In Ubuntu, the default value of MaxKeepAliveRequests is 100. You can change it to any value you desire. The recommended value of MaxKeepAliveRequests is between 50 and 75.

You can change this value by editing the Apache configuration file.

sudo nano /etc/apache2/apache2.conf

Change the value from 100 to 60.

MaxKeepAliveRequests 60

Save the file and restart Apache.

sudo /etc/init.d/apache2 restart

KeepAliveTimeout defines how long the server waits for the new request from connected clients. Setting KeepAliveTimeout to a high value may cause performance issues in a heavily loaded web server. In Ubuntu, the default value of KeepAliveTimeout is 15. The recommended value of KeepAliveTimeout is between 1 and 5.

You can change this value by editing the Apache configuration file.

sudo nano /etc/apache2/apache2.conf

Change the value from 15 to 3.

KeepAliveTimeout 3

Save the file and restart Apache.

It sets the limit on the number of simultaneous connections that will be served. Every new connection request will be queued up after this limit. Once a process is freed, then the queued connection will be served. In Ubuntu, the default MaxClients value is 250. It is recommended to keep this value at 150.

You can change this value by editing the “mpm_prefork.conf” file.

sudo nano /etc/apache2/mods-available/mpm_prefork.conf

Change the value from 250 to 150.

MaxClients 150

Save the file and restart Apache.

It is used to recycle processes. When this limit is set to 0, an unlimited amount of requests are allowed per process. MaxConnectionsPerChild sets the limit on the number of requests that an individual child process will handle. After it reaches the specified limit, the child process will die. In Ubuntu, the default MaxConnectionsPerChild value is 100.

The recommended values for this setting are:

  • virtualized server 300
  • server with 1-4GB RAM 500
  • server with 4+GB RAM 1000

You can change this value by editing the “mpm_prefork.conf” file.

sudo nano /etc/apache2/mods-available/mpm_prefork.conf

Change the value from 100 to 300.

MaxConnectionsPerChild 300

Save the file and restart Apache.

By default, this setting is set to On in Ubuntu. When the Apache server is getting requests from hundreds and thousands of IPs at once, then this setting should be Off. It is recommended to disable this setting to increase connection throughput.

You can disable this setting by editing the Apache configuration file.

sudo nano /etc/apache2/apache2.conf

Change the value from On to Off.

KeepAlive Off

Save the file and restart Apache.

It sets the desired minimum and maximum number of idle child server processes. It controls how many unused child-processes Apache will keep alive while waiting for more requests to put them to use. Each child-process consumes resources, so if you set the MaxSpareServers value too high, then it can cause resource problems.

The recommended values for MinSpareServers are:

  • virtualized server 5
  • server with 1-2GB RAM 10
  • server with 2-4GB RAM 20
  • server with 4+ GB RAM 25

The recommended values for the MaxSpareServers value should be set as double that of MinSpareServers.

You can change the MinSpareServers value to 5 and the MaxSpareServers value to 10 by editing the “mpm_prefork.conf” file.

sudo nano /etc/apache2/mods-available/mpm_prefork.conf
MinSpareServers  5
MaxSpareServers  10

Save the file and restart Apache.

Configuring Apache for maximum performance is very easy. You can also understand the web server requirements and test with various available options. You will find more tips for optimizing Apache in my next post. Feel free to comment if you have any questions.