Apache2 Performance

This article introduces you to the optimization of the most used web server called Apache2. It gives you just an overview about the optimization of the configuration file and shows you how to install useful modules. Of course, there are some other optimizations with Apache2, for instance the code optimization, but this is not in the scope of this article. This article is a part of the web-application performance series.

Hostname lookups

Is HostNameLookups set to on Apache2 does a hostname lookup for every IP. You don’t need this functionality, because it has no impact on the response and it needs a lot of time. So, set this directive to off.

HostNameLookups Off

Keep-alive

To keep alive a HTTP connection means to let the file descriptors open to handle the next request (from the same user) faster. The idea of keep-alive is cool, but it only makes sense if you have a small amount of users. Otherwise you have open file descriptors that need unnecessary resources. So, it would be better to set this mechanism to Off to serve a high number of requests faster. Modify the configuration file:

KeepAlive Off

Adapt workers

The optimal number of workers (multi-thread & multi-process module) is important for a well working webserver. On the one hand you can have to less workers and on the other hand you can have to much workers. Both cases are not desirable. The problem is, that you have to test the server under a realistic load. You can control the behavior of the Apache2 with the following calculation:

# ServerLimit * ThreadsPerChild = MaxClients
ThreadLimit 50
ServerLimit 30
StartServers 5
MaxClients 1500
MinSpareThreads 30
MaxSpareThreads 50
ThreadsPerChild 50

The setting depends on the power of the hardware. If you have a server that have only one task (to respond HTTP requests), you can increase this settings to make the server more powerful.

Apache benchmark

To test your web server configuration/performance you can use ab (Apache Benchmark) which would be delivered with the Apache2 binary (If not, download it afterwards). You can use this benchmarking tool with:

ab -n 1000 -c 100 http://example.com/
  • n: number of total requests
  • c: number of concurrent requests
  • url: url to test

The tool sends out 1000/100 waves with 100 concurrent requests. Note that you can plot the output with gnuplot.

Mod_expires

Apache2 handles the expires header with mod_expires. The expires header is a HTTP header, that tells the browser, how long the transfered file is valid. You can turn on this module with typing:

 a2enmod expires

into the shell. After that you can configure the web server (httpd.conf):

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 2 hours"
ExpiresByType text/xml  "access plus 2 hours"
ExpiresByType image/jpg "access plus 10 weeks"
ExpiresByType image/gif "access plus 10 weeks"
#add all types of tiles that you need
</IfModule>

The webserver then adds the expires header to the files. It automatically calculates the timestamp after your settings.

Mod_headers

This module is used to append headers to the HTTP response. After enabling mod_headers with

a2enmod headers

you can use the functionality with adding the following line to the configuration file:

Header append Cache-Control "public"

Mod_deflate

This module is used to compress the server output. You have to a2enmod this module and after that you can use it by typing this into the configuration file:

SetOutputFiler DEFLATE
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain text/html text/htm
AddOutputFilterByType DEFLATE application/javascript
#add all types that you need
</IfModule>

Additional

In addition to that, Apache2 has some more modules to improve the performance of the server. Below i listed these modules with the link to the documentation:

Leave a Reply

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