Search This Blog

Tuesday, January 20, 2009

How to Compress Perl/CGI-Scripts with 1&1 (and Other Shared Hosts)

According to Yahoo’s Best Practices for Speeding up Websites, it is a good idea to compress (gzip) all text-based files, including php, css, javascript, and the topic of today: Perl CGI scripts. However, if you are hosting your website on 1&1 (or other shared hosting package*), you probably know of no easy way to g-zip such files.

May 6, 2012 Update: If you have the ability to ssh into your server as root, skip to step 4 below. By far the fastest and cleanest method is to install cpanm (if it's not already), then turn around and use it to install CGI::WebGzip:
cpan App::cpanminus
cpanm CGI::WebGzip
Any Perl module can be installed using cpanm Module::Name: see Cpan for more.

I first tried the method outlined in the article How To Install Perl Modules On Shared Hosting, but I couldn’t get it to work quite like the author suggested. With a little experimentation, however, I found a way to gzip Perl files on a 1&1 Beginner’s Package.

Step 1:
Download a copy of CGI-WebGzip from CSPAN. Unzip it, and extract, the file you need. (If you don’t have the software to unzip tar.gz files, download a copy of the free 7-zip for your operating system, and tell your computer to use the 7-Zip File Manager (7zFM.exe on Windows) to open files with the tar.gz extension. Such files are "zipped twice," so you'll have to unzip the .gz part, then the .tar part before you finally get to your files.)

Step 2:
Open and take out take out the CGI:: on the very first line, changing it from:
package CGI::WebGzip;
package WebGzip;
Step 3:
Upload the file to the same directory where the Perl CGI-scripts you want to compress live.

Now open your Perl file. On the second line under the Perl shebang (#!/usr/local/bin/perl), add the following:
use lib "$ENV{DOCUMENT_ROOT}/path_to_Perl_scripts"
where you change path_to_Perl_scripts to the directory from root in which they live.

For example, if you have your Perl files stored in a directory called Perl inside another directory called scripts, the line would read use lib "$ENV{DOCUMENT_ROOT}/scripts/Perl". This tells the script to use the library located in such-and-such directory from the root.

Step 4:
Somewhere in your Perl script should be the following line or one very similar:
print "Content-type: text/html\n\n";
which tells the script to print the output to the screen as HTML. Directly above that line, add the following line:

(Update continued: cpanm installers need to use
use CGI::WebGzip
instead, the normative way of importing Perl modules. Otherwise...)
use WebGzip;
print "Content-type: text/html\n\n";
This line tells the script to include the module you just uploaded when it outputs its contents.

Step 5:
First check and make sure that your Perl script loads as expected: it is possible that you left a semi-colon off somewhere or got something a bit out of place. Once that is done, test to see that your file is being compressed using either HTTP Compression Test or GIDZipTest.

If compression is still not working, go back to step four and see if there are any other such lines in the script. If you are not using a 1&1 hosting package, you might also look at the asterisk below.*

* Your server will need to have the Compress::Zlib module installed. It is possible, even if it is not installed, you could use a method similar to the one outlined in this document to include it: you can download it here.

If you’re not sure if you have it installed and you’re not sure how to check, try the directions here and see if it works. If it works, you have Compress::Zlib installed.


  1. Perl online training|Perl training|call us+919000444287 ...
    Perl Online Training, Perl Scripting online training by real time Experts from Hyderabad, India. Call 9000444287 for online training demo. Online Perl training ...

  2. DreamHost is definitely the best hosting company with plans for any hosting needs.

  3. It is amazing that people describe these things.