How to Limit Bandwidth Using mod_bandwidth Module

Limit Bandwidth using mod_bandwidth module

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 configuration you make. In this space, it will also keep a “count” of the info currently using (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 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 is not compatible with the old versions of Apache, including but not limited to 1.3 beta.

  1. 1. Login to your server via SSH as root.
  2. 2. Type: mkdir /root/mod_bw
  3. 3. Type: cd /root/mod_bw
  4. 4. Type: wget
  5. 5. Type: /usr/local/apache/bin/apxs -c /root/mod_bw/mod_bandwidth.c -o
  6. 6. Type: mkdir /usr/local/apache/bw_limit
  7. 7. Type: mkdir /usr/local/apache/bw_limit/link
  8. 8. Type: mkdir /usr/local/apache/bw_limit/master
  9. 9. Type: pico -w /etc/httpd/conf/httpd.conf
  11. 10. Locate the following Line:
    LoadModule rewrite_module
  12. 11. Before the above line add this:
    LoadModule bandwidth_module libexec/
  13. 12. Now locate this line: AddModule mod_env.c
  14. 13. Before the above line add this:
    AddModule mod_bandwidth.c
  15. 14. Now locate this line: # Document types.
  16. 15. Before the above line add this:
    BandWidthDataDir “/usr/local/apache/bw_limit”
    BandWidthModule On
  17. 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:
    BandWidthModule On
    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.
  18. 17. Save the file and exit. CTRL-X then Y then enter.
  19. 18. Type: service httpd restart
  20. 19. Type: cd /usr/sbin
  21. 20. Type: wget
  22. 21. Type: chmod 755
  23. 22. Type: pico -w
  24. 23. Change $LINKDIR to the following:
  25. 24. Save the file and exit.CTRL-X then Y then enter.
  26. 25. Type: perl
  27. 26. Type: pico -w /etc/rc.d/rc.local
  28. 27. Scroll down to the very end of the file and add the following:
    # The following line Launches CleanLink for Mod_Bandwidth
    perl /usr/sbin/
  29. 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 --limit=1048576
#service httpd stop
#service httpd start
#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 the expert advise and help you set up things in a timely manner.

Hope you liked it and follow us for more updates on Facebook Twitter