How to optimize Apache configuration for high traffic website

optimise Apache configuration for high traffic website and apps

If you are running a website that receives a lot of traffic, it is essential to optimize your Apache web server configuration to ensure maximum performance and availability. In this article, we will provide you with some tips and examples on how to optimize your Apache configuration for high traffic websites.

1. Use the right MPM module

Apache uses Multi-Processing Modules (MPMs) to handle client requests. The MPMs are responsible for creating child processes or threads to handle incoming requests. There are three MPM modules available in Apache: prefork, worker, and event.

The prefork MPM is the oldest and most stable MPM module. It creates one process per connection, which can lead to high resource usage. The worker MPM creates multiple threads per process, which can improve performance and reduce resource usage. The event MPM is similar to the worker MPM, but it adds an asynchronous event notification mechanism to improve performance further.

To optimize your Apache configuration for high traffic, we recommend using the worker or event MPM modules. You can enable these modules by uncommenting the following lines in your Apache configuration file:

LoadModule mpm_worker_module modules/mod_mpm_worker.so

or

LoadModule mpm_event_module modules/mod_mpm_event.so

2. Increase the number of child processes/threads

To handle a high volume of incoming requests, you need to increase the number of child processes or threads that Apache can create. The number of child processes or threads you should create depends on the amount of available memory and CPU resources on your server.

To increase the number of child processes or threads, you need to modify the StartServersMaxClientsMaxSpareServers, and MinSpareServers directives in your Apache configuration file.

StartServers is the number of child processes that are launched when Apache starts. MaxClients is the maximum number of simultaneous connections that Apache can handle. MaxSpareServers is the maximum number of idle child processes that Apache keeps alive. MinSpareServers is the minimum number of idle child processes that Apache keeps alive.

For example, to set the StartServers to 10, the MaxClients to 500, the MaxSpareServers to 200, and the MinSpareServers to 10, you can add the following lines to your Apache configuration file:

StartServers 10
MaxClients 500
MaxSpareServers 200
MinSpareServers 10

You should also consider setting the ServerLimit directive to the same value as MaxClients. This ensures that Apache does not create more child processes than necessary.

Another important parameter that can help increase the number of child processes/threads in Apache is the MaxRequestWorkers directive. This directive specifies the maximum number of simultaneous requests that can be handled by Apache.

By default, the MaxRequestWorkers value is set to 256. However, this value can be increased to allow Apache to handle more requests simultaneously.

To adjust the MaxRequestWorkers value, you can add the following line to your Apache configuration file:

MaxRequestWorkers 500

In the example above, we have set the MaxRequestWorkers value to 500, which means that Apache can handle up to 500 simultaneous requests.

It is important to note that increasing the MaxRequestWorkers value also increases the amount of memory used by Apache. Therefore, you should ensure that your server has enough memory available to handle the additional requests.

3. Enable KeepAlive

The KeepAlive feature allows the client to reuse the same connection to the server for multiple requests, reducing the overhead of creating new connections for each request. Enabling KeepAlive can significantly improve the performance of your website.

To enable KeepAlive, you need to set the KeepAlive directive to On. You should also set the KeepAliveTimeout directive to a value that is appropriate for your website. For example, to set the KeepAlive timeout to 5 seconds, you can add the following lines to your Apache configuration file:

KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100 

4. Enable caching

Caching is the process of storing frequently accessed data in memory or on disk, reducing the number of requests that need to be processed by the server. Enabling caching can significantly improve the performance of your website, especially for static content.

Apache provides several caching modules, such as mod_cache and mod_disk_cache. To enable caching, you need to load the appropriate caching module and configure it in your Apache configuration file. For example, to enable caching of static content using mod_disk_cache, you can add the following lines to your Apache configuration file:

LoadModule cache_module modules/mod_cache.so
LoadModule disk_cache_module modules/mod_disk_cache.so
CacheEnable disk /
CacheRoot /var/cache/apache2/mod_disk_cache
CacheDirLevels 2
CacheDirLength 1

These lines enable the mod_cache and mod_disk_cache modules, and configure caching for the root directory (/) using the disk cache. The CacheRoot directive specifies the directory where cached content will be stored, while CacheDirLevels and CacheDirLength control the structure of the cache directory hierarchy.

5. Use compression

Compression is the process of reducing the size of data sent over the network by removing redundant information. Enabling compression can significantly reduce the amount of data that needs to be transmitted, improving the performance of your website.

Apache provides a compression module called mod_deflate. To enable compression, you need to load the mod_deflate module and configure it in your Apache configuration file. For example, to enable compression for text-based content, you can add the following lines to your Apache configuration file:

LoadModule deflate_module modules/mod_deflate.so
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json

These lines enable the mod_deflate module and specify the types of content that should be compressed.

6. Limit access to resources

Limiting access to resources can help reduce the load on your server and improve the performance of your website. You can limit access to resources by using the Order, Allow, and Deny directives in your Apache configuration file.

For example, to limit access to a directory called admin, you can add the following lines to your Apache configuration file:

<Directory /var/www/html/admin>
    Order deny,allow
    Deny from all
    Allow from 192.168.0.0/24
</Directory>

These lines specify that access to the admin directory should be denied for all clients except those in the 192.168.0.0/24 subnet.

7. Optimize server resources

Apart from optimizing Apache, you can also optimize the server resources to ensure maximum performance and availability of your website.

a) Increase memory limit and set Opcache

The default memory limit of PHP scripts may not be sufficient to handle a high volume of incoming requests. You can increase the memory limit by modifying the memory_limit directive in your PHP configuration file.

In addition, you can also enable Opcache, which is a bytecode cache for PHP scripts. Opcache stores precompiled script bytecode in memory, reducing the overhead of parsing and compiling scripts for every request.

Install OPCache
# For CentOS, RedHat and Alma Linux use this command :
$ sudo yum install php-opcache
# For Ubuntu and Debian use this command :
$ sudo apt-get install php-opcache

Once the installation is complete, restart Apache to apply the changes:

$ sudo systemctl restart apache2

That’s it! OPcache should now be installed and enabled on your Ubuntu system. You can verify that it’s enabled by checking your PHP configuration.

b) Set up a Swap file or Swap partition

If your server is running out of memory, it may start using the hard disk as virtual memory, which can significantly reduce the performance of your website. You can avoid this by setting up a Swap file or Swap partition, which provides additional virtual memory when the physical memory is exhausted.

To set up a Swap file, you can use the following commands:

$ sudo fallocate -l 1G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile

These commands create a 1GB Swap file, set the file permissions, format the file as Swap, and enable Swap.

c) Enable GZIP compression

Enabling GZIP compression can reduce the size of data transmitted over the network, improving the performance of your website. You can enable GZIP compression by loading the deflate module in Apache and configuring it in your Apache configuration file.

To enable GZIP compression, you can use the following commands:

$ sudo a2enmod deflate
$ sudo service apache2 restart

These commands enable the deflate module in Apache and restart the Apache service. Once the deflate module is enabled, Apache automatically compresses content using GZIP when the client supports it.

Conclusion

Optimizing Apache for high traffic is not a one-size-fits-all process. It requires a deep understanding of the workload, careful tuning, and continuous monitoring. The settings mentioned in this article are a good starting point, but you may need to adjust them based on your particular situation and needs.

Remember, while Apache’s optimization is crucial, it’s only one piece of the puzzle. The overall performance also depends on other factors like network infrastructure, database performance, application optimization, and more.

LEAVE A COMMENT