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

Important things!

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

# -- my first perl script!

print "Content-type: text/html\n\n";
print "Hello, world!\n";

Please check this and let me know if you need any help!

Arunlal Ashok

DevOps (Server & Cloud infrastructure) Engineer. I'm managing Linux servers since 2012. I started this blog to share and discuss my ideas. Any questions? Write to arun (@)

You may also like...

7 Responses

  1. frank says:

    1) Where does the .htaccess file live?
    2) What are the permissions associated with the .htaccess file?
    3) Do I need to specify the suffix for the executable file or can I run any executable file?

  2. black sasha says:

    Hi Arunlal Ashok, many thanks, this simple explanation resolved my problem with same days ….
    best regards

  3. Manjunath L says:

    yes the above documentation is helpful, in Windows if we want to run perl programs in alias ie., outside cgi-gin, By editing .htaccess with the above two lines, it worked for me. amazing, Thank you

  4. chandana says:

    To the below path i.e
    i have to create a file from web page.
    what is the shell script written for this ?

Leave a Reply

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