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.

Capture

Instructions

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.

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

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

8 thoughts on “How can a user run a .cgi script outside the cgi-bin directory?

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

  3. To the below path i.e
    /home/chandana/LINUX_BASED_DIAG/modules/ce/src/hal/diag/cards/csc01/code/
    i have to create a file from web page.
    what is the shell script written for this ?

  4. Hello,
    “please make sure that those extensions are appended with AddHandler directive on the main Apache configuration file” ????

    what does the above mean??

    what do you mean … “AddHandler directive” into the “main Apache configuration file”??
    do you mean like the below example

    # ——————————
    # main Apache configuration file

    home/xxx/pubic_html

    # AddHandler directive (is that the same code thats in the .htaccess file?)
    AddHandler cgi-script .cgi .pl
    Options +ExecCGI

    # ——————————

Leave a Reply

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