run cgi script in apache

How to Run CGI Script in Apache

Sometimes you may need to run non-php script such as perl or python scripts in Apache server. In such cases, you need to run them as CGI script in Apache. In this article, we will look at how to run CGI script in Apache web server.


How to Run CGI Script in Apache

There are different ways to run CGI Script in Apache – using server configuration, virtual host, or .htaccess (mod_rewrite). The key is to place the following two lines in either of these locations.

AddHandler cgi-script .cgi .pl .py
Options +ExecCGI

In the above code, the first line specifies the file extensions who need to be treated as CGI script. The second line tells Apache to execute them as CGI script. In our case, we have specified 3 file extensions to be run as CGI script.


1. Using Apache Server Configuration

If you have access to Apache server configuration file, open it in a text editor.

$ sudo vi /etc/httpd/conf/httpd.conf

Your configuration file may be in any of the following locations, depending on your Linux distribution and type of installation.

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

Add the following line to your configuration file, or uncomment them by removing # at their beginning.

AddHandler cgi-script .cgi .pl .py
Options +ExecCGI

If you want to enable CGI execution for specific directory (e.g /var/www/html/cgi-files), add the above lines within Directory tag for that folder.

<Directory /var/www/html/cgi-files>
   Options +ExecCGI
   SetHandler cgi-script
</Directory>

In this case, you need to use SetHandler directive instead of AddHandler.

Restart Apache Server to apply changes

$ sudo service apache2 restart

Please note, this will enable server-wide CGI script execution, for all website/domains/virtual hosts.


2. Using virtual host

If you don’t have access to Apache server configuration or if you want to enable CGI execution for only specific domain, then open that domain’s virtual host configuration file. Replace the following file path with that of your virtual host configuration file.

$ sudo vi /etc/apache2/sites-available/example.com.conf

Add the following lines to it to enable cgi execution for all files in /var/www/html/cg-files

<Directory /var/www/html/cgi-files>
  <Files ~ (.cgi$)>
   SetHandler cgi-script
   Options ExecCGI
   allow from all
  </Files>
 </Directory>

Save and close the file.

Restart Apache Server to apply changes

$ sudo service apache2 restart


3. Using .htaccess file

If you have enabled mod_rewrite (.htaccess) for your website, open it in a text editor.

$ sudo vi /var/www/html/.htaccess

Add the following lines to it to enable CGI execution for specific file extensions.

AddHandler cgi-script .cgi .pl .py
Options +ExecCGI

Instead, if you want to enable CGI execution for all files in a particular directory /var/www/html/cg-files then add the following lines to your .htaccess file.

<Directory /var/www/html/cgi-files>
  <Files ~ (.cgi$)>
   SetHandler cgi-script
   Options ExecCGI
   allow from all
  </Files>
 </Directory>

Now, when you open browser and request any of the .pl or .py files for which CGI execution is enabled, Apache will automatically execute it as a CGI script.

That’s it. In this article, we have learnt 3 different ways to run CGI script in Apache.

Also read:

How to Create Permanent Alias in Linux
How to Give User Folder Access in Linux
How to Install Swift in Ubuntu
How to Install Monit in CentOS
How to Install mod_wsgi in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *