How can a user run a .cgi script outside the cgi-bin directory?
What does CGI mean?
Common Gateway Interface is abbreviated to CGI. This is a standard way for web servers to interface executable scripts with end users.
Those executable scripts or programs execute by this way and generate web pages dynamically.
CGI scripts are usually written in scripting language, but can be written in any programming language.
Most of the hosts does not allow to execute those scripts outside the CGI-BIN folder on the public folder.
Some hosts already allowed this feature via the main Apache configuration file. Here I’m explaining the steps to add some extra lines on the .htaccess file to overwrite the main configuration, if it’s disabled. This is really simple. You can simply do this from the cPanel.
If you have shell access to your cPanel account, you can simply create a .htaccess file under the home directory or the public folder.
Step 1 : Log into cPanel.
Step 2 : Create a .htaccess file under the home directory. If you prefer shell, you can use vi command.
# vi .htaccess
Step 3 : Add the following code in that .htaccess file.
AddHandler cgi-script .cgi .pl Options +ExecCGI
That’s it!! Please try to execute the code from your browser now. It’ll work!!
> You can add any file extension to the AddHandler line in your .htaccess file. Here the .pl stands for the per scripts and .cgi for CGI scripts.
> To enable server-wide, please make sure that those extensions are appended with AddHandler directive on the main Apache configuration file.
> File permission for .htaccess is 644.
> File permission for scripts are 755.
Do you want to test this?
Here I’m attaching a sample perl CGI script.
Please try to add this code and execute this from the web browser.
#!/usr/bin/perl # hello.pl -- my first perl script CryBit.com! print "Content-type: text/html\n\n"; print "Hello, world!\n";
Please check this and let me know if you need any help!