Help getting Perl/CGI running on OS X localhost with apache

Discussion in 'Perl Misc' started by petermichaux, May 10, 2005.

  1. petermichaux

    petermichaux Guest

    Hi,

    I am trying to learn about perl and cgi. I have apache and php running
    on my OS X (10.3) localhost. I followed the instructions on the
    following site to configure apache for perl my modifying the file
    "/etc/httpd/httpd.conf" on my computer.

    http://www.cgi101.com/book/connect/mac.html

    The only difference from that webpage is my configure file will parse
    ".shtml" files instead of ".html" files.

    I have written a file called "first.cgi" and saved it in my sites
    folder.

    #!/usr/bin/perl -w
    print "Content-type: text/html\n\n";
    print "Hello, World!\n";

    At the terminal command line I can successfully run the command "perl
    first.cgi".

    When i point my browser to "http://localhost/~peter/first.cgi" I get
    the following message

    Forbidden
    You don't have permission to access /~peter/first.cgi on this server.
    Apache/1.3.33 Server at peter-michauxs-computer.local Port 80

    I tried using the terminal command "chmod 755 first.cgi". It looks like
    the permissions are set correctly

    -rwxr-xr-x 1 peter peter 108 9 May 23:14 first.cgi*

    The forbidden message did not go away. What's going wrong? Is apache
    not configured correctly? Do i need any extra files in my sites folder?

    Any help greatly appreciated!!!!!

    Thanks,
    Peter
     
    petermichaux, May 10, 2005
    #1
    1. Advertisements

  2. petermichaux

    John Bokma Guest

    wrote:
    shtml = SSI, not CGI
    Use a cgi-bin dir *outside* your document root.
    use warnings; # instead of -w
    use strict;

    Also, use -T.
    Be careful with printing your own headers...

    You can use <<HERE docs, eg.

    print <<HELLO;
    Content-type: text/plain

    Hello, World!
    HELLO

    (note that what you served was not valid HTML, hence I changed the
    content-type to plain).
    make a cgi-bin directory in the *parent* of your Document Root, e.g.

    cgi-bin
    htdocs

    next you need:

    ScriptAlias /cgi-bin/ "/absolute/path/to/cgi-bin/"

    <Directory "/absolute/path/to/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
    </Directory>

    Note that the second path has *no* trailing / and the first *has*.

    If you are serious in developing stuff I recommend using virtual hosts,
    see: http://johnbokma.com/mexit/2005/01/05/apache-httpd-conf-split.html

    Check your error_log, I often use:

    tail -f logs/error_log

    HTH,
     
    John Bokma, May 10, 2005
    #2
    1. Advertisements

  3. petermichaux

    axel Guest

    Have you checked that the permissions on the directory allow access?
    Quick check: copy first.cgi to first.html and see if the perl script is
    displayed as text.

    Otherwise it seems like an Apache configuration issue (perhaps CGI scripts
    are not enabled in user directories).

    Axel
     
    axel, May 10, 2005
    #3
  4. That's fine. If you need help with Perl, this is a good place to ask.
    Btw, please study the posting guidelines:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    You'd better not bring up CGI questions here, unless the problem is
    related to the use of any of the CPAN modules for dealing with CGI
    tasks. Other CGI questions are better asked in
    comp.infosystems.www.authoring.cgi.
    That's an Apache configuration problem, which is even more off topic in
    this group. You may want to try this group instead:

    comp.infosystems.www.servers.ms-windows

    OTOH, if you want to avoid the hazzle you are currently experiencing,
    why not try the bundle at http://www.indigostar.com/indigoperl.htm
     
    Gunnar Hjalmarsson, May 10, 2005
    #4
  5. petermichaux

    petermichaux Guest

    Hi John,

    Thanks for the reply. Unfortunately it went over my head. Could you
    clarify a few points
    Will this affect my first.cgi test since i am directing my browser to
    the actual cgi file? So what should I do?
    My sites folder is "/users/peter/Sites". What should the cgi-bin
    directory be?

    I cannot understand what you complete first.cgi file would be with the
    modifications you have suggested. I have seen other hello world files
    like mine suggested on the internet. I'm surprised that you seem to say
    it is not even close to correct.

    I do not understand "make a cgi-bin directory in the *parent* of your
    Document Root, e.g....". Where would cgi-bin and htdocs be in relation
    to "/users/peter/Sites"?

    Sorry, I guess I need a bit more hand holding to get started. But I am
    really interested.

    Thanks,
    Peter
     
    petermichaux, May 10, 2005
    #5
  6. He is on OS X, which is based on BSD Unix so, he'll probably have better
    luck in

    comp.infosystems.www.servers.unix
    comp.infosystems.www.servers.mac
    I don't think that is available for OS X.

    Sinan
     
    A. Sinan Unur, May 10, 2005
    #6
  7. wrote in @o13g2000cwo.googlegroups.com:
    Good for you. However, what you are interested in, currently, does not have
    anything to do with Perl.

    You have an Apache configuration issue. Thus, you should read about Apache
    configuration, and ask questions on the relevant fora.

    Sinan
     
    A. Sinan Unur, May 10, 2005
    #7
  8. petermichaux

    petermichaux Guest

    The off topic messages did not appear until after I posted my second
    message. It seemed like one person did not mind the question so I
    continued. Rough crowd 'round here.

    As far as _perl_ goes, is my file "first.cgi" acceptable for what I am
    trying to do? John does not think it will work.

    #!/usr/bin/perl -w
    print "Content-type: text/html\n\n";
    print "Hello, World!\n";

    Thanks,
    Peter
     
    petermichaux, May 10, 2005
    #8
  9. Thanks for the correction! Don't know why OS X made me think Windows...
    No, of course it's not.
     
    Gunnar Hjalmarsson, May 10, 2005
    #9
  10. If you consider the volume of messages in this group, you'll likely
    understand why we point out when somebody brings up an off topic problem.
    John didn't say it won't work, but he suggested a couple of alternative
    ways to do it.
    That's a perfectly fine script for testing purposes ( provided that the
    path to the Perl interpreter actually is /usr/bin/perl ).
     
    Gunnar Hjalmarsson, May 10, 2005
    #10
  11. petermichaux

    John Bokma Guest

    wrote:
    ..shtml is parsed HTML, you can call a script using SSI directives.

    cgi is more commonly used like:

    http://example.com/cgi-bin/helloworld.cgi
    if you put index.html in Sites, and you see the index.html if you go to
    http://localhost/ your document root is /users/peter/Sites. More safe:
    search in your httpd.conf for DocumentRoot and check its value.
    /usr/bin/perl -T

    use strict;
    use warnings;

    print <<HELLO;
    Content-type: text/plain

    Hello, World!
    HELLO

    (untested)
    This holds for an amazing number of CGI scripts you will find. Most will
    be extremely badly written. A huge number are developed using trial and
    error, which often means: find a script, change lines until it works,
    make it available. In short, most scripts you find are insecure, garbage
    and an extremely bad start for anyone wanting to learn CGI.
    Normally Apache set up questions should be asked in an Apache related
    group.

    Oh, and cgi-bin should be readable by your Apache process. 711 often
    does the trick. If you can turn one 1 into 0, do so. (Same for the 5 in
    755, if you can turn a 5 in a 0 do so).
     
    John Bokma, May 10, 2005
    #11
  12. petermichaux

    John Bokma Guest

    wrote:
    Oh, it will work. But you suggest that it's html, which it's not. Adding
    use strict and use warnings might help you later on, as will the -T switch.

    In the future:

    use CGI;
    use CGI::Carp 'fatalsToBrowser';

    instead of hacking your own QUERY_STRING decoding stuff (or horror: cut &
    paste it from a cgi script you found somewhere) might save you a serious
    headache.

    Golden rule: You can't learn programming by cut and paste until it works.
     
    John Bokma, May 10, 2005
    #12
  13. petermichaux

    axel Guest

     
    axel, May 10, 2005
    #13

  14. Have you checked the Perl FAQs that mention the CGI?

    perldoc -q CGI

    Where can I learn about CGI or Web programming in Perl?

    What is the correct form of response from a CGI script?

    My CGI script runs from the command line but not the browser. (500
    Server Error)

    How can I get better error messages from a CGI program?

    How do I make sure users can't enter values into a form that cause my
    CGI script to do bad things?

    How do I decode a CGI form?


    Get it to work with .html first.

    Once that is working, then move on to getting it to work with secure HTML.


    You shouldn't say that you will be giving HTML unless you actually
    give HTML.

    Your output looks a lot more line text/plain than text/html.


    Then you do not have a Perl problem, your problem is somewhere else.


    Yes, your web server appears to be not configured correctly.


    I dunno, we help with Perl here.

    Folks that can help with server setup hang out somewhere else.


    Ask Perl questions in a Perl newsgroup.

    Ask server setup questions in a server newsgroup, such as:

    comp.infosystems.www.servers.mac
     
    Tad McClellan, May 11, 2005
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.