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 WebGzip.pm, 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 WebGzip.pm and take out take out the CGI:: on the very first line, changing it from:
package CGI::WebGzip;
to:
package WebGzip;
Step 3:
Upload the WebGzip.pm 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.

10 comments:

  1. It is amazing that people describe these things.
    Blanche

    ReplyDelete
  2. Your comment was great it’s really helpful for me a lot,Thank you so much for share my blog.
    Jeans Manufacturers in Delhi

    ReplyDelete
  3. Superb it’s really helpful for me a lot,Thank you for share my blog.
    play school cartoon wall painting

    ReplyDelete
  4. Awesome blog, I got a lot of valuable information by this. Thank you so much for share my blog.
    Lifestyle Magazine

    ReplyDelete
  5. Superb blog comment, thanks for this valuable information.
    Shipping Company in India

    ReplyDelete
  6. Excellent post! I must thank you for this informative read. I hope you will post again soon.
    Website Designing Company In Delhi | Internet Marketing Services In Delhi

    ReplyDelete
  7. This is an amazing blog. Your blog is really good and your blog has always good thank you for information. Online Teaching Software.

    ReplyDelete
  8. One of the best things about these kind of services is that you can even design your own website with many advantages as well. To get more interesting details about webpage scientist, learn here.

    ReplyDelete
  9. Great! Thanks for sharing this kind of information it’s really very nice and useful for me.
    Website Designing Company in South Delhi
    Web Plus Era is a leading Website Designing Company in Dwarka. We offer web development solutions, but also Delhi web services. Mobile apps have been the center of attraction for most of the people. For almost all e-commerce websites and business operators, mobile apps are a wonderful way to make your way to a lot of major customers, clients, and a large market segment. Web Plus Era is the best App Development Company in Delhi. Responsive Website designs are in excessive call for because of their better functionalities and responsiveness to make stop person revel in a memorable one. Companies are making an investment large on responsive net layout offerings. Web Plus Era is among the quickest rising Responsive Web Design Services responsive net designing organizations within side the India,Delhi. Website Designing Company in DelhiVery satisfied with the Website designing work effort by your team! Happy to recommend it to anyone looking for Website Designing service Web Plus Era is best Place for your Digital Work like website,seo,smo etc.

    ReplyDelete