Friday, September 21, 2007

Running your own CGI scripts

Running your own CGI scripts:

Issue: If you want to enable a directory and its subdirectories for CGI, create the following .htaccess file and add the following lines

Options ExecCGI

addType application/x-httpd-cgi .cgi .pl


These contents may be combined with other configuration features such as restricted access to a part of your Web site. If the system administrator really do not want you to run CGI scripts she or he can disable the use of .htaccess files for all users. .htaccess file must be set to be readable by everybody.

You can change the ownerhsip and permissions for the .htaccess file as follows:

# chmod 644 .htaccess

# chown user:user .htaccess


1) place your script in the same folder where .htaccess file is.

2) Make sure that the scipt has extension .cgi or .pl. The latter is commonly used for programs written in PERL - a popular language for writing CGI scripts.

In case you wrote your script in C or C++ you must compile it using a compiler for the particular operating system. For example a program compiled on Windows will not run directly on Unix or Linux. You have to compile the source code using a complier for Unix, Linux, or other operating system accordingly.

After the program is transfered to the Web server it needs to be marked as executable. Typically files are marked and read-write only by default and cannot be run as programs.

You can do it by typing in console window the following command:

# chmod a+rx program.pl ( where program.pl is the name of your program.)

You can test a sample script in PERL and look at its see its source code. If you want to copy the script to your Web site you need to modify content of the very first line so that it points to the location of PERL on your Web server.

There are some security issues in case your program needs to write to a file - you need to set permission for other people (such as Web server daemon) to allow modification of that file or files in your directory. Use chmod command to enable proper access to some of your files.