CGI Scripts on Linux: Apache CGI-bin Configuration Examples

The Common Gateway Interface (CGI) is an essential technology that serves as a link between web clients and servers, enabling the execution of scripts and programs on a web server. Although newer technologies have largely replaced CGI, it remains a useful tool for Linux system administrators for quick system monitoring and administrative tasks via web browsers.

In this tutorial you will learn:

  • How to install the Apache web server on Ubuntu Linux
  • How to create and execute CGI scripts in various programming languages
  • How to access and test CGI scripts from a web browser
CGI Scripts on Linux: Apache CGI-bin Configuration Examples
CGI Scripts on Linux: Apache CGI-bin Configuration Examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux operating system
Software Apache web server, GNU Bash, Python, Perl, C/C++ compilers
Other Basic knowledge of Linux command line and programming
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user
INTERESTING FACT ABOUT CGI WITH APACHE ON LINUX
Apache on Linux has historically been one of the primary platforms for CGI due to its robustness and flexibility. Despite the rise of newer technologies, CGI remains relevant for tasks that require spawning external programs or when running scripts in languages not natively supported by the web server. One notable aspect is Apache’s ability to handle CGI scripts securely and efficiently, leveraging Linux’s strong process isolation mechanisms. Additionally, Apache can be configured to execute CGI scripts in almost any programming language, making it an incredibly versatile tool for dynamic content generation.

Installation and Setup

Before you can start using CGI scripts, you must set up the Apache web server and prepare your environment for CGI execution. The process involves several detailed steps to ensure Apache is properly configured to execute CGI scripts.

  1. Installing Apache: Begin by installing the Apache web server on your Ubuntu system. Open your terminal and enter the following command:
    $ sudo apt update
    $ sudo apt install apache2

    This command updates your package list and installs the Apache 2 web server. Apache is pre-configured with a default website and the ability to execute CGI scripts.

  2. Enabling the CGI Module: Apache requires the CGI module to be enabled for running CGI scripts. Enable this module by executing:
    $ sudo a2enmod cgi

    This command enables the ‘cgi’ module, which is necessary for handling CGI script execution.

  3. Apache Configuration for CGI: To execute CGI scripts written in various languages such as Python, Perl, or Bash, Apache must be configured to recognize and handle these files appropriately. Modify the Apache configuration file, which is usually located in /etc/apache2/sites-available/ or directly in the main configuration file, depending on your system setup. Add or ensure the following configurations are present:
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI
        AddHandler cgi-script .cgi .pl .py .sh .rb .go .js .php
        Require all granted
    </Directory>

    This configuration sets up a URL path that maps to the directory where CGI scripts are stored (ScriptAlias), allows for the execution of CGI scripts in the specified directory (Options +ExecCGI), informs Apache that files with extensions like .cgi, .pl, .py, .sh, .rb, .go, .js, and .php should be treated as CGI scripts (AddHandler), and permits all users to access the CGI scripts, which may need to be restricted based on security policies (Require all granted).

  4. Restart Apache to Apply Changes: After configuring the settings, restart Apache to ensure all changes take effect:
    $ sudo systemctl restart apache2
  5. Verifying Apache Installation: To confirm that Apache has been successfully installed and is running, you can visit your server’s IP address in a web browser. You should see the default Apache Ubuntu default page.

Creating CGI Scripts

Once Apache is configured to run CGI scripts, you can start creating scripts in various programming languages. Each script must be executable and located in the designated CGI directory. Follow these detailed steps for creating CGI scripts using Bash, Python, Perl, and C/C++.

  1. Creating a Bash CGI Script: Start with a simple Bash script that outputs HTML content. Use your favorite text editor to create a file named example-bash.sh in the /usr/lib/cgi-bin/ directory.
    #!/bin/bash
    echo "Content-type: text/html"
    echo ""
    echo "<html><head><title>CGI Bash Example</title></head><body>"
    echo "<h1>Hello from Bash CGI!</h1>"
    echo "</body></html>"

    Save the script and make it executable:

    $ sudo chmod +x /usr/lib/cgi-bin/example-bash.sh

    This script sends HTTP headers and HTML content as output, which is viewed in a web browser.

  2. Creating a Python CGI Script: Python scripts can also serve CGI purposes. Create a file named example-python.py in the /usr/lib/cgi-bin/ directory.
    #!/usr/bin/python3
    print("Content-type: text/html\n\n")
    print("<html><head><title>CGI Python Example</title></head><body>")
    print("<h1>Hello from Python CGI!</h1>")
    print("</body></html>")

    Save the script and make it executable:

    $ sudo chmod +x /usr/lib/cgi-bin/example-python.py

    This script performs a similar function as the Bash script, using Python to output HTML.

  3. Creating a Perl CGI Script: Perl is another popular language for CGI scripts. Create a file named example-perl.pl in the /usr/lib/cgi-bin/ directory.
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print "<html><head><title>CGI Perl Example</title></head><body>";
    print "<h1>Hello from Perl CGI!</h1>";
    print "</body></html>";

    Save the script and make it executable:

    $ sudo chmod +x /usr/lib/cgi-bin/example-perl.pl

    This Perl script outputs HTML to the browser, demonstrating how CGI scripts function in Perl.

  4. Creating a C CGI Script: C can also be utilized for CGI scripting to handle more complex processing tasks. First, ensure that the GCC compiler is installed on your system:
    $ sudo apt install gcc

    Next, create a file named example-c.c in the /usr/lib/cgi-bin/ directory:

    #include <stdio.h>
    int main(void) {
        printf("Content-Type: text/html\n\n");
        printf("<html><head><title>CGI C Example</title></head><body>");
        printf("<h1>Hello from C CGI!</h1>");
        printf("</body></html>");
        return 0;
    }

    Compile the C program to create an executable suitable for CGI processing:

    $ sudo gcc -o /usr/lib/cgi-bin/example-c /usr/lib/cgi-bin/example-c.c

    This step compiles your C source code into an executable named example-c, which is stored in the CGI directory, allowing it to be executed as a CGI script by the web server. This example demonstrates how to output HTML content using a C program, compiled as a CGI script.

  5. Ruby CGI Script: Ruby is an easy-to-use scripting language ideal for quick scripts. First, ensure Ruby is installed:
    $ sudo apt install ruby

    Create a file named example-ruby.rb in the /usr/lib/cgi-bin/ directory:

    #!/usr/bin/ruby
    puts "Content-type: text/html\n\n"
    puts "<html><head><title>CGI Ruby Example</title></head><body>"
    puts "<h1>Hello from Ruby CGI!</h1>"
    puts "</body></html>"

    Save this script and make it executable:

    $ sudo chmod +x /usr/lib/cgi-bin/example-ruby.rb

    This script outputs HTML content, similar to the earlier examples but using Ruby.



  6. Node.js CGI Script: Node.js is typically used for full server applications, but can run CGI scripts with proper setup. Install Node.js first:
    $ sudo apt-get install nodejs

    Create a file named example-node.js in the /usr/lib/cgi-bin/ directory:

    #!/usr/bin/node
    console.log("Content-type: text/html\n\n");
    console.log("<html><head><title>CGI Node.js Example</title></head><body>");
    console.log("<h1>Hello from Node.js CGI!</h1>");
    console.log("</body></html>");

    Make the script executable:

    $ sudo chmod +x /usr/lib/cgi-bin/example-node.js

    This example uses JavaScript in a Node.js environment to serve CGI content.

  7. PHP CGI Script: PHP can also be used as a CGI script. Install PHP CLI if it’s not already available:
    $ sudo apt install php-cli

    Create a file named example-php.php in the /usr/lib/cgi-bin/ directory:

    #!/usr/bin/php
    <?php
    echo "Content-type: text/html\n\n";
    echo "<html><head><title>CGI PHP Example</title></head><body>";
    echo "<h1>Hello from PHP CGI!</h1>";
    echo "</body></html>";

    Change the file to be executable:

    $ sudo chmod +x /usr/lib/cgi-bin/example-php.php

    This PHP script will generate HTML content when accessed through a web server.

  8. Go CGI Script: Go is known for its performance and efficiency. Install Go:
    $ sudo apt install golang-go

    Create a Go script named example-go.go in the /usr/lib/cgi-bin/ directory:

    // File: example-go.go
    package main
    import "fmt"
    func main() {
        fmt.Println("Content-type: text/html\n\n")
        fmt.Println("<html><head><title>CGI Go Example</title></head><body>")
        fmt.Println("<h1>Hello from Go CGI!</h1>")
        fmt.Println("</body></html>")
    }

    Compile the Go program and make the executable:

    $ sudo go build -o /usr/lib/cgi-bin/example-go /usr/lib/cgi-bin/example-go.go

    This script uses Go to produce HTML output for CGI purposes.

  9. Tcl CGI Script: Tcl is another scripting language suitable for CGI scripting. Install Tcl:
    $ sudo apt install tcl

    Create a Tcl script named example-tcl.tcl in the /usr/lib/cgi-bin/ directory:

    #!/usr/bin/tclsh
    puts "Content-type: text/html\n\n"
    puts "<html><head><title>CGI Tcl Example</title></head><body>"
    puts "<h1>Hello from Tcl CGI!</h1>"
    puts "</body></html>"

    Make the script executable:

    $ sudo chmod +x /usr/lib/cgi-bin/example-tcl.tcl

    This script demonstrates CGI capabilities using Tcl to generate HTML content.

Testing CGI Scripts

After creating CGI scripts in various programming languages, it is crucial to test them to ensure they function correctly. This involves accessing each script through a web browser to verify that they execute and render the intended output.

  1. Accessing CGI Scripts: Use your web browser to navigate to each CGI script you have created. For example, to access a Bash CGI script, you might use:
    http://cgi-example.local/cgi-bin/example-bash.sh

    Similarly, replace example-bash.sh with the name of other scripts, like example-python.py, example-perl.pl, example-ruby.rb, etc., to test those scripts.

  2. Viewing CGI Outputs: Upon accessing the URLs of your scripts, your web server executes them and sends their output as a web page. Check the output to ensure it displays correctly and reflects what is expected from the script. This confirms the script is functioning properly and the server is correctly configured to handle CGI scripts.

Conclusion

Though it may be seen as outdated, CGI provides a powerful way to integrate external programming scripts into the web environment for effective system management. By following this guide, you can deploy CGI scripts for various administrative and monitoring tasks on your Linux servers.

 



Comments and Discussions
Linux Forum