Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Enabling CGI Script from Apache's htdocs directory

by monkfan (Curate)
on Apr 06, 2007 at 13:44 UTC ( #608670=perlquestion: print w/ replies, xml ) Need Help??
monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,
I have a very simple cgi script like this:
#!/usr/bin/perl print "Content-type: text/html\n\n"; print "Hello World !! It works.\n";
which I stored in:
/usr/local/apache/htdocs/Test/cgi-bin
However I was unable to see it working from this website:
http://bioinfo-z.comp.nus.edu.sg/Test/cgi-bin/hello.cgi
However, my index.html can be accessed without problem here.

What was wrong with my setting above? Was there any special structure required for CGI?
As a SU I've also activated my Apache's
svcadm enable www:apache22

Regards,
Edward

Comment on Enabling CGI Script from Apache's htdocs directory
Select or Download Code
Re: Enabling CGI Script from Apache's htdocs directory
by derby (Abbot) on Apr 06, 2007 at 13:49 UTC

    Normally cgi-bin is not within the htdocs directory. Can you copy your script to /usr/local/apache/cgi-bin? If so, will it run from there? It all depends on your apache configuration (/usr/local/apache/conf/httpd.conf).

    -derby
      Hi,

      I've copied my hello.cgi to this directory /usr/local/apache/cgi-bin. And it works, thanks.

      But my intention is to really access the cgi-bin from my htdocs/ directory. Like this:
      htdocs-- |_Test |_cgi-bin/ | |_Webap1 |_ cgi-bin/ | |_Webap2 |_ cgi-bin/
      Because there will be many webapps I need to build (hence many subdirectory other than "Test"). I've tried to modified the following httpd.conf entry:
      #ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/" # into ScriptAlias /cgi-bin/ "/usr/local/apache/htdocs/cgi-bin/
      But still unable to get it working. How can I enable it from this httpd.conf below?

      Regards,
      Edward

        Check out the howto.

        -derby

        update: I'm not a apache 2.2 expert by anymeans but I think you're going to want something like this in your httpd.conf:

        <Directory /usr/local/apache/htdocs/*/cgi-bin> Options +ExecCGI </Directory>
        That being said ... I think one of the drawbacks to your approach is your exposing way too much of your underlying architecture. User's are going to see something like this:
        http://bioinfo-z.comp.nus.edu.sg/Test/cgi-bin/foo http://bioinfo-z.comp.nus.edu.sg/WebApp1/cgi-bin/bar http://bioinfo-z.comp.nus.edu.sg/WebApp2/cgi-bin/baz
        I think a better approach would be to either use virtual servers:
        http://test.comp.nus.edu.sg/cgi-bin/foo http://webapp1.comp.nus.edu.sg/cgi-bin/bar http://webapp2.comp.nus.edu.sg/cgi-bin/baz
        Or something like CGI::Application
        http://bioinfo-z.comp.nus.edu.sg/cgi-bin/test?mode=foo http://bioinfo-z.comp.nus.edu.sg/cgi-bin/webapp1?mode=bar http://bioinfo-z.comp.nus.edu.sg/cgi-bin/webapp2?mode=baz

        Because there will be many webapps I need to build (hence many subdirectory other than "Test"). I've tried to modified the following httpd.conf entry:
        #ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/" # into ScriptAlias /cgi-bin/ "/usr/local/apache/htdocs/cgi-bin/

        Note that there's the ending quote missing. But this modification would allow you to access scripts in /usr/local/apache/htdocs/cgi-bin/hello.cgi as

        http://bioinfo-z.comp.nus.edu.sg/cgi-bin/hello.cgi

        Try

        ScriptAlias /Test/cgi-bin/ /usr/local/apache/htdocs/Test/cgi-bin/ ScriptAlias /Webap1/cgi-bin/ /usr/local/apache/htdocs/Webap1/cgi-bin/ ScriptAlias /Webap2/cgi-bin/ /usr/local/apache/htdocs/Webap2/cgi-bin/

        inside your <IfModule alias_module></IfModule> block.

        You could also omit the cgi-bin part

        ScriptAlias /Test/ /usr/local/apache/htdocs/Test/cgi-bin/ ScriptAlias /Webap1/ /usr/local/apache/htdocs/Webap1/cgi-bin/ ScriptAlias /Webap2/ /usr/local/apache/htdocs/Webap2/cgi-bin/

        to access e.g /usr/local/apache/htdocs/Webap1/cgi-bin/hello.cgi as

        http://bioinfo-z.comp.nus.edu.sg/Webap1/hello.cgi

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Enabling CGI Script from Apache's htdocs directory
by gloryhack (Deacon) on Apr 06, 2007 at 14:10 UTC
    For some reason the domain you reference won't resolve for me right now so I can't answer my first question, which I'll present to you: What do you mean by "unable to see it working"? What is the failure indication?

    If, as I suspect, the failure indication is what appears to be an empty web page but no errors logged by Apache, it's possibly because your content-type is text/html but there's no HTML in your output. Try changing the content-type to text/plain and see what happens.

    If that's not it, what's the failure indication?

Re: Enabling CGI Script from Apache's htdocs directory
by MonkE (Hermit) on Apr 06, 2007 at 14:13 UTC

    In your httpd.conf shouldn't that be:

    ScriptAlias /cgi-bin/ "/usr/local/apache/htdocs/Test/cgi-bin/

    That's where you're putting your CGI script afterall isn't it?

Re: Enabling CGI Script from Apache's htdocs directory
by ww (Bishop) on Apr 06, 2007 at 15:05 UTC
    Slightly OT, but (further derby's comment), here's a side consideration for you, monkfan

    The reason 1.x Apache versions conventionally put the cgi-bin in the directory above htdocs is for your protection. While not a v2.x user, I suspect this is still true, unless there's a new security mechanism.

    If your cgi-bin(s) are in or below htdocs, the contents are accessible to those with evil intent... in many cases, even if you attempt to restrict access with the usual methods such as .htaccess. This is not a-good-thing if your scripts have sensitive content, as many (perhaps not yours, but many) do.

    IMO, the usual "good" reason for creating a cgi-bin in user-space is when you don't have the necessary rights to put your scripts in the site's root (note: "site," not "server.")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://608670]
Approved by Corion
Front-paged by neversaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-11-27 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (188 votes), past polls