PERL on the apache Web Server

This section will ensure that the PERL scripting language is installed on the system. Also, we will look at how to write a PERL script and get it to work through the browser.

PERL Configuration Top of Page

PERL , the Practical Extraction and Report Language, has - as of this writing - over 28 years of development. It's one of the oldest programming languages, and has been in use on websites since they were first conceived and launched. In this section, we'll consider setting our web site up to run PERL scripts. Also, we'll write a simple script to show a very basic example of how PERL works.

Figure 6.39. PERL Configuration Image #1

PERL Configuration Image #1

The image above first shows our check on the system to see if PERL is installed. In this case, it is - which is typically the case on most default installations of Linux. The next thing shown in the image is to change to the '/var/www/cgi-bin' directory. This directory is where PERL scripts must live in order to run correctly through the browser. [30]

PERL Script Top of Page

The next step is to create a file called first.pl. See the image below for the content that needs to be placed in the file.

Figure 6.40. PERL Configuration Image #2

PERL Configuration Image #2

Inside the first.pl file, create content exactly as shown above. The same content is shown below, as "live" text, in case you have problems.[31]

        #!/usr/bin/perl  1
        print "Content-type: text/html\n\n"; 2
        print "Hello from the World of PERL.";

1

This line, called the "she-bang", must point to the PERL executable on your system. That location is found by the `which` command used earlier.

2

These lines must be duplicated exactly as shown.

After the PERL script has been created, it must be set to executable by setting the execute bit as shown. The command is `chmod +x first.pl`.

Web Access PERL Script Top of Page

Now you're ready to try to access the file through a browser, as shown below.

Figure 6.41. PERL Configuration Image #3

PERL Configuration Image #3

When all of the above is in place, the script can be called by navigating to http://<your-server.name>/cgi-bin/first.pl. If it works, congratulations. If not, stop here and figure out why before proceeding.

What Makes this PERL Script Work? Top of Page

Figure 6.42. PERL Configuration Image #4

PERL Configuration Image #4

What make this script work like it does? Looking at the image above, the apache directives that apply to the CGI configuration are shown. The line that configures ScriptAlias makes the cgi-bin directory accessible through a browser. Remember that this directory is outside the main web root '/var/www/html'. That directive also says, in essence, run scripts in this directory. The next stanza (between the '<Directory>' & '</Directory>' containers) limits access and functionality in such a way that no one can read the scripts through the browser, but they can still be executed when called correctly. The result of the script running are what is returned to the person accessing the web site.



[30] CGI stands for Common Gateway Interface. To quote: "Common Gateway Interface (CGI) is a standard way for web servers to interface with executable programs installed on a server that generate web pages dynamically. Such programs are known as CGI scripts or simply CGIs; they are usually written in a scripting language, but can be written in any programming language." See CGI on W3C & CGI Wikipedia Article for more info.

[31] Bear in mind that every character and quote in this file is important, and the script will not run if it's not right. CGI programming can be very unforgiving. For more information, see Apache 2.4 Webserver Documentation - click on "CGI: Dynamic Content", or Apache 2.4 CGI Documentation - click on "Writing a CGI Program", for more details.