Apache Mod Deflate tutorial

Support/Development for PHP
nobody
Forum Regular
Forum Regular
Posts: 349
Joined: Sun Mar 29, 2009 6:52 pm

Apache Mod Deflate tutorial

Unread post by nobody »

I noticed that noone has discussed this topic here in the forum.

Many people (both admins and users) try to compress their websites to optimize the use of bandwidth and in order to speed up the loading proccess. Even google page speed online suggests to use compression. Many people try to do this via php but thats not very good. Best way is to do it directly from apache. This applies for anyone running apache.

I have been using it in my server for more than 6 months. It does work.
Just go to google page speed online and check for example a joomla site before and after you setup the mod deflate. I saw up to 25points difference !

First of all make sure that in your httpd.con you have loaded the mod deflate.
There shoudl be this line somewhere. If not add it !

Code: Select all

LoadModule deflate_module modules/mod_deflate.so
Ok

Next we will create the deflate.conf

Code: Select all

vi /etc/httpd/conf.d/deflate.conf

Next paste the following to the conf we have just created and save it.

Code: Select all

<IfModule mod_deflate.c>
#What types must be compressed
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/x-httpd-php
        AddOutputFilterByType DEFLATE application/x-httpd-fastphp
        AddOutputFilterByType DEFLATE application/x-httpd-eruby
        AddOutputFilterByType DEFLATE image/svg+xml
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript

#What it should not compress
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary

#Maximum compression level (even old hardware have no problem with that...)
        DeflateCompressionLevel 9

#Handle browsers with known issues
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

#Logging
        DeflateFilterNote Input instream
        DeflateFilterNote Output outstream
        DeflateFilterNote Ratio ratio
        LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
</IfModule>

Restart apache

Code: Select all

service httpd restart

Done !
Hello IT.
Phone : Blah Blah ....
Have you tried turning it on and off again ?
Phone : Blah Blah ....
....
I'm sorry, are you from the Past ?!
http://www.youtube.com/watch?v=-E4fm4Wqego
User avatar
mikeshinn
Atomicorp Staff - Site Admin
Atomicorp Staff - Site Admin
Posts: 4149
Joined: Thu Feb 07, 2008 7:49 pm
Location: Chantilly, VA

Re: Apache Mod Deflate tutorial

Unread post by mikeshinn »

Thanks for the great tutorial!
scott
Atomicorp Staff - Site Admin
Atomicorp Staff - Site Admin
Posts: 8355
Joined: Wed Dec 31, 1969 8:00 pm
Location: earth
Contact:

Re: Apache Mod Deflate tutorial

Unread post by scott »

Great work, we're trying all this out on the atomic servers right now
nobody
Forum Regular
Forum Regular
Posts: 349
Joined: Sun Mar 29, 2009 6:52 pm

Re: Apache Mod Deflate tutorial

Unread post by nobody »

You are welcome guys. You have helped me a million times. I am happy I was able to help once too :)
Hello IT.
Phone : Blah Blah ....
Have you tried turning it on and off again ?
Phone : Blah Blah ....
....
I'm sorry, are you from the Past ?!
http://www.youtube.com/watch?v=-E4fm4Wqego
User avatar
mikeshinn
Atomicorp Staff - Site Admin
Atomicorp Staff - Site Admin
Posts: 4149
Joined: Thu Feb 07, 2008 7:49 pm
Location: Chantilly, VA

Re: Apache Mod Deflate tutorial

Unread post by mikeshinn »

And if you want to test to see if your server is compressing content, you can use this site to do it:

http://www.whatsmyip.org/http-compression-test/
nobody
Forum Regular
Forum Regular
Posts: 349
Joined: Sun Mar 29, 2009 6:52 pm

Re: Apache Mod Deflate tutorial

Unread post by nobody »

scott wrote:Great work, we're trying all this out on the atomic servers right now
Hey guys.
I didn't see any feedback so I wanted to ask. Did you see good results ? Maybe you did any configuration adjustments or added lines within the deflate.conf ?
Hello IT.
Phone : Blah Blah ....
Have you tried turning it on and off again ?
Phone : Blah Blah ....
....
I'm sorry, are you from the Past ?!
http://www.youtube.com/watch?v=-E4fm4Wqego
faris
Long Time Forum Regular
Long Time Forum Regular
Posts: 2321
Joined: Thu Dec 09, 2004 11:19 am

Re: Apache Mod Deflate tutorial

Unread post by faris »

mod_deflate increases CPU usage -but it is always a good idea to use it even so. I've been using it for years. There are no downsides as such, as long as the config is good (and there are various ways of doing it -- I like the one here, which is similar to mine).
--------------------------------
<advert>
If you want to rent a UK-based VPS that comes with friendly advice and support from a fellow ART fan, please get in touch.
</advert>
User avatar
JnascECSI
Forum Regular
Forum Regular
Posts: 306
Joined: Mon Apr 14, 2008 8:29 am
Location: Rhode Island

Re: Apache Mod Deflate tutorial

Unread post by JnascECSI »

I started using it last month and as mentioned the cpu increase was up but it did make all the sites exspecially the joomla ones more snappy... :D
James Nascimento
Chief Information Officer
East Commerce Solutions, Inc.
22 Morris Lane
East Providence, RI 02914
Ph. 800-527-5395 x263
Fax. 888-999-5891
nobody
Forum Regular
Forum Regular
Posts: 349
Joined: Sun Mar 29, 2009 6:52 pm

Re: Apache Mod Deflate tutorial

Unread post by nobody »

Statistically the cpu goes up but only by an average of 2-3%.
With todays CPU's this increase is close to nothing. But on the other hand sites load faster and i think thats great.
Any suggestions for the conf though guys ? Maybe we can enhance this confguration more and make it even better !
Hello IT.
Phone : Blah Blah ....
Have you tried turning it on and off again ?
Phone : Blah Blah ....
....
I'm sorry, are you from the Past ?!
http://www.youtube.com/watch?v=-E4fm4Wqego
faris
Long Time Forum Regular
Long Time Forum Regular
Posts: 2321
Joined: Thu Dec 09, 2004 11:19 am

Re: Apache Mod Deflate tutorial

Unread post by faris »

As I recall, there's two basic ways to set up mod_deflate:

Compress all by default, then define exceptions
Don't compress all by default, and define what to compress (with exceptions)

There's lots of things you don't want to compress:

Anything above a certain size, possibly (but I don't think this is an option in the config)

Every type of video file, possibly? (e.g. add .mkv in particular, plus possibly .ts, .mpg, .mpeg although some of those might benefit. But do we want to? They are likely to be big, and reducing a 10Mb file to 9Mb isn't worth my CPU cycles, I don't think.

What about Flash - .swf? .flv?

Audio files? .ogg, for example is nicely compressed to start with. But again some of those are compressible but may be big and not worth the bother.


Anyway here's an alternative mod_deflate.conf, which appears to be from the "deflate all, except as defined" school, and comes from the Apache website ( http://httpd.apache.org/docs/2.0/mod/mod_deflate.html ) with a modification to comment out the cludge for 2.0.48 on the assumption that people will be using a later version)

Code: Select all

<Location />
 # Insert filter
 SetOutputFilter DEFLATE

 # Netscape 4.x has some problems...
 BrowserMatch ^Mozilla/4 gzip-only-text/html

 # Netscape 4.06-4.08 have some more problems
 BrowserMatch ^Mozilla/4\.0[678] no-gzip

 # MSIE masquerades as Netscape, but it is fine
 # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

 # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
 # the above regex won't work. You can use the following
 # workaround to get the desired effect:
 # BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

 # Don't compress images
 SetEnvIfNoCase Request_URI \
 \.(?:gif|jpe?g|png)$ no-gzip dont-vary

 # Make sure proxies don't deliver the wrong content
 Header append Vary User-Agent env=!dont-vary
 </Location> 
--------------------------------
<advert>
If you want to rent a UK-based VPS that comes with friendly advice and support from a fellow ART fan, please get in touch.
</advert>
Post Reply