Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Apache»How to Configure CGI Scripts in Apache

    How to Configure CGI Scripts in Apache

    RahulBy RahulOctober 12, 20142 Mins ReadUpdated:December 15, 2019

    CGI stands for Common Gateway Interface are useful for creating dynamic content on the web page by transferring data from server to client. A CGI script can be written in any scripting language like Bash, java, C, Perl, Python, etc. Which can be easily executed by the webserver.

    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 the 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 the other with Perl scripting.

    Bash Script: [/usr/local/cgi-bin/mybash.cgi]

    1
    2
    3
    4
    #!/bin/bash
     
    echo -e "Content-type: text/html\n\n"
    echo "Welcome! to First CGI Script"

    Perl Script: [/usr/local/cgi-bin/myperl.cgi ]

    1
    2
    3
    4
    #!/usr/bin/perl
     
    print "Content-type: text/html\n";
    print "Welcome! to First CGI Script";

    Now put your both scripts under scripts directory which can be created anywhere in the system. Make sure that it is accessible by Apache. For this example we have created both script under /usr/local/cgi-bin/ directory.

    Step 2 – Configure Apache VirtualHost

    Now edit Apache configuration file and go to defined VirtualHost. Add the following highlighted lines in the VirtualHost configuration.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <VirtualHost *:80>
         ServerName www.example.com
         DocumentRoot /var/www/html
     
         ScriptAlias /cgi-bin/ "/usr/local/cgi-bin/"
         <Directory "/usr/local/cgi-bin/">
             Require all granted
             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.

      http://localhost/cgi-bin/myperl.cgi
      http://localhost/cgi-bin/mybash.cgi
    

    Apache CGI Script

    Apache CGI scripts
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleBash: mail: command not found (CentOS, Redhat, Ubuntu and Debian)
    Next Article How to Backup and Restore MongoDB Database

    Related Posts

    How To Install Apache Solr 9.0 on Fedora 36/35

    Updated:May 26, 20223 Mins Read

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    How To Disable HTTP Methods in Apache

    Updated:December 31, 20212 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    Common Apache Commands on Ubuntu & Debian

    4 Mins Read

    2 Comments

    1. Nobody on January 6, 2021 4:01 pm

      1. The Perl-script is missing one newline.

      2. For it to work a symbolic link to cgi.load had to be created in modules-enabled. Then a restart of Apache.
      sudo ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/
      sudo service apache2 restart

      Reply
    2. Allentown dating on October 23, 2019 10:08 am

      You were not mistaken

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.