Running Python Programs on the macOS / Mac OS X Apache Web Server

The macOS / Mac OS X operating system includes a pre-configured Apache web server and also includes the libraries needed to run Python. Thus, Python CGI scripts can be run without any configuration changes to the Apache configuration as long as the scripts are placed in the correct location and given the correct file permissions.

The default location to place CGI programs for the Mac apache installation is in the following directory:


Files placed in this directory can be accessed via the following URL:


In order for Apache to be able to execute the CGI script, the permissions on the file must be set correctly. For example, open EditRocket and create a file called with the following contents. Save this file to the /Library/WebServer/CGI-Executables directory.

print "Content-type: text/html"
print "<html><head>"
print ""
print "</head><body>"
print "Test Page"
print "</body></html>"

Open a terminal window (Applications -> Utilities -> Terminal). Execute the following command:

cd /Library/WebServer/CGI-Executables

Then execute the following (if prompted, enter your password)

sudo chmod 755

You now have a Python program,, saved with the correct permissions in the /Library/WebServer/CGI-Executables directory.

Now, we need to restart the web server to make sure the changes take effect. To restart, do the following:

1. Go to System Preferences -> Sharing View

2. Uncheck the Web Sharing box. Then check the Web Sharing box again. This will start and stop the apache web server.

You can test this page by entering the following in your web browser: