Apache is a popular open-source web server that is used by millions of websites around the world. However, like all web servers, it can benefit from some fine tuning to improve its performance.
In this article, we will discuss five important aspects to help you fine tune your Apache web server for high traffic workloads as efficiently as possible. This is a complicated topic so take your time digesting it and use our table of contents below to jump to the sections that most interests you.
#1 Keep Apache Updated
There are several important features that coincide with Apache software updates. They will not only help ensure the security of your websites, but can also improve performance through bug fixes, code optimization, and new features. Here are some of the benefits of using the latest version of Apache:
Improved Performance
Newer versions of Apache often include performance improvements that can make your web server faster. For example, Apache 2.4 introduced the Event MPM, which can significantly improve the performance of high-traffic websites.
Bug Fixes
Apache is a complex piece of software, and new bugs are often discovered. Using the latest version of Apache helps to ensure that you are running a stable and bug-free web server.
Security Patches
Apache is also a popular target for attackers, so it is important to keep your web server up to date with the latest security updates. These updates can help to protect your website from known security vulnerabilities.
If you are running an older version of Apache, upgrading to the latest version as soon as possible to help improve the performance, stability, and security of your websites.
#2 Choose The Right MPM Module
An MPM module is a modular approach to web server functions in the Apache HTTP Server. It determines how Apache binds to network ports, accepts HTTP requests, and dispatches child processes to handle the HTTP requests. There are three basic MPM options built-in to Apache that we will discuss in this article: Prefork, Worker, and Event.
MPM Prefork
Prefork is the default MPM module in Apache. It is a simple and reliable module that is well-suited for small to medium-sized websites. However, its single-threaded design makes it inefficient for high-traffic websites. Prefork should only be utilized in scenarios where website code is incompatible with more efficient threaded MPMs like Event or Worker.
MPM Worker
Worker is a more sophisticated MPM module that uses threads to handle requests. This makes it more efficient for high-traffic websites, but it can also be more complex to configure. MPM Worker shines particularly bright when the majority of traffic handled by Apache is encrypted by the HTTPS protocol.
Note: Prior to Apache 2.4, Worker out performs MPM Event.
MPM Event
Event is the most recent MPM module in Apache. It is designed for high-performance websites that require low latency. However, it can be more difficult to configure than the other two MPM modules.
Note: Prior to Apache 2.4, bugs in MPM Event code hindered its performance significantly.
The Best MPM
The MPM module that you select will determine how Apache handles requests and how it scales to handle more traffic. If you are not sure which MPM module to choose, our everyday heroes recommend starting off with Prefork as the default. It is the simplest and most compatible module, and it is well-suited for most websites. If you find that your website is not performing well under heavy traffic, you can then move into using Worker or Event. Here are some additional factors to consider when choosing an MPM module:
- The type of website you are running. For example, a static website with few dynamic requests will not need as much processing power as a dynamic website with a lot of user interaction.
- The amount of traffic your website receives. If you expect a lot of traffic, you will need to choose and configure an MPM module that can handle threads like Worker or Event.
- The operating system you are using. Some MPM modules are only available for certain operating systems.
#3 Important Apache Directives
Nothing is more impactful to your Apache server performance than correctly optimized directives. Apache directives is just a fancy term for settings. These settings are critical to handling website performance. Our everyday heroes recommend properly tuning the following crucial Apache directives:
TimeOut
An often overlooked but highly impactful setting is the Apache TimeOut directive. This directive is important because it determines how long Apache will wait for a client to send a request before it gives up and terminates the connection.
The default value is 60 seconds, but this can be adjust up or down to meet the needs of your website(s). Additionally, the default value is often not suitable for best performance which is dictated by the type of content each website is serving up to users.
A longer TimeOut value will allow clients with slow connections to complete their requests, but it can also lead to increased server load. A shorter timeout value will reduce server load, but it can also cause clients with slow connections to time out.
The optimal TimeOut value for your website will depend on a number of factors, including the average speed of your clients’ connections, the amount of traffic your website receives, and the resources available on your server. Below are some of the key benefits of using a proper Apache timeout directive:
- Prevents server overload: If a client’s request takes too long to process, it can overload the server and cause other requests to be delayed or even dropped. Setting a TimeOut directive ensures that the server will not wait indefinitely for a client’s request, which can help to prevent server overload.
- Improves performance: A properly configured TimeOut directive can improve the performance of your website by reducing the amount of time that the server spends waiting for slow clients to send their requests. This can free up resources on the server that can be used to process other requests, which can lead to improved performance for all users.
- Protects against DoS attacks: A DoS attack is an attempt to overwhelm a server with requests in order to make it unavailable to legitimate users. By setting a TimeOut directive, you can help to protect your website from DoS attacks by preventing the server from waiting indefinitely for slow or malicious requests.
As with most things in the world, there are always drawbacks which coincide with benefits. Here are some of those drawbacks to consider when using a bad Apache TimeOut value:
- Clients with slow connections may give up: If the timeout value is too short, clients with slow connections may abandon the website before their requests can be processed. This can lead to a poor user experience for these clients.
- Server load may increase: If the TimeOut value is too long, the server may spend a lot of time waiting for slow clients to send their requests. This can increase server load and lead to performance problems for other users.
- Security risks: If the TimeOut value is too long, it may allow malicious clients to send slow requests (SlowLoris Attack) that can overload the server or consume resources.
Overall, the Apache TimeOut directive is an important setting that can have a significant impact on the performance and security of your website(s). By setting a good TimeOut value, you can help to ensure that your website is available to all users and that it is less susceptible to DoS style attacks.
KeepAlive
The Apache KeepAlive directives control how Apache handles persistent connections. Persistent connections allow a client to reuse the same connection for multiple requests, which can improve performance by reducing the number of new connections that need to be created. There are three Apache KeepAlive directives to consider:
- KeepAlive: This directive controls whether or not Apache will allow persistent connections. The default value is “On”, so persistent connections are enabled by default.
- MaxKeepAliveRequests: This directive specifies the maximum number of requests that can be made on a single persistent connection. The default value is 100, but this can be increased or decreased depending on the needs of your website.
- KeepAliveTimeout: This directive specifies the number of seconds that Apache will wait for another request from the same client before closing the connection. The default value is 5 seconds, but this can be increased or decreased depending on the needs of your website.
To optimize your Apache KeepAlive directives, you should consider the following metrics:
- The average speed of your clients’ connections: If your clients have slow connections, you may need to increase the MaxKeepAliveRequests value to allow them to make more requests on a single connection.
- The amount of traffic your website receives: If your website receives a lot of traffic, you may need to increase the KeepAliveTimeout value to prevent the server from closing too many connections.
- The resources available on your server: If your server has limited resources, you may need to decrease the MaxKeepAliveRequests value to reduce the number of connections that are open at any given time.
There is no such thing as a one-size-fits-all Apache configuration. The best we can do from a generic standpoint are the following general guidelines for optimizing the KeepAlive directives:
- KeepAlive: Set this directive to “On” unless you have a specific reason to disable it.
- MaxKeepAliveRequests: Increase this value if your clients have slow connections or if your website receives a lot of traffic.
- KeepAliveTimeout: Increase this value if your server has limited resources or if you want to prevent the server from closing too many connections.
It is important to experiment with different values for these directives to find the optimal settings for your website. You can use the Apache Performance Tuning Guide to help you determine the best values for your specific needs.
MaxRequestWorkers
The MaxRequestWorkers directive in Apache sets the upper limit on the number of simultaneous requests that will be served. It is a critical part of server optimization, and its optimal setting is based on several changing variables. This means its configuration needs to be reevaluated and tailored periodically over time, changed by watching traffic habits and system resource usage. Lets go over some tips on how to optimize the Apache MaxRequestWorkers directive:
- Determine the amount of available memory. The MaxRequestWorkers directive should not be set higher than the amount of available memory on your server. You can use a tool like
to determine the amount of memory that is currently being used by Apache.free
- Monitor your traffic patterns. The number of simultaneous requests that your server receives will vary depending on the time of day, day of the week, and month of the year. You can use a tool like
ApacheBench or Siege
to generate simulated traffic and test different settings for the MaxRequestWorkers directive. - Start with a low value and increase it gradually. It is better to start with a low value for the MaxRequestWorkers directive and increase it gradually as needed. This will help to prevent your server from thrashing and causing performance problems.
- Monitor your system resources. As you increase the value of the MaxRequestWorkers directive, you should monitor your system resources to make sure that they are not being overloaded.
You can use tools liketop
andiostat
to monitor your CPU, memory, and disk usage.
It is important to note that the MaxRequestWorkers directive is just one facet that impacts performance. Other factors, such as the number of CPU cores, the amount of memory, and the type of MPM (Multi-Processing Module) that you are using, will also affect performance.
Performance Tuning Guide
There are many other factors that can affect the performance of Apache, and the Apache documentation covers them in greater detail. Here are some of the others that you may want to consider:
- The type of hardware that your server is running on. The more powerful your server’s hardware, the better it will be able to handle high traffic.
- The number of concurrent connections that your server is handling. If you are expecting a lot of concurrent connections, you will need to increase the number of child processes that Apache spawns.
- The type of content that your website serves. If your website serves a lot of static content, such as images or CSS files, you can improve performance by caching this content.
- The configuration of your web applications. The way that your web applications are configured can also affect the performance of Apache. For example, if your web applications are using a lot of CPU or memory, this can slow down Apache.
To get the most out of your server hardware, we encourage you to read the Apache documentation for more information on these and other facets that affect the performance of Apache. A good starting point for this is the apache.org comprehensive guide:
#4 Enable Caching & Compression
Caching and compression are two incredibly important techniques that can be used to improve the performance of an Apache web server and reduce the overall bandwidth consumption of your website(s). Caching stores frequently accessed files in memory, so that they can be served more quickly to subsequent requests. Compression reduces the size of files by encoding them in a more efficient format, which can also improve performance. While browser caching prevents browsers from downloaded content it has already seen, greatly reducing the number of connection requests needed to fulfil follow-up clicks.
To enable caching and compression in Apache, you need to configure the following optional Apache modules. They each provide directives for handling the various types of caching discussed above.
- mod_cache – This module provides caching functionality for Apache.
- mod_deflate – This module provides compression functionality for Apache.
- mod_expires – This module provided browser-level caching functionality for Apache.
mod_cache
An Apache HTTP Server module that implements an RFC 2616 compliant HTTP content caching filter. It can be used to cache both local and proxied content, and supports a variety of caching policies, including caching based on the Vary header, caching of content negotiated responses, and caching of stale content.
Mod_cache requires the services of one or more storage management modules. The following storage management modules are included in the base Apache distribution:
- mod_cache_disk: Implements a disk based storage manager.
- mod_mem_cache: Implements a memory based storage manager.
Mod_cache can be configured to use a variety of caching policies, including:
- Cache based on the Vary header: caches content that is negotiated by the Vary header.
- Cache content negotiated responses: caches content negotiated by the Accept header.
- Cache stale content: allows caching of stale content for a specified period of time.
Mod_cache can be used to improve the performance of web servers by caching frequently accessed content. It can also be used to reduce the load on backend servers by caching content that is served from those servers.
mod_deflate
An Apache module that provides lossless data compression for HTTP responses. It is based on the Deflate algorithm, which is a combination of the LZ77 algorithm and Huffman coding. mod_deflate can be used to compress HTML, CSS, JavaScript, and other text-based content. This can improve the performance and decrease bandwidth costs of web pages by reducing the amount of data that needs to be transferred over the network.
The compression level of mod_deflate can be configured using the DeflateCompressionLevel directive. The higher the value of this directive, the better the compression, but the more CPU time is required to achieve it. The default value of this directive is 6.
Once mod_deflate is enabled, it will automatically compress any content that is eligible for compression. You can check if mod_deflate is working by visiting your website in a browser and looking for the “Content-Encoding” header in the response. If the response includes the “deflate” encoding, then mod_deflate is working correctly.
Here are some of the benefits of using mod_deflate:
- Improved web page performance: reduces the amount of data that needs to be transferred over the network, which can improve the performance of web pages.
- Increased bandwidth efficiency: helps conserve bandwidth by compressing content that is sent to clients.
- Reduced server load: reduce the load on the server by compressing content before it is sent to clients.
Compression via mod_deflate is a necessity for high performance web pages. Its performant characteristics greatly out-weigh the drawbacks of slightly elevated CPU usage needed by both the web server and the end-user’s browser.
mod_expires
Mod_expires is an Apache module that controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. This allows you to define expiration intervals for different types of content on your web site. For example, you could use mod_expires directives to instruct browsers to cache image files for one hour, JavaScript files for two weeks, and CSS files for two months.
The Expires HTTP header tells the browser how long to cache a file before it should check with the server to see if there is a newer version available. The Cache-Control HTTP header is a more flexible way to control caching, and it can be used to specify other caching directives in addition to the max-age directive.
Mod_expires is a powerful tool that can be used to improve the performance of your web site by reducing the number of requests that need to be made to the server. To learn more about mod_expires, you can refer to the Apache documentation.
Here are some of the benefits of using mod_expires:
- Improved performance: caching static content, can reduce the number of requests required, which can improve performance.
- Significantly Reduced bandwidth: caching static content also reduce bandwidth usage, which can save money on your upkeep costs.
- Improved SEO: search engines, such as Google, take caching into account when ranking websites. Cached content gets rated higher due as a means of speeding up browsing.
Since the most efficient download is one that is already downloaded, proper browser caching is another necessity for high volume websites and mod_expires puts the keys to the kingdom into your hands.
Beyond Apache
The following are some additional caching tools outside of Apache you may also wish to consider. These are not implemented directly in Apache but are just as impactful performantly and so are worth consideration as they can vastly improve the effectiveness of your website(s) caching strategy.
- PHP Opcache – a free and open-source opcode cache for PHP that speeds up PHP execution by storing precompiled script bytecode in shared memory.
- CMS Plugins – depending on your CMS of choice there are many plugins available to take advantage of different caching techniques. Below is a short list of the big ones and links to their plugin directories’ caching plugins.
- Redis (Remote Dictionary Server) – an open-source, in-memory data structure store used as a database, cache, message broker, and streaming engine.
- Memcached – a free, open-source, high-performance memory caching system
- CDN (Content Delivery Network) – a geographically distributed network of servers that delivers static web content to users based on their geographic location.
#5 Continuous Monitoring & Adjustments
Apache performance should always be monitored and periodically adjust any necessary to compensate for changes in content, traffic, and software updates. This ensure that your website can handle high traffic levels, provide a good user experience, and identify potential problems early on.
Some metrics that can be monitored include CPU usage, memory usage, network traffic, and request latency. By monitoring these metrics, you can get a good understanding of how Apache is performing and identify any potential problems. Once you have identified a problem, you can make adjustments to Apache’s configuration to improve performance.
To monitor Apache performance, you can use a variety of tools, including:
mod_status
Mod_status is an Apache module that provides a web-based interface for monitoring the status of your Apache web server. It shows information about the number of requests that have been processed, the number of active connections, the amount of memory that is being used, and other statistics.
ApacheBench / Siege
ApacheBench or Siege are command-line tools that can be used to benchmark the performance of an Apache web server. They can be used to measure the number of requests per second that the server can handle, the average response time, and other metrics.
Webalizer
Webalizer is a web server log analysis tool that can be used to generate reports on website traffic. It can be used to track the number of visitors to your website, the pages that they visit, and the time that they spend on each page.
Monitoring Apache performance and making necessary adjustments is the only assured way to keep your website running as fast and efficiently as possible.
Conclusion
Apache performance tuning is a complex and multifaceted process, but it can be essential for ensuring that your web server is able to handle high traffic levels and provide a good user experience. There are a number of factors that can affect Apache performance, including the MPM module that you use, the settings that you configure, and the hardware that your server is running on. By carefully tuning these factors, you can significantly improve the performance of your Apache web server.
If you are having questions or performance related problems with your website, reach out to one of our Everyday Heroes via chat, phone, or email. We are happy to explore the optimal performance paths available for your specific website and its unique traffic footprint.
Jason Potter is a Senior Linux Systems Administrator & Technical Writer with more than 20 years experience providing technical support to customers and has a passion for writing competent and thorough technical documentation at all skill levels.
Leave a Reply