Flangy > Software Development > Python CGI Development

Updated September 10, 2003
Send comments to

Contents:

Setting up Mac OS X to run Python CGIs

I have a PowerBook running OS X that I use as a web development machine. I can run the same servers that our Linux production machine runs (Apache, MySQL) and I can run Photoshop for graphic editing, which is a pretty good deal.

In order to set up a stock OS X install to run Python CGIs you need to do some extra steps, which is what this document is all about. Note that much of this is applicable to getting Python CGIs running on other UNIX-style environments too.

Configuration Steps

  1. Upgrade to latest OS release (optional)

    As of Sept 2003 I'm running 10.2.6 with various security updates.

  2. Turn on Apache

    The easiest way to get Apache running is to open System Preferences and go to Sharing. From there start Personal Web Sharing. This UI sets up Apache to launch when you boot your machine.

  3. Install Python

    You have a few options for getting Python on your machine.

  4. Add domain names for your sites

    I use name-based virtual hosts to run multiple domain names off my laptop. For each real domain I add a .dev domain to OS X's NetInfo database.

    In NetInfo Manager, click on the lock at the bottom. Go to /machines/localhost and select Edit | Duplicate from the menu (command-D.) In the copy, double-click the value of the 'name' property and change it to your dev domain. Duplicate a new entry for each domain you want to set up.

    Alternatively, you can get some command line scripts that modify the NetInfo database.

  5. Pick folders for your sites

    Files have to go somewhere, of course. I made a top-level /www folder, with a directory for each domain. This lets me take some apache setup shortcuts on the /www directory.

  6. Configure Apache

    Load up /etc/httpd/httpd.conf in your favorite text editor.

    1. Enable the CGI handler

      Look for the section:

      # To use CGI scripts:
      #
      #AddHandler cgi-script .cgi
      

      Uncomment and add whatever extensions you plan to use for your scripts:

      AddHandler cgi-script py

      I prefer to keep the .py extension for Python CGI files, and then use mod_rewrite to hide script extensions [see].

    2. Enable name-based virtual hosting
      #
      # Use name-based virtual hosting.
      #
      NameVirtualHost *
      
    3. Add Virtual Hosts

      Set up a VirtualHost for each domain you added to NetInfo:

      <VirtualHost _default_>
        ServerName conversatron.dev
        DocumentRoot "/www/conversatron"
      
        <Directory "/www/conversatron">
          Options Indexes FollowSymLinks ExecCGI Includes
          AllowOverride All
        </Directory>
      </VirtualHost>
      

      Make sure that Options includes ExecCGI.

      If all of your domains have a common parent and you want to shortcut the setup a bit, you can replace the VirtualHosts' Directory blocks with one on the parent folder.

      This Directory block should go above the VirtualHost blocks.

      <Directory "/www">
        Options Indexes FollowSymLinks ExecCGI Includes
        AllowOverride All
      </Directory>
      

      Then your VirtualHost blocks can look like:

      <VirtualHost _default_>
        ServerName adamv.dev
        DocumentRoot /www/adamv
      </VirtualHost>
      

      I prefer to put my virtual host information in a separate file vhosts.conf and put this at the end of httpd.conf:

      NameVirtualHost *
      Include /etc/httpd/vhosts.conf

      I also set up a domain www.dev that just has links to the roots of all the development sites on my laptop. I make a domain for this instead of putting it at localhost so I can have a different page come up for people who Rendezvous into my box via Safari.

      The Rendezvous module seems to attach itself to the first virtual host specified if virtual hosts are turned on. (Virtual hosts are off by default on a new OS X 10.2.x install.)

      To set up a link page at www.dev and a Rendezvous page at localhost, after the <Directory "/www"> block in your vhosts setup add these hosts:

      <VirtualHost _default_>
      	ServerName *
      	DocumentRoot /www/localhost
      </VirtualHost>
      
      <VirtualHost _default_>
      	ServerName www.dev
      	DocumentRoot /www
      </VirtualHost>
      	

      Change /www/localhost to wherever you want your default webpage to live. The stock default webpage is the "welcome to Apache" page at /Library/Webserver/Documents/.

  7. Restart Apache

    Restart Apache from Terminal: sudo apachectl restart

    If there was an error you can use apachectl configtest to test the config files.

 

Troubleshooting

To debug a script error you need to look at your httpd error log. The default place for this is /var/log/httpd/error_log. At the command line you can use the tail command to view the end of the log. In the OS X GUI you can use the Console app to open the error log. Problems below will be given in terms of the errors in the log file.

Additional Tricks

 

The End.