Apache does not have any rate limiting or bandwidth limitations by default. In fact, only external modules provide this functionality, so you’d have had to make a special effort to enable it. In this post we will see how you can limit bandwidth using mod_bandwidth module and limit the users to 1Mbps per domain.
By default, Apache will use as much bandwidth as it can. mod_bandwidth can be used by hosting companies, which would like to limit the bandwidth for their users.
What is Mod Bandwidth?
“Mod_bandwidth” is a module for the Apache webserver that enable the setting of server-wide or per connection bandwidth limits, based on the directory, size of files and remote IP/domain.
How does it work?
The mod will set a shared memory holding all of the configurations you make. In this space, it will also keep a “count” of the info currently used (as current connections, bw used, time, and bytes sent). When you assign a bw limit, the mod will “split” the data, and will send it piece by piece, with a small delay between pieces. The delay will be adjusted so at least 1 piece is sent in a second, thus evicting browser timeout.
If there are two or more clients downloading from the same vhost, the limit will be “splitted” too. So, if you have a bw limit of 16Kb and two clients, each one will have 8Kb to download. You can also set a fixed download rate, so each client will have a fixed maximum download rate.
How to enable it for a domain?
In the root shell type: /scripts/setbwlimit then follow the instructions (remember you are setting the limit based on bytes per second so 1024 = 1kb sec and that is to all users)
How to install mod_bandwidth
Before you go about the installation procedure, please make a note that the following steps are specific to Apache 1.3.x versions only and are not compatible with the old versions of Apache, including but not limited to 1.3 beta.
- 1. Login to your server via SSH as root.
- 2. Type: mkdir /root/mod_bw
- 3. Type: cd /root/mod_bw
- 4. Type: wget ftp://ftp.cohprog.com/pub/apache/module/1.3.0/mod_bandwidth.c
- 5. Type: /usr/local/apache/bin/apxs -c /root/mod_bw/mod_bandwidth.c -o
- 6. Type: mkdir /usr/local/apache/bw_limit
- 7. Type: mkdir /usr/local/apache/bw_limit/link
- 8. Type: mkdir /usr/local/apache/bw_limit/master
- 9. Type: pico -w /etc/httpd/conf/httpd.conf
- 10. Locate the following Line:
- 11. Before the above line add this:
LoadModule bandwidth_module libexec/mod_bandwidth.so
- 12. Now locate this line: AddModule mod_env.c
- 13. Before the above line add this:
- 14. Now locate this line: # Document types.
- 15. Before the above line add this:
- 16. To enable mod_bandwidth on a virtual host locate the virtual host entry for the specified domain/account you wish to limit. Just before the line add the following:
BandWidth all 1048576
The 1048576 can be replaced with whatever rate you wish to limit the account too. In our case, we are trying to limit it to 1Mbps.
- 17. Save the file and exit. CTRL-X then Y then enter.
- 18. Type: service httpd restart
- 19. Type: cd /usr/sbin
- 20. Type: wget ftp://ftp.cohprog.com/pub/apache/module/cleanlink.pl
- 21. Type: chmod 755 cleanlink.pl
- 22. Type: pico -w cleanlink.pl
- 23. Change $LINKDIR to the following:
- 24. Save the file and exit.CTRL-X then Y then enter.
- 25. Type: perl cleanlink.pl
- 26. Type: pico -w /etc/rc.d/rc.local
- 27. Scroll down to the very end of the file and add the following:
# The following line Launches CleanLink for Mod_Bandwidth
- 28. Save the file and exit. CTRL-X then Y then enter.
You should now have the user limited to 1Mbps bandwidth. Once you are done with the installation, proceed with the below steps:
#/scripts/setbwlimit --domain=domainname.com --limit=1048576 #/scripts/restartsrv_apache #service httpd stop #service httpd start #/usr/local/apache/conf/userdata/std/2/ #ls #cat cp_bw_all_limit.conf
After you execute the above commands, the output should look similar to the one below:
# # BandWidthModule On # BandWidth all 1048576 # # # BandWidthModule On # BandWidth all 1048576 #
In case you need any assistance to configure your Apache modules on your Linux server or if you have any issues with Apache, you can reach our server management expert who can provide expert advice and help you set up things in a timely manner.
Hope you liked it and follow us on Facebook, Twitter, LinkedIn