CGI stands for Common Gateway Interface are useful for creating dynamic content on web page by transferring data from server to client. CGI script can be written in any scripting language like bash, java, c, perl, python etc. Which can be easily executed by web server.
This article will help you to configure your own script as CGI scripts in apache VirtualHost. We will use some test cgi scripts for example. Follow below steps.
Step 1. Create CGI Script
Create any test script in any scripting language. For this example I am creating two scripts, one with the bash scripting and other with perl scripting.
Bash Script: [
#!/bin/bash echo "Content-type: text/htmlnn" echo "Welcome! to First CGI Script"
Perl Script: [
#!/usr/bin/perl print "Content-type: text/htmlnn"; print "Welcome! to First CGI Script";
Now put your both scripts under scripts directory which can be created anywhere in system. Make sure that is accessible by Apache. For this example we have created both script under /usr/local/cgi-bin/ directory.
Step 2: Configure VirtualHost
Now edit Apache configuration file and go to defined VirtualHost. Add the following highlighted lines in VirtualHost configuration.
<VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www/html
ScriptAlias /cgi-bin/ "/usr/local/cgi-bin/" <Directory "/usr/local/cgi-bin/"> Options +ExecCGI AddHandler cgi-script .cgi </Directory></VirtualHost>
Save configuration file and restart Apache service.
Step 3: Verify CGI Script
Now test your CGI scripts by opening urls like below in your favorite web browser. You will show the text “Welcome! to First CGI Script” on your webpage.