CGI Configuration on Apache

There are 3 ways to use CGI.

  • First Way
    When we install Apache server, then /usr/lib/cgi-bin folder is automatically created. You can place c++ files in that folder and execute them on browser(localhost/cgi-bin/example).
  • Second Way
    You can also configure /var/www/cgi-bin for CGI programming.
  • Third Way
    You can also configure cgi-bin in home directory as /public_html/cgi-bin.

According to me 3rd way is best as its in home directory and we don’t have to use sudo while compiling or creating any file.

Steps to configure cgi-bin in public_html and public_html. Run following commands in terminal

  1. $ sudo a2enmod cgi
  2. $ sudo a2enmod cgid
  3. $ sudo a2enmod userdir
  4. $ sudo service apache2 restart
  5. $ mkdir ~/public_html
  6. $ cd ~/public_html
  7. $ mkdir cgi-bin
  8. $ cd /etc/apache2
  9. $ sudo vim sites-available/default
  10. Add following text in file:
     ScriptAlias /cgi-bin/ /home/*/public_html/cgi-bin/
     <Directory "/home/*/public_html/cgi-bin">
             AllowOverride None
             Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
             SetHandler cgi-script
             Order allow,deny
             Allow from all
  11. $ sudo service apache2 restart

4 Replies to “CGI Configuration on Apache”

  1. Balpreet kaur says: Reply

    Thanks…for this post……!!!

  2. Balpreet kaur says: Reply

    By the way how to test the cgi configuration…..???

    1. Mandeep Kaur says: Reply

      Run any CGI program using C++, python or any other language. See this post:

      1. Balpreetkaur says: Reply

        Thanks dear…..!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.