Skip to main content

Improve performance of your website and limit bandwidth used

I received recently a question from Armen, a reader on a shared hosting, asking how to limit the bandwidth used of a website.

In my old Hosting Manual from A to Z, I had described some of the steps before but never went much further. So here I am today.

You are looking to speed up your website? Or limit the download speed of one of your web gallery? Here are few apache modules to do so.

I’m assuming you have either access to your virtualhost, or to the .htaccess of your website.

Gzip Compression

One of the traditional way to improve the performance is to compressed files to reduce the HTTP response time. We usually use Gzip compression to zips HTML documents, scripts, etc… Basically anything not too CPU intensive and that will improve the loading speed. (So no need to compress PDF, zip files, etc…).

At the same time than improving the speed to display your pages, it will reduce the server bandwidth. The trade off is a little more CPU load (But usually, it is negligible)
There are nowadays 2 popular modules to do it:
The very powerful mod_pagespeed, developed not too long ago by Google, that applies  performance best practices to your website. However not all Shared hosting got this module enabled (Better to check with them)
– If you have your own server (let’s say Debian/Ubuntu 64b), you simply need to install their .deb.
In root, run:

now remove the .deb and restart the Apache service

When installed (or if already enabled by your share hosting provider), simply copy/paste the following in your .htaccess (or virtualhost)

That will activate the default Rewrite level (CoreFilters) that includes a set of filters that Google team believes are safe for production server.There is actually the following list of filters:

If you want to add more, you can then simply add

Some websites recommend to add:

If you want to know more about mod_pagespeed, DigitalOcean has a very good tutorial.

– If you are on a shared hosting, not sure your provider support it. Better to check with them….if not, you can still use the mod_deflate.
In your .htaccess, add:

Browser Caching

2 modules are usually used to force the visitor’s browser to cache the website and are usually enabled in most of the shared hosting. It’s the modules expires and headers.
I’m more interested in the mod expires, so I will only talk about this one, that is good enough for me.

This will significantly improve the experience of your visitors (Except if it is one time shot of course), however note that caching is not for website with very dynamic content, but for most of the blogs, galleries, etc… it doesn’t change much.If you have your own server, you will need to enable it first.

In root, simply run:

And restart Apache

Now, configure your virtualhost or .htaccess as the following:

This will cache all the content of your website for 30 seconds.
But most probably you want to better tweak this as some pictures, pdf, etc…does not need to be retrieved for a long time or directly having a much longer cache if you don’t plan to change your website frequently.

For example, instead you could have:

A long cache period, if you don’t plan to make much dynamic change to your website.Make sure when you make any modification, to restart Apache.

Limit the bandwidth

Let’s say now, you have a web gallery, like based on Piwigo or Lychee and you want to limit the bandwidth that your visitors are using.

– You will need to have the mod bandwidth activated. Probably already enabled. If not, simply run:

However, this mod seems not to be working with .htaccess… for shared hosting, it won’t be able to do it that way, you will need the mod_ratelimit instead. (Less advanced tho)

For those with dedicated server or VPS, in your virtualhost, add the following:

This will force the Bandwidth control, set no limitation is the traffic is coming from (On your local network for example), will fix a limit of 100kb/s for all type of content and a limit of 50kb/s for files larger than 8mb.

If you want to better customize those values, you can also split per filetype like this example:

all your .avi files >1kb will have a limit of 30kb/s while your MP3 >1mb will have a limit of 10kb/s.

And for those with only access to .htaccess, you will need the mod_ratelimit and in our .htaccess, add:

This will limit the download speed to 100kb/s on the content located under /downloads

– If you want to limit the maximum concurrent connections per IP (Like to block the download accelerator type of program, or avoid someone is using all your bandwidth for himself), you can simply add:

Once again, after your modification, restart Apache.

If you are looking for something more advanced with quota, maximum of connection per seconds, etc… I recommend you to check the mod_cband.

Hope all these will help you speed up your website and save some of your bandwidth.
If you know better way yet still easy to tweak Apache to improve the performance, please share them in the comments!

Add a password to your website or web directory

In my cases, you may want to add a password to one of your website or web folder.

For example in my case, I have a website where I can simply download some files I’ve downloaded with my Deluge web-ui. This is done with Apache, where I simply have a virtualhost to link to a folder with the Option +Indexes to display all the files.

But obviously I don’t want anyone else to access to this folder. Hence I’ve set up a simple username/password with the Apache htpasswd option.It can works with pretty anything, that does not includes per default a login system, like Yify-pop for the moment.

Here is how to do it.

1) Create a dedicated directory to store the password file

In Root, run

2) Create a password file with users

Still in root:

And you will need to enter a password

If you have multiple users, simply redo the command with user2, then user3, etc…

3) Modify your virtualhost

Now, you will need to edit (Or create) your virtualhost to add the authentication.

Inside the Directory path values


Save and reload apache

Now, everytime you navigate to the website linked to the modified virtualhost, Apache will ask you your username and password.

How to redirect a port to a sub-domain – ProxyPass

Very often, you need to connect to a service through it’s dedicated port, for example, Subsonic, will be 4040 (http://mywebsite.tld:4040), Deluge torrent will be 8112 or could be changed to 9092, ….

But it could be complicated to remember all theses ports and if you are behind a proxy that block all the connection except HTTP and HTTPS (80 and 443), you actually may want to set up a subdomain to access your services. In my case, to access my Deluge torrent WebUI, I will prefer to go to without the need to enter the port number.

This could be easily achieve on your LAMP server by tweaking your virtualhost with the variables ProxyPass and ProxyPassReverse.


You will need to install the following packages in root:

And you will also need to activate the mod-proxy-http (Already installed)


Virtualhost configuration

Here is my full Virtualhost for Deluge that you could reuse:

(You need to keep the exact syntax. If for example you forget the / after http://localhost:9092, you will come up with a blank page when you will try to access to the subdomain)

The “/” of ProxyPass and ProxyPassReverse tells Apache that you want to reroute all the connection from the root folder of your Vhost to
You could aslo reroute the website to another folder for example.

Restart your apache server (in root)

And your good to go!

Before the Internet

How to set PHP.ini value per site – 2MB max file size value

If you have installed some services using PHP where you can upload files (Like Pydio, WordPress, Roundcube, PHPMyAdmin, etc…) you have per default a maximum upload file size of 2 megabytes that could be more than annoying.

This value can be changed directly in your php.ini file but will apply to all PHP services…

If you want to better tweak this per service, like 10MB for Roundcube, 20MB for PHPMyadmin and 1GB for Pydio, I’d suggest you to configure this value directly in the Virtualhost of each of your services on your LAMP server.

You just need to add:

in your virtualhosts (Vhost) after the first <VirtualHost *> and restart/reload apache:

And voila !

Quite easy that way I believe.


Bandwidth restriction – How to limit the download speed for your visitors a terrible thing when you haven’t a very high speed upload rate on your personal server at home or VPS or even cheap dedicated server, and that 1 or few users are saturating your bandwidth (By genuinely “consuming” your content such as photos, videos, or file to share, … or even by some script kiddies downloading in loop one of your picture from your blog or so).


The best thing is obviously….to have a good upload 🙂 but as you may not be able to decide that, the next good idea is to set a bandwidth speed limit.


The Mod_bw module of Apache can help you to set restriction per Virtualhost (Different websites or services), per IP, per type of file and even per file’s size.



To install the mod_bw module, just type:

and enable it if not done automatically at the end of the installation



You can directly write your configuration inside the vhost of your choice.

If for example you want to set a limit per visitors, type of file, and size of file for your website “myblog” but without restriction on any request from your local network, edit the related virtualhost:

and add the following at the end, before the </Virtualhost>:

Obvisouly you can set as you wish. Me I used to filter only 2 levels, Bandwidth All and LargeFileLimit *

Save your file (CTRL+X, then Yes) and restart apache