Science and technology

How to generate webpages utilizing CGI scripts

Back within the stone age of the Internet after I first created my first enterprise web site, life was good.

I put in Apache and created a number of easy HTML pages that said a number of necessary issues about my enterprise and gave necessary data like an outline of my product and methods to contact me. It was a static web site as a result of the content material seldom modified. Maintenance was easy due to the unchanging nature of my website.

Static content material

Static content material is simple and nonetheless widespread. Let’s take a fast take a look at a pair pattern static internet pages. You do not want a working web site to carry out these little experiments. Just place the recordsdata in your house listing and open them along with your browser. You will see precisely what you’ll if the file have been served to your browser by way of an online server.

The very first thing you want on a static web site is the index.html file which is often situated within the /var/www/html listing. This file might be so simple as a textual content phrase similar to “Hello world” with none HTML markup in any respect. This would merely show the textual content string. Create index.html in your house listing and add “Hello world” (with out the quotes) because it’s solely content material. Open the index.html in your browser with the next URL.

file:///residence/<yourhomedirectory>/index.html

So HTML shouldn’t be required, however in case you had a considerable amount of textual content that wanted formatting, the outcomes of an online web page with no HTML coding can be incomprehensible with every thing working collectively.

So the subsequent step is to make the content material extra readable by utilizing a little bit of HTML coding to offer some formatting. The following command creates a web page with absolutely the minimal markup required for a static internet web page with HTML. You may additionally use your favourite editor to create the content material.

echo "<h1>Hello World</h1>" > test1.html

Now view index.html and see the distinction.

Of course you’ll be able to put a variety of further HTML across the precise content material line to make a extra full and customary internet web page. That extra full model as proven beneath will nonetheless show the identical ends in the browser, but it surely additionally kinds the idea for extra standardized site. Go forward and use this content material to your index.html file and show it in your browser.


I constructed a pair static web sites utilizing these strategies, however my life was about to alter.

Dynamic internet pages for a brand new job

I took a brand new job through which my main activity was to create and preserve the CGI (Common Gateway Interface) code for a really dynamic web site. In this context, dynamic signifies that the HTML wanted to provide the online web page on a browser was generated from information that could possibly be totally different each time the web page was accessed. This contains enter from the consumer on an online kind that’s used to lookup information in a database. The ensuing information is surrounded by applicable HTML and displayed on the requesting browser. But it doesn’t have to be that complicated.

Using CGI scripts for a web site lets you create easy or complicated interactive packages that may be run to offer a dynamic internet web page that may change based mostly on enter, calculations, present situations within the server, and so forth. There are many languages that can be utilized for CGI scripts. We will take a look at two of them, Perl and Bash. Other standard CGI languages embrace PHP and Python.

This article doesn’t cowl set up and setup of Apache or some other internet server. If you’ve entry to an online server you could experiment with, you’ll be able to instantly view the outcomes as they would seem in a browser. Otherwise, you’ll be able to nonetheless run the packages from the command line and examine the HTML that may be created. You can even redirect that HTML output to a file after which show the ensuing file in your browser.

Using Perl

Perl is a very fashionable language for CGI scripts. Its power is that it’s a very highly effective language for the manipulation of textual content.

To get CGI scripts to execute, you want the next line within the in httpd.conf for the web site you might be utilizing. This tells the online server the place your executable CGI recordsdata are situated. For this experiment, let’s not fear about that.

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

Add the next Perl code to the file index.cgi, which ought to be situated in your house listing to your experimentation. Set the possession of the file to apache.apache while you use an online server, and set the permissions to 755 as a result of it should be executable regardless of the place it’s situated.

#!/usr/bin/perl
print "Content-type: textual content/htmlnn";
print "<html><physique>n";
print "<h1>Hello World</h1>n";
print "Using Perl<p>n";
print "</physique></html>n";

Run this program from the command line and examine the outcomes. It ought to show the HTML code it is going to generate.

Now view the index.cgi in your browser. Well, all you get is the contents of the file. Browsers actually need to have this delivered as CGI content material. Apache does not likely know that it must run the file as a CGI program except the Apache configuration for the site contains the “ScriptAlias” definition as proven above. Without that little bit of configuration Apache merely ship the info within the file to the browser. If you’ve entry to an online server, you possibly can do this out along with your executable index recordsdata within the /var/www/cgi-bin listing.

To see what this is able to appear like in your browser, run this system once more and redirect the output to a brand new file. Name it no matter you need. Then use your browser to view the file that comprises the generated content material.

The above CGI program continues to be producing static content material as a result of it at all times shows the identical output. Add the next line to your CGI program instantly after the “Hello World” line. The Perl “system” command executes the instructions following it in a system shell, and returns the outcome to this system. In this case, we merely grep the present RAM utilization out of the outcomes from the free command.

system "free | grep Memn";

Now run this system once more and redirect the output to the outcomes file. Reload the file within the browser. You ought to see an extra line in order that shows the system reminiscence statistics. Run this system and refresh the browser a pair extra instances and spot that the reminiscence utilization ought to change often.

Using Bash

Bash might be the only language of all to be used in CGI scripts. Its main power for CGI programming is that it has direct entry to all the customary GNU utilities and system packages.

Rename the present index.cgi to Perl.index.cgi and create a brand new index.cgi with the next content material. Remember to set the permissions accurately to executable.

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content material="text/html; charset=UTF-8">'
echo '<title>Hello World</title>'
echo '</head>'
echo '<physique>'
echo '<h1>Hello World</h1><p>'
echo 'Using Bash<p>'
free | grep Mem
echo '</physique>'
echo '</html>'
exit zero

Execute this program from the command line and examine the output, then run it and redirect the output to the short-term outcomes file you created earlier than. Then refresh the browser to view what it seems to be like displayed as an online web page.

Conclusion

It is definitely quite simple to create CGI packages that can be utilized to generate a variety of dynamic internet pages. This is a trivial instance however you need to now see a few of the potentialities.  

Most Popular

To Top