Apache is one of the most widely-used web servers in the world, known for its flexibility and robustness. At times, system administrators may find themselves in a situation where they need to limit bandwidth used by Apache to prevent one website or service from using all available resources. This article will detail step-by-step how to limit bandwidth in Apache.
Prerequisites
- You should have a running Apache web server.
- You must have sudo or root privileges to install and configure modules.
Install Apache Module
To limit bandwidth in Apache, we need to install a module named mod_ratelimit. It is not installed by default with Apache. This module allows you to limit the bandwidth for clients. If you use Apache version 2.4 or higher, this module is already built into the server, and you just need to enable it.
For Ubuntu/Debian
If you’re running Apache on a Ubuntu/Debian system, you can enable mod_ratelimit by using the following commands:
sudo a2enmod ratelimit
sudo systemctl restart apache2
For CentOS/RHEL
For CentOS/RHEL, you may need to manually compile this module. You can compile the mod_ratelimit by following these steps:
- Download the Apache source code from its official website.
- Extract the downloaded file.
- Change your current directory to the extracted folder and compile the mod_ratelimit module:
cd /path/to/extracted_folder/modules/filters/
apxs -i -a -c mod_ratelimit.c
- After compilation, restart the Apache service:
systemctl restart httpd
Now that you have the mod_ratelimit module enabled, you can proceed to limit the bandwidth.
Limiting Bandwidth in Apache
There are two key directives provided by the mod_ratelimit module to limit bandwidth in Apache:
- RLimitRate: This directive allows you to set a limit on the connection speed from the client end. The speed is specified in bytes per second.
- SetOutputFilter RATE_LIMIT: This directive sets a filter on the server’s response speed.
Here is an example of limiting the bandwidth on a specific directory:
1 2 3 4 | <Directory "/var/www/html/mywebsite/files"> SetOutputFilter RATE_LIMIT RLimitRate 50000 </Directory> |
In this example, the bandwidth for files in the directory “/var/www/html/mywebsite/files” has been limited to 50000 bytes/sec (roughly 50KB/sec).
You can also limit the bandwidth on a specific virtual host:
1 2 3 4 5 6 | <VirtualHost *:80> ServerName www.mywebsite.com DocumentRoot /var/www/html/mywebsite SetOutputFilter RATE_LIMIT RLimitRate 100000 </VirtualHost> |
In this example, the bandwidth for the virtual host www.mywebsite.com is limited to 100000 bytes/sec (roughly 100KB/sec).
Once you have added these configurations, remember to restart your Apache server for the changes to take effect:
- For Ubuntu/Debian:
sudo systemctl restart apache2
- For CentOS/RHEL:
systemctl restart httpd
Conclusion
Limiting bandwidth on an Apache server is an effective method for controlling resource usage, ensuring that all hosted websites or services get their fair share of resources. This prevents a single service from monopolizing the entire available bandwidth, which could degrade the performance of other services.
While mod_ratelimit is a simple and effective way to limit bandwidth in Apache, it’s important to remember that its functionality is fairly basic. It doesn’t provide dynamic bandwidth throttling or differentiate between different types of traffic. For more advanced bandwidth management features, you may need to consider other Apache modules or a more advanced web server management system.