How to Run CGI Script in Apache

Page 1

By default, Apache script can run mainly PHP scripts. But sometimes, web developers and administrators need to run non-PHP scripts in Apache server. Sometimes their entire website or application is built using non-PHP language such as Python or Perl. In such cases, you need to run these scripts as CGI scripts in Apache server. In this article, we will learn how to run CGI script in Apache.

How to Run CGI Script in Apache

There are several ways to run CGI script in Apache – using server configuration, virtual host or .htaccess. The key is to place the following two lines in any of these files.

AddHandler cgi-script .cgi .pl .py

Options +ExecCGI

In the above code, the first line tells Apache server to treat .cgi, .pl, .py files as CGI script. The second line tells Apache server to execute CGI scripts.

Now we will look at how to run CGI script by modifying Apache configuration, virtual host and .htaccess files, one by one.

1. Using Apache Configuration

Open Apache server configuration in text editor.

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

Please update the Apache configuration file location as per your requirement. It is typically located at any of the following places:

● /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.

AddHandler cgi-script .cgi .pl .py

Options +ExecCGI

These lines enable CGI execution across your website. If you want to enable CGI execution only for specific directory such as /var/www/html/cgi-files, then add the above lines in the directory tag of the 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 directive.

Restart Apache server to apply changes.

$ sudo service apache2 restart

2. Using Virtual Host

The above method enabled CGI execution for all domains, sub domains and virtual hosts on your website. If you want to enable CGI scripts for specific domain, then open virtual host file of that domain or sub domain.

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

Add the following code block to enable CGI script execution for /var/www/html/cgi-files directory.

<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.

$ sudo service apache2 restart

3. Using .htaccess File

If you do not have access to Apache server configuration file or virtual host file, then open .htaccess file.

$ vi /var/www/html/.htaccess

Add the following lines to it to enable CGI execution for specific file types on your website.

AddHandler cgi-script .cgi .pl .py

Options +ExecCGI

If you want to enable CGI execution for all files in a specific directory, add the following code to your website.

<Directory /var/www/html/cgi-files>

<Files ~ (.cgi$)>

SetHandler cgi-script

Options ExecCGI

allow from all

</Files>

</Directory>

Now when you run any of the .cgi, .pl or .py scripts on your website, in your web browser, it will be automatically executed as CGI script in Apache server.

Originally published at https://techosha.com/how-to-run-cgi-script-in-apache/

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.