Tuning Apache Web Server for Optimal Performance

In this post, we will look at various methods and configurations that might help you improve the performance of your Apache httpd web server. We will discuss topics like adjusting the server’s maximum number of concurrent connections, enabling compression for faster content delivery, using caching to speed up the delivery of frequently requested resources, and optimizing PHP settings for the server’s resources and the type of PHP applications it will run. By the end of this article, you will have a better understanding of how to fine-tune your Apache web server so that it works at peak performance and can manage even the most heavy traffic.

Tuning Apache Web Server

There are several ways to tune an Apache web server for optimal performance. Here are a the examples and use cases:

1. Tune the Apache MPM (Multi-Processing Module) to match the server’s resources and the type of traffic it receives.

Apache MPM is in responsible for managing the processes and threads that handle incoming server requests. One of the MPM options for Apache is the “event” MPM, which is designed for high-concurrency servers that serve a huge number of connections.

It is critical to match the server’s resources and the type of traffic it receives while establishing the event MPM. Here’s an example of a configuration for a server with 8 cores and 16GB of RAM that receives a high volume of static and dynamic content requests:

<IfModule mpm_event_module>
    StartServers             4
    MinSpareThreads          75
    MaxSpareThreads         250
    ThreadLimit              64
    ThreadsPerChild          25
    MaxRequestWorkers       400
    MaxConnectionsPerChild   0
</IfModule>

In this example, we are starting 4 servers at the beginning, with a minimum of 75 spare threads and a maximum of 250 spare threads. The ThreadLimit is set to 64, and each server will have 25 threads available to handle requests. The MaxRequestWorkers is set to 400, which is the maximum number of requests that can be handled simultaneously by the server. The MaxConnectionsPerChild is set to 0, which means that there is no limit on the number of connections per child process.

This configuration is designed to take advantage of the server’s 8 cores by utilizing a high number of threads, while also allowing for a large number of simultaneous connections.

2. Adjust the server’s maximum number of simultaneous connections and the timeout values for idle connections.

Below are the examples of how you can adjust the server’s maximum number of simultaneous connections and timeout values for idle connections in an Apache configuration:
a. Limit the number of simultaneous connections:

<IfModule mpm_event_module>
    MaxClients 150
    MaxConnectionsPerChild 0
</IfModule>

in this MPM event, we set MaxClients directive to 150. This means that the server will only allow 150 simultaneous connections at any given time. The MaxConnectionsPerChild directive is set to 0, which means that there is no limit on the number of connections per child process.

See also  How to Install Webmin on Oracle Linux 8/CentOS 8/Redhat 8

b. Adjust the timeout values for idle connections:

Timeout 60
KeepAliveTimeout 30

Above example, we configure the Timeout directive to 60 seconds. This means that if a connection is idle for more than 60 seconds, the server will close the connection. The KeepAliveTimeout directive is set to 30 seconds, which means that the server will close an idle connection after 30 seconds if the KeepAlive option is enabled. This helps to release resources and free up connections for other clients. There are many directives available in Apache that can be used to adjust the server’s maximum number of simultaneous connections and timeout values for idle connections. Other directives that can be used to achieve the same goal include:

  • MaxRequestWorkers
  • ServerLimit
  • KeepAlive
  • MinSpareServers
  • MaxSpareServers
  • ListenBacklog
  • Timeout
  • KeepAliveTimeout

You can choose the right configuration based on your use case and the resources available on your server.

3. Enable mod_deflate to compress content before sending it to the client.

Example of how you can enable mod_deflate to compress content before sending it to the client in an Apache configuration:

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0[678] no-gzip
  BrowserMatch bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

In this example, we are using the mod_deflate module to compress various types of content, including HTML, CSS, JavaScript, text, XML and fonts. The AddOutputFilterByType directive is used to specify the types of content to compress, and the DEFLATE filter is used to compress the content. The browser match and header append lines are used to remove browser bugs which some old browsers have, this ensures that the compressed content is served correctly to all clients.

See also  What PHP Is Used For: A Comprehensive Guide

4. Use mod_expires to set expiration times for static resources.

An example Apache configuration to use mod_expires to set expiration times for static resources as below :

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css "access plus 1 week"
  ExpiresByType text/javascript "access plus 1 week"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/ico "access plus 1 month"
</IfModule>

This configuration tells Apache to enable mod expires and specifies different expiration times for various sorts of static resources. CSS and JavaScript files, for example, will expire after one week, whereas picture files will expire after one month. This can assist reduce the amount of bandwidth and server resources consumed by your site while also improving its performance for returning visitors.

5. Configure mod_cache to cache frequently requested resources in memory.

Here is an example configuration for mod_cache to cache frequently requested resources in memory using the “mem_cache” storage type:

LoadModule cache_module modules/mod_cache.so
LoadModule cache_socache_module modules/mod_cache_socache.so

<IfModule mod_cache.c>
   CacheEnable mem /
   CacheSocacheMemcache "localhost:11211"
</IfModule>

In this example, mod_cache is being enabled for the entire website using the “mem” storage type and the “CacheSocacheMemcache” directive is being used to specify the location of the memcached server (localhost on port 11211). This is one way to configure mod_cache, but it is important to note that the configuration will depend on the specific use case and resources of the server.

6. Optimize the PHP settings for the server’s resources and the type of PHP applications it will be running.

Here’s an example of how to optimize PHP settings for a server’s resources and the types of PHP apps that will run on it:

  • Increase the memory limit by editing the “memory_limit” setting in the “php.ini” file. For example, to increase the limit to 256MB, change the line to “memory_limit = 256M”
  • Adjust the maximum execution time for PHP scripts by editing the “max_execution_time” setting in the “php.ini” file. For example, to set the maximum execution time to 30 seconds, change the line to “max_execution_time = 30”
  • Increase the maximum file upload size by editing the “upload_max_filesize” setting in the “php.ini” file. For example, to increase the maximum file upload size to 50MB, change the line to “upload_max_filesize = 50M”
  • Enable the OpCache extension to improve PHP performance by adding “zend_extension=opcache.so” in the “php.ini” file and setting “opcache.enable=1”
  • Set the session save path to a directory outside of the web root by editing the “session.save_path” setting in the “php.ini” file. For example, “session.save_path = /var/lib/php/sessions”
  • Adjust the log level of errors by editing the “error_reporting” setting in the “php.ini” file. For example, to show all errors except notices and warnings, change the line to “error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING”
See also  Configuring PHP-FPM for High Traffic Websites: Scaling Strategies

It is important to understand that these settings may differ based on the specific requirements of your PHP applications and the server resources available.

Conclusion

Tuning your Apache web server can significantly boost your website’s performance and scalability. You can improve the server’s resources and handle more traffic by tweaking the MPM settings, connection timeout values, and enabling modules such as mod deflate, mod expires, and mod cache. Furthermore, optimizing PHP settings might enhance the performance of your PHP apps. Always test your changes and monitor the server’s performance to ensure that they produce the desired effect. Your website can handle more traffic, load pages faster, and provide a better user experience for your visitors if it is configured properly.

Leave a Comment