FreeBSD Setup Lighttpd CGI BIN For Perl Programs

CGI applications can be read and written to all computer languages originating from STDIN and written to STDOUT, regardless of whether the program is interpreted by a Unix shell and compiled by C or C++, or a combination of both, for example Perl. The first CGI programs were written in C and needed to be compiled into executable binary files. Therefore, the directory where the compiled CGI program is executed is named cgi-bin, and the source file directory is named cgi-src. Today most servers come with preconfigured directories for CGI programs.

The interaction between the browser and the web server is governed by the HyperText Transfer Protocol (HTTP), which is now the official Internet standard. HTTP uses a simple request and response model. For example, the client makes a TCP connection to the server and sends a request, the server sends a response, and the connection is closed. The request and response is in the form of a message written in a series of simple lines of text.

HTTP request and response messages have two parts. The first is the header, which contains descriptive information about the request or response. The different types of headers and their possible content are determined entirely by the HTTP protocol. The header is followed by a blank line, then the message body. The message body is the actual content of the message, such as an HTML page or a GIF image.

1. Common Gateway Interface

Common Gateway Interface or abbreviated as CGI is a standard for connecting various application programs to web pages. CGI is like a computer program that acts as an intermediary between the HTML standards that create web displays and other programs, such as databases. The results obtained from the search process are sent back to the web page to be displayed in HTML format.

Initially CGI was one that approached server-side programming applications. The CGI programs that are often used are C++ and Perl. CGI is part of a web server that can communicate with other programs on the server. With CGI the web server can call programs created from various programming languages (Common). Interaction between users and various applications, for example databases, can be bridged by CGI (Gateway). This CGI capability can be used for the IIS Web Server.

The HTTP protocol makes the client and server understand each other by transferring all the information between them using headers, where each header is a key-value pair. When you submit a form, the CGI program looks for headers containing input information, processes the data received, for example, queries the database for keywords provided through the form, and when ready to returns a response to the form. the client will send a custom header telling the client what type of information to expect, followed by the information itself. The server can send additional headers, but these are optional. Look at the image below.

CGI is often used as a mechanism to obtain information from users through filling out forms, accessing databases, or producing dynamic pages. although in principle the CGI mechanism does not have security holes, programs or scripts created as CGI can have security holes or not on purpose). Potential security holes used that can occur with CGI include:
  1. A naughty user can install a CGI script so that it can send a password file to visitors who execute the CGI.
  2. The CGI program is called many times so that the server becomes burdened because it has to run several CGI programs which use up memory and CPU cycles from the web server.

2. Installing Perl Language

Because we will activate the CGI BIN mod, you must install the Perl language first. The FreeBSD server has its own binary package system. This will make it easier for you to install and manage the software. With this package system you can also manage the installation of Perl modules.

Type the command as follows to install Perl5.
root@ns5:~ # pkg install perl5.38-5.38.2_1
The above command will install Perl5 on your FreeBSD server, and the binary path to Perl is /usr/local/bin/Perl.

Due to the large number of Perl versions in the FreeBSD package repository, you must specify the Perl version in the /etc/make.conf file.
root@ns5:~ # ee /etc/make.conf

DEFAULT_VERSIONS+=  perl5=5.30

3. Enable mod CGI on Lighttpd

CGI principles and how to write CGI applications are not the focus of this discussion. But understanding CGI configuration helps to understand the mod_cgi code that we will implement to configure lighttpd to run CGI processes.

Before you continue discussing this article, to make it easier to configure CGI mods on Lighttpd, you should read our previous article.

Before we activate the CGI mod, we first install several PHP applications that Lighttpd needs to activate the CGI mod. Type this command to install the CGI mod dependencies.
root@ns5:~ # pkg install php82 mod_php82 php82-mysqli php82-xmlwriter
root@ns5:~ # pkg install php82-gd php82-phar php82-ctype php82-filter php82-iconv php82-curl php82-mysqli php82-pdo php82-tokenizer php82-mbstring php82-session php82-simplexml php82-xml php82-zlib php82-zip php82-dom php82-pdo_mysql php82-ctype
OK, now let's continue activating the CGI mod. Open the "/usr/local/etc/lighttpd/modules.conf" file, and enter mod_cgi in the file.

server.modules = (
#  "mod_rewrite",
#  "mod_auth",
#  "mod_authn_file",
#  "mod_redirect",
#  "mod_setenv",

Now that the CGI module is active, we will run the module. Open the "/usr/local/etc/lighttpd/conf.d/cgi.conf" file and delete all the scripts in it, then replace it with the script below.

$HTTP["url"] =~ "/cgi-bin/" {
cgi.assign                 = ( ".pl"  => "/usr/local/bin/perl",
                               ".cgi" => "/usr/local/bin/perl",
                               ".rb"  => "/usr/bin/ruby",
                               ".erb" => "/usr/bin/eruby",
                               ".py"  => "/usr/local/bin/python" )

Documents ending with ".pl", ".cgi", and ".py" will be regarded as CGI programs. Interpreters for these programs are followed immediately.. The next step is to restart Lighttpd.
root@ns5:~ # service lighttpd restart

4. Run Lighttpd mod_cgi

After you have finished configuring everything, do a test to see whether the configuration above can run on the Lighttpd web server. You create the "/usr/local/www/data/cgi-bin" folder, then create the "" file.
root@ns5:~ # mkdir -p /usr/local/www/data/cgi-bin
root@ns5:~ # cd /usr/local/www/data/cgi-bin
root@ns5:/usr/local/www/data/cgi-bin # touch
root@ns5:/usr/local/www/data/cgi-bin # chown -R www:www /usr/local/www/data/cgi-bin
In the "/usr/local/www/data/cgi-bin/ file, you enter the following script.

use strict;
use warnings;

# initializing arrays
my @places = ('Bangalore', 'Chennai', 'Coimbatore');
my @prime_numbers = (2, 3, 5, 7);
my @books;
$books[0] = 'Harry Potter';
$books[1] = 'Sherlock Holmes';
$books[2] = 'To Kill a Mocking Bird';
my @mixed_arr = ('Hello', 1, 'q', @places, $books[0]);

# ----- printing entire array -----
print "\@places: @places\n";
print "\@prime_numbers: @prime_numbers\n";
print "\@mixed_arr: @mixed_arr\n";
# use join to connect array items with preferred characters
print "My favorite books: ".join(', ', @books)."\n\n";

# ----- printing individual item -----
print "First item in \@places: $places[0]\n";
print "Last  item in \@places: $places[-1]\n";
print "Third last item in \@mixed_arr: $mixed_arr[-3]\n";

# ----- printing array slice -----
print "2nd and 3rd item in \@prime_numbers: @prime_numbers[1..2]\n\n";

# ----- array size -----
print "Index of last item in \@places: $#places\n";
my $arr_size = @places;
print "Number of items in \@places:    $arr_size\n\n";

# ----- manipulating array -----
# Add an item, same as $places[$#places + 1]
$places[3] = "Salem";
print "Manually add item: @places\n";

# Delete first item and shift left
print "shift:             @places\n";
# Delete last item
print "pop:               @places\n";

# Add an item at end of array
push(@places, "Mysore");
print "push:              @places\n";
# Add an item at beginning of array
unshift(@places, "Hyderabad");
print "unshift:           @places\n";

# change array size by assigning value to $#places
# Change no. of items to 2
$#places = 1;
print "\$#places = 1       @places\n";
# Change no. of items to 0
$#places = -1;
print "\$#places = -1      @places\n";

Open the Google Chrome web browser, type "", the result is as shown in the image below.

We continue by testing the .cgi file. Still in the "/usr/local/www/data/cgi-bin" folder, create the "cgiperl.cgi" file.
root@ns5:/usr/local/www/data/cgi-bin # touch cgiperl.cgi
root@ns5:/usr/local/www/data/cgi-bin # chown -R www:www cgiperl.cgi
Type the following script in the "/usr/local/www/data/cgi-bin/cgiperl.cgi" file.


print "Content-type: text/html\n\n";
print <<htmlcode;
<title>CGI Perl Example</title>
<h1>CGI Perl Example</h1>
<h3><p>Congratulations on successfully configuring the Perl mod on Apache24!</p><h3>

You open Google Chrome again, type "". Please see the results.

Configuring CGI scripts on the Lighttpd web server is a relatively simple process that can be done by following the guide found in this article. Enabling CGI support, creating CGI scripts, configuring Lighttpd to recognize scripts, and testing scripts are important steps in configuring CGI scripts in Lighttpd. With these steps, you can create dynamic and interactive web pages that can be used to provide information or services to your website visitors.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post