Apache2 Installation and Enabling CGI on Ubuntu-12.04

Recently I required to use Python for dynamic web pages. So I was required to install Apache web server and enabling the Cgi in it. So here I am gonna explain the whole process.

Firstly, we need to install Apache web server.

1
$ sudo apt-get install apache2

Next, we create a directory CGI where all python (or other CGI) scripts will reside.

1
$ sudo mkdir /var/www/cgi-bin

Next we tell Apache which directories it can execute in and how to handle files with .py extension. So type in

1
$ sudo vi /etc/apache2/sites-available/default

Look for the following lines in the opened files

/etc/apache2/sites-available/default
1
2
3
4
5
6
7
     ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
      <Directory "/usr/lib/cgi-bin/">
          AllowOverride None
          Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
          Order allow,deny
          Allow from all
      </Directory>

And replace those lines with these

/etc/apache2/sites-available/default
1
2
3
4
5
6
7
8
9
         ScriptAlias /cgi-bin/ /var/www/cgi-bin/
          <Directory "/var/www/cgi-bin">
              AllowOverride None
              Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
              Order allow,deny
              Allow from all
              AddHandler cgi-script .py
              AddHandler default-handler .html .htm
          </Directory>

And now, restart the Apache.

1
$ sudo service apache2 restart

Comments