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:
echo "Content-type: text/html";
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:
# 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
# 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
# Controls who can get stuff from this server.
Allow from all
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!