How to enable gzip compression in apache

What is gzip compression? Why do we need to enable it?

Gzip is the file format and method of compression and decompression of files using it’s algorithm to reduce the file size. It is used in web server, where web servers  send data to http client with gzip compression for faster data transfer and low bandwidth consumption.

Enabling gzip compression is the best practice and it is highly recommended, so pages are likely to load faster in browsers.

Enable gzip compression to improve website speed
Enable gzip compression to improve website speed

How to check gzip compression enabled or not

You can check if gzip compression is enabled or not on a particular website using following methods.

You can use the google PageSeep Insights tool not only to check gzip compression but also to analyze performance of a website.

You can also check if gzip compression is  enabled on server or not using command line tool curl on linux. Try out the following,

Replace the URL with what ever the website that you want to check for gzip compression.

Where,

-I   means, only make HEAD request to server to get headers

-H    specify the accepted encoding gzip using header ‘Accept-Encoding’

If you see header Content-Encoding with gzip in response headers, then compression is enabled on server and it’s working

It works in such a way that, client would specify it’s supported compression and encoding using header Accept-Encoding. Server will use compression if it is enabled on server else will send plain text back to client. Server will notify encoding format and compression to client through header Content-Encoding in response headers.

Here is the curl request on google.co.in to verify gzip compression

We can see that, the header Content-Encoding: gzip in response headers. Means gzip compression is enabled.

Enable gzip compression in apache

The gzip compression can be enabled  by directly changing httpd conf file. That is httpd.conf  or you can use .htaccess file to target only specific directory or path or site.

Add the following code to  /etc/httpd/conf/httpd.conf    if apache is installed somewhere add the that specific httpd.conf file

To enable gzip compression via .htaccess, put the following code into .htaccess file which  in the desired site directory.

Restart the apache. That’s it, apache will compress the response which is being sent to client. Enabling gzip compression absolutely would boost your site performance.

You can test if gzip compression is enabled or not using curl as mentioned in this article.

The gzip compression is recommended on all types of text files including following files and extensions

.html
.php
.css
.js
.txt
.json

But, gzip compression is not recommended on graphic files or .zip (in the sense files which are compressed already). Because by compressing these kind of files we hardly save few bytes. Thus increase in loading time because of added unaffected compression task.

Why GZIP compression ?

As explained, gzip compression saves the lot of bandwidth by reducing file size. It saves up to 50% to 80% of bandwidth. So, it reduces download and waiting time of browser for resources. If you enable gzip compression, it won’t affect unsupported browsers, where they can fallback to normal(no compression) data download.

 

Summary

GZIP compression is process of zipping or compressing files on server before they get delivered or transferred via network to browser. Browser will uncompress the data before it uses. As it saves 50% to 80% of bandwidth, if it enabled performance of  website will increase considerably.

Monitor apache webserver realtime using Apachetop

Apachetop is a monitoring tool to monitor  the  performance of apache server and to watch request which are being served live. It’s very likely based on application mytop. It displays current number of reads, writes,  number of requests processed so far and current request being processed. This tool will use apache access_log to  Monitor apache webserver realtime using apachetop

Install apachetop

If you are using CentOS you can install this application by using yum as follows

$ yum install apachetop

On Debian based systems you can install using apt-get as follows,

$ apt-get install apachetop

If you wanna install it from source, download/clone it from github and then compile/install.

Clone Repository

$ git clone https://github.com/tessus/apachetop.git

How to apachetop

Run the application to monitor apache webserver, to do so, type apachetop command. You would see the screen something like below

If you open apachetop. You can see the help by hitting letter h.

You can filter request by URL or referrers or hosts. You can toggle filter by using f when apachetop is running

 

This application by default assumes that the path of access_log file as /var/log/httpd/access_log. If you have a custom installation of apache or running apache on SCL (Software Collections).You can specify the path with option -f. i.e,

$apachetop -f /opt/rh/httpd24/root/etc/httpd/logs/access_log