Mac OS X Web sharing, .cgi scripts not executing: showing up as plain text
Alec Jacobson
March 26, 2011
I wanted cgi scripts to be executed everywhere in the website I am hosting from a macbook pro. The default, is to have cgi script only execute inside /Library/Webserver/CGI-Executables and not in the main site /Library/Webserver/Documents. So if you have a test script saved in test.cgi:
#!/bin/bash
echo "Content-type: text/html";
echo "";
echo "Hello";
You can put it in /Library/Webserver/CGI-Executables. Then change its permissions:
chmod 755 test.cgi
And see the result at http://localhost/cgi-bin/test.cgi.
If you want to have .cgi scripts run anywhere then you need to change your apache configuration (mine is located at /private/etc/apache2/httpd.conf )
Make sure this line is uncommented:
AddHandler cgi-script .cgi
And be sure that ExecCGI is added to you options for the /Library/Webserver/Documents directory like this:
<Directory "/Library/WebServer/Documents">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks MultiViews +ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
Now if you move your test.cgi script to /Library/Webserver/Documents you should be able to see it in action at http://localhost/test.cgi.
BUT for me the script just shows up in the browser in plain text. I searched and searched, but couldn't figure out what I'd done wrong.
The answer was simple. Restart Apache. You can do this by opening System Preferences > Sharing and toggling off and on "Web Sharing". Now everything works great!