Using Python scripts with Apache on OS X El Capitan

If you've got the Apache web server software working under OS X El Capitan, but want to use Python scripts with Apache, the following steps will allow you to run Python scripts from a cgi-bin directory.

First, ensure that the hash sign (#) is removed from the LoadModule cgi_module libexec/apache2/mod_cgi.so in /etc/apache2/httpd.conf. If you need to remove the hash/pound sign, restart Apache after modifying the file, which you can do with sudo apachectl restart. You will also need to use the sudo command to edit the file, if you edit it with a text editor such as GNU nano or vi.

The default configuration for Apache on OS X allows you to execute scripts that are placed in the /Library/WebServer/CGI-Executables/ directory. This is configured through the ScriptAliasMatch line in the following section of the httpd.conf file.

    #
    # ScriptAlias: This controls which directories contain server scripts. 
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #
    ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"

You can place a test Python script, e.g., pythontest.py, containing the following code in that directory:

Advanced Mac OS X - Technical and Security Skills
Advanced Mac OS X
Technical and Security Skills
1x1 px

#!/usr/bin/python

print "Content-type: text/html"
print
print """<!DOCTYPE HTML>
<html lang="en">
<body>
<h1>Hello World!</h1>
</body>
</html>
"""

The file system permissions on the file need to be set to allow the file to be executed by Apache - see Linux File Permissions (the same permissions apply on OS X/macOS as Linux). You can give the owner of the file read, write, and execute permissions and all other accounts read and execute permission using the chmod command with chmod 755 /Library/WebServer/CGI-Executables/pythontest.py.

Note: Be sure not to omit the second print command that appears in the above code immdediately after the print "Content-type: text/html" line, since the first line of output should be a header line followed by a blank line and then the HyperText Markup Language (HTML) code - see run python script as cgi apache server. If you omit that line, instead of seeing "Hello World!" output, you will see a page with the title "500 Internal Server Error" and text displayed in your browser similar to the following text:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at john.smith@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

And if you look in the error log for Apache, you will see the errror message "End of script output before headers". E.g.:

[Mon Feb 06 21:51:56.161511 2017] [cgi:error] [pid 42331] [client ::1:61820] End of script output before headers: pythontest.py

If you wish to create a Common Gateway Interface (CGI) directory for a virtual host as explained at Running an Apache web server under OS X El Capitan, you can modify the httpd.conf file to enable virtual host support by removing the hash sign (#) from the line below that is found in httpd.conf:

#Include /private/etc/apache2/extra/httpd-vhosts.conf

You can then edit the /private/etc/apache2/extra/httpd-vhosts.conf file to create a virtual hosts section suitable for the website you wish to support with Apache and create a cgi-bin directory beneath the directory you specify for DocumentRoot in the modified httpd-vhosts file.

You can then place your Python scripts in that directory to have Apache execute them. E.g., if the virtual host is myserver.example.com with a cgi-bin directory below the document root directory, I could place the pythontest.py file there and execute it with http://myserver.example.com/cgi-bin/pythontest.py.

Related articles:

  1. Running an Apache web server under OS X El Capitan
  2. PHP for Apache on OS X El Capitan
  3. Using Perl with Apache under OS X El Capitan