Installing mod_bandwidth For the Ultimate in Apache Utilization Control
“Mod_bandwidth” is a module for the Apache webserver that enables the setting of server-wide or per connection bandwidth limits, based on the directory, size of files and remote IP/domain. Installing this module involves a number of steps, but it is fairly simple to accomplish.
Reprints and reposting of this article is NOT AUTHORIZED without the express consent of the author. Permission to link to this article from other locations is hereby granted.
SSH into your server as root and perform the following:
mkdir /root/modbw
cd modbw
wget ftp://ftp.cohprog.com/pub/apache/module/1.3.0/mod_bandwidth.c
/usr/local/apache/bin/apxs -c /root/mbw/mod_bandwidth.c -o /usr/local/apache/libexec/mod_bandwidth.so
(The above is all one command)
mkdir /usr/local/apache/apachebw
mkdir /usr/local/apache/apachebw/link
mkdir /usr/local/apache/apachebw/master
chown nobody:nobody -R /usr/local/apache/apachebw
pico /etc/httpd/conf/httpd.conf
Find the line that looks like this:
LoadModule rewrite_module libexec/mod_rewrite.so
And BEFORE that line, add this:
LoadModule bandwidth_module libexec/mod_bandwidth.so
Then locate the line that looks like this:
AddModule mod_env.c
and BEFORE that line, add this:
AddModule mod_bandwidth.c
Then locate the line that looks like this:
# Document types.
And BEFORE that line add this:
BandWidthDataDir “/usr/local/apache/apachebw”
BandWidthModule On
Then to enable it on a virtual host, locate the virtualhost entry for the
domain you wish to limit and just BEFORE the </virtualhost> line, add
this:
BandWidthModule On
BandWidth all 512
The 512 can be replaced with whatever rate you want to limit it to in
bytes per second.
Save the httpd.conf file.
service httpd restart
cd /usr/sbin
wget ftp://ftp.cohprog.com/pub/apache/module/cleanlink.pl
chmod 755 cleanlink.pl
pico cleanlink.pl
Change $LINKDIR as follows:
$LINKDIR=”/usr/local/apache/apachebw/link”;
Save the file.
perl cleanlink.pl
pico /etc/rc.d/rc.local
At the end of that file add:
# Launch Cleanlink for mod_bandwidth
perl /usr/sbin/cleanlink.pl
Save that file.
There is actually a lot more you can do with this module than simply limiting the bandwidth rates. You can limit the number of connections, file sizes and more. I encourage you to read the documentation located here for all the deatils of everything you can sutomize with this module.