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


    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.

    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

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

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

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

    petermichaux, May 10, 2005
    1. Advertisements

  2. petermichaux

    John Bokma Guest

    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!

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


    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

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

    If you are serious in developing stuff I recommend using virtual hosts,

    Check your error_log, I often use:

    tail -f logs/error_log

    John Bokma, May 10, 2005
    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, May 10, 2005
  4. That's fine. If you need help with Perl, this is a good place to ask.
    Btw, please study the posting guidelines:

    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
    That's an Apache configuration problem, which is even more off topic in
    this group. You may want to try this group instead:

    OTOH, if you want to avoid the hazzle you are currently experiencing,
    why not try the bundle at
    Gunnar Hjalmarsson, May 10, 2005
  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.

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

    I don't think that is available for OS X.

    A. Sinan Unur, May 10, 2005
  7. wrote in
    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.

    A. Sinan Unur, May 10, 2005
  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";

    petermichaux, May 10, 2005
  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
  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
  11. petermichaux

    John Bokma Guest

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

    cgi is more commonly used like:
    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!

    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

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

    John Bokma Guest

    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

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

    axel Guest

    axel, May 10, 2005

  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:

    Tad McClellan, May 11, 2005
    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.