Limit Apache Bandwidth Per User
Introduction
In some hosting environments, it may be necessary to limit the bandwidth usage of a specific user, domain, file type, or virtual host. Although cPanel provides account-level bandwidth limits, there may be cases where you need to control the bandwidth speed per hour, per day, or per request directly through Apache.
Apache bandwidth limiting can be done using the mod_bw module. This module allows you to control the download/upload speed for users, domains, file extensions, large files, or specific MIME types.
Prerequisites
Before proceeding, make sure you have the following:
- Root access to the server.
- Apache installed and running.
- cPanel/WHM server access, if this is a cPanel-based server.
mod_bwmodule installed and enabled in Apache.- Basic knowledge of Apache virtual host configuration.
- Backup of Apache configuration files before making changes.
Check Whether mod_bw Is Installed
Run the following command to check whether the bandwidth module is enabled in Apache:
/usr/local/apache/bin/apachectl -M | grep mod_bw
If the module is installed and enabled, it should appear in the output.
Method 1: Limit Bandwidth for a Specific cPanel User
In this example, we will limit a user’s bandwidth speed to 250 KB/s.
Step 1: Go to the Apache Userdata Directory
For cPanel servers, it is recommended to use the custom userdata include path instead of directly editing httpd.conf.
Example format:
/usr/local/apache/conf/userdata/std/2/USERNAME/DOMAIN.COM
Example:
/usr/local/apache/conf/userdata/std/2/heman/hemanth.com
Step 2: Create the Directory
If the directory does not exist, create it:
mkdir -p /usr/local/apache/conf/userdata/std/2/heman/hemanth.com
Step 3: Create the Bandwidth Limit Configuration File
Create a custom Apache configuration file:
vi /usr/local/apache/conf/userdata/std/2/heman/hemanth.com/cp_bw_all_limit.conf
Add the following content:
BandWidthModule On BandWidth all 256000
Save and close the file.
This configuration limits the user bandwidth to approximately 250 KB/s.
Step 4: Rebuild Apache Configuration
After adding the custom include file, rebuild the Apache configuration:
/scripts/rebuildhttpdconf
Step 5: Restart Apache
Restart Apache to apply the changes:
/scripts/restartsrv_httpd
Why Use the cPanel Userdata Include Path?
It is not recommended to directly edit the main httpd.conf file because changes may be overwritten during Apache rebuilds, cPanel updates, or EasyApache changes.
Using the custom userdata path ensures that the configuration remains safe and persistent after Apache rebuilds or upgrades.
Method 2: Limit Every User on a Virtual Host to 10 KB/s
Use the following configuration to limit every user on a virtual host to a maximum speed of 10 KB/s:
BandwidthModule On ForceBandWidthModule On Bandwidth all 10240 MinBandwidth all -1 ServerName www.example.com
Method 3: Limit Internal LAN Users
The following configuration limits all internal users to 1000 KB/s, with a minimum bandwidth of 50 KB/s. It also limits files greater than 500 KB to 50 KB/s:
BandwidthModule On ForceBandWidthModule On Bandwidth all 1024000 MinBandwidth all 50000 LargeFileLimit * 500 50000 ServerName www.example.com
Method 4: Limit Specific File Extensions
To limit .avi and .mpg files to 20 KB/s, use the following configuration:
BandwidthModule On ForceBandWidthModule On LargeFileLimit .avi 1 20000 LargeFileLimit .mpg 1 20000 ServerName www.example.com
Method 5: Limit Bandwidth by MIME Type
You can also limit bandwidth based on MIME type using Apache output filters.
Example: Limit text-based content such as HTML and plain text to 5 KB/s:
BandwidthModule On AddOutputFilterByType MOD_BW text/html text/plain Bandwidth all 5000 ServerName www.example.com
Conclusion
Apache bandwidth limiting can be configured using the mod_bw module to control traffic speed for specific users, domains, virtual hosts, file extensions, large files, or MIME types.
For cPanel servers, it is always recommended to place custom Apache configurations inside the userdata include path instead of directly editing httpd.conf. This helps preserve the configuration during Apache rebuilds, EasyApache updates, and cPanel upgrades.
After making any changes, rebuild the Apache configuration and restart Apache to apply the bandwidth limits.
