PDA

View Full Version : Having trouble enabling CGI support in built in Apache


orca498
02-04-2002, 01:51 PM
I tried to follow the article below from macoshints, but I get:

[localhost:/Users/patrickm] root# cd /Library/WebServer/Configuration
/Library/WebServer/Configuration: No such file or directory.
[localhost:/Users/patrickm] root#

I have the basics set up, and I can get "test-cgi" to work. Other scripts give me a 500 Internal Server Error however.

Can anyone help?


FYI: I tried posting this question to the article on Hints last night. It worked, but this morning my account was gone and so was my post.

-------------------Original Article---------------

PB ONLY - Enabling CGI support in Apache
Thu, Dec 28 '00 at 08:29PM ? from: robg

Read the rest for the step-by-step on how to enable CGI support in Apache...

The following instructions assume that you have enabled the web server already, and have it up and running in at least its basic state. Once that's done, here's how you can enable SSI and CGI on your machine.

1. Open a terminal session, and type:

su [become root; enter your root password]
apachectl stop [stops the web server]

2. Change to the web server configuration directory, and see what's there

cd /Library/WebServer/Configuration
ls apache.con*

This should list at least one file, apache.conf. It will hopefully also list apache.conf.bak, but if it doesn't, then type this:

cp apache.conf apache.conf.bak

This will create a backup of your configuration file in case you make a mistake.

3. Edit the configuration file in your favorite editor; the example shows vi, but pico or emacs will work just as well.

vi apache.conf

4. Move down to (roughly) line 336; the section we're looking for looks like this:

#
# This may also be "None", "All", or any combination of "Indexes",
# "Includes", "FollowSymLinks", "ExecCGI", or "MultiViews".
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
Options Indexes FollowSymLinks MultiViews

You need to change the last line to read:

Options Indexes FollowSymLinks MultiViews ExecCGI +Includes

The ExecCGI turns on the CGI option; the +Includes enables SSI. When I tried without the "+" sign, my SSI calls didn't work.

5. Around line 431, you'll want to edit the default file types that Apache looks for as index files, since SSI files are traditionally named ".shtml". The section you're looking for looks like this:

#
# DirectoryIndex: Name of the file or files to use as a pre-written HTML
# directory index. Separate multiple entries with spaces.
#

DirectoryIndex index.html

Make this line read:

DirectoryIndex index.html index.htm index.shtml

I added "index.htm" as this is also a fairly common index file name, especially for Windows-sourced files.

6. Finally, find this section around line 800:

#
# To use server-parsed HTML files
#
#AddType text/html .shtml
#AddHandler server-parsed .shtml

Uncomment (remove the '#') the AddTypeand AddHandler lines to enable SSI processing.

7. Save your changes, quit the editor, and then restart the web server:

apachectl start

8. Next, we need to make sure the test CGI file is properly permissioned. Enter the following into your terminal session:

cd /Library/WebServer/CGI-Executables/
chmod 755 test-cgi

This will give everyone (important for the web user!) the ability to execute the cgi script.

9. Disconnect as root and end your terminal session by typing exit twice.

To test your CGI functionality, you can use the test CGI that's included with Apache. Launch a web browser, and enter this URL:

http://127.0.0.1/cgi-bin/test-cgi

If CGI has been enabled correctly, you should see something like this:
CGI/1.0 test script report:

argc is 0. argv is .

SERVER_SOFTWARE = Apache/1.3.12 (Darwin)
SERVER_NAME = griffx
GATEWAY_INTERFACE = CGI/1.1
... etc etc etc.That's it! Now you can download and install CGI's that add things like bulletin boards, shopping carts, counters, ad managers, etc., to your webserver. Just be careful you do not get any Mac-style line breaks while downloading and installing the files - they will not work if this happens! See this article for all the details!

Installing CGI's is not overly difficult (you should be comfortable with the terminal and understand the basics of UNIX file permissions before starting). Be aware that there may be some malicious CGI scripts on the web, so make sure you trust the source before you install a third-party CGI!

W3iRd0
02-04-2002, 02:36 PM
A three part article on apache on Mac OS X (http://http://www.oreillynet.com/pub/a/mac/2001/12/07/apache.html). Enjoy.

orca498
02-04-2002, 02:45 PM
Originally posted by W3iRd0
A three part article on apache on Mac OS X (http://http://www.oreillynet.com/pub/a/mac/2001/12/07/apache.html). Enjoy.

Thanks W3iRd0, I've seen this article. It's what got me this far, but not quite all the way. Again, thanks!

gboy3223
02-04-2002, 07:33 PM
I don't know if this helps but I had a similar problem and found that I actually had to have a connection to make it work

orca498
02-04-2002, 07:40 PM
Still no go, but thanks.

rudar
02-05-2002, 12:42 AM
the apache.conf file those directions tell you to look for in /Library/WebServer/Configuration/ looks suspiciously like the httpd.conf file that I found in /etc/httpd/

I guess I would first check (using either the gui or the cli) that there is in fact no folder 'Configuration' in /Library/WebServer, then check (using the cli unless you've enabled viewing all the system files in the gui; I believe that's possible but not sure how...) whether there's a /etc/httpd/httpd.conf files, and if so, edit that one instead.

Then again, I've spent about since Friday morning trying to get PHP to access postgres databases, so I'm not sure I'd trust my advice...

Dr-NiKoN
02-05-2002, 01:42 AM
CGI-support is enabled by default in OS X's Apache.
Try this:
cd /Library/WebServer/CGI-Executables/
edit a file:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print <<"nikon";
<HTML>
<HEAD>
<TITLE>CGI works out of the box :)</TITLE>
</HEAD>

<BODY>
<H1>Yes.</H1>
</BODY>

</HTML>
nikon
Save the file, and then:
chmod 755 filename.cgi
Then, in your web-browser enter the adress localhost/cgi-bin/filename.cgi

Any other CGI-script you make works the same way.
PS: You need to put your CGI's in /Library/WebServer/CGI/Executables/, if you want to be able to run CGI's from any folder, then you need to reconfigure apache.

Let us know.

nikon

orca498
02-06-2002, 02:20 AM
Well now I seem to be able to create a script and run it. If I use #! /bin/sh as the shebang line.

Nikon, I tried using the code you sent, but it didn't work. Now, I'm a newbie so I'm not too sure if it was something I did or what. I know that when I tried running the script I got a blank page, and when I tried running it in the terminal it had trouble with the: <<nikon and the nikon at the end.

New Question: I thought I saw a post that said something about being able to modify the httpd.conf file so that: say you have a .pl file in your cgi directory but someone types in filename.cgi it will still execute? It basically tells the cgi directory that anything in it is a CGI script.

I searched my little narrow behind off, but can't find it. Any ideas?

As usual I thank you in advance.

W3iRd0
02-06-2002, 02:50 AM
The CGI-Executables directory is ScriptAliased as cgi-bin as a default. That means that everything inside of it is treated as scripts. You can use redirects to accomplish the trick you mentioned.

ATTENTION:
scripts that start with #!/bin/sh or similar are shell scripts, as far as I know, not very good for websites.

The code you can't execute is a perl script, are you sure perl is installed and/or enabled in your apache setup?

Dr-NiKoN
02-06-2002, 06:43 AM
"when I tried running it in the terminal it had trouble with the: <<nikon and the nikon at the end. "

The use of nikon was just an example. It tells the script where the html-stuff starts and ends.
You could replace nikon with almost anything.
The important thing is that the last nikon(the end of the html-part) is on a line by its own, and is ended with a carrier-return(must be a carrier return, no spaces or anything).

As w3irdo said, #!/bin/sh isn't the way to go.
Learn a decent CGI-language first, or steal your scripts :)

If you think the problem is with perl itself just write the following:
which perl
This should output something like:
/usr/bin/perl
And, you use that output in the first line of your script.

Try reading this:
http://httpd.apache.org/docs-2.0/mod/mod_alias.html#redirectmatch
You should also read the whole mod-cgi page:
http://httpd.apache.org/docs-2.0/mod/mod_cgi.html

nikon