To begin adding dynamics to your local Website(s), one of the primary tools you might use is Perl. This is included in the standard OS X install and requires only a few short steps to enable cgi scripts to run on your localhost.
In the /Library/WebServer/CGI-Executables/ directory, you’ll find some included test scripts. By default, an alias to this directory can be found in Apache’s httpd.conf file as cgi-bin. To enable scripts to also be available to individual users on the system, you will need to also find and uncomment the following line in httpd.conf:
# AddHandler cgi-script .cgi
Note that you can also add an additional line as that shown above, but with a .pl extension, to support using .pl files with, or in place of, the .cgi extension.
As a security warning, I should mention that this does allow cgi to be executed anywhere Apache serves documents. This may not be an issue for localhost testing, but carefully think out your security strategy if you will be enabling Internet access to any of these local sites on your system.
To test your CGI capability, load one of the test scripts in your browser, i.e. http://127.0.0.1/cgi-bin/test-cgi (provided by Apple in the CGI-Executables directory). If you receive a forbidden error, it is due to permissions not being set. All CGI scripts need execution rights, the most common being 755. This can be done by using the Terminal to the location of the CGI script and running CHMOD 755 filename.cgi, or in this case, test-cgi. In this case we will open the Terminal and issue the following commands:
sudo chmod 755 test-cgi
Upon revisiting the script in your Web browser, the code will carry out its responsibility and you should see something similar to the image below.
Once you have successfully executed a test script, you can either place Perl scripts in the CGI executables directory (remember to set your permissions), or, if you have enabled scripts to be run in the Users/shortname/Sites folders, you can place them there.
As you can see from these first two sections on Apache and Perl, not much is different on OS X, other than a few file locations. It’s the same scenario with PHP. You can either install PHP from source, download it from php.net, or you can use a binary install packaged designed especially for OS X.
Apple had noted on the Apple Developer Connection Website that, previous to PHP version 4.3, installing from source could be a little tricky; however, PHP has resolved these issues with the latest release. For those that do prefer the convenience of installing PHP from a binary (.pkg file), Marc Liyanage has a Website that provides a binary of the latest release, which is compatible with both OS 10.2 and the new Panther, 10.3. This can be downloaded here. You will find that the binary offers the convenience of built-in support for numerous configurations, including MySQL (and PostgreSQL), PDF and XML.
Getting MySQL onto your system is just as easy! MySQL provides a binary package installer for OS X that can be found here. Just be sure to select the download from the list of Mac OS X Package Installer downloads.
Once it’s installed, you can manage MySQL from the Terminal utility with traditional mysql command line actions, or via an add-on tool from third parties. This author uses Webmin (free from webmin.com), which offers browser-based control over not just MySQL, but Apache, Sendmail, and Perl modules, too.
Dynamic Web Serving Ready to Go
As you’ve seen, getting your Web server up and running is quick and easy on OS X, including the ability to test your scripts and even entire Web applications locally. Below, I’ve included some closing notes on leveraging Python and CVS on your OS X system.
Installing Python on OS X
Python 2.2 is included in the BSD subsystem of OS X; however, many new open-source applications are using a newer build of Python. There is a MacPython build with special OS X features available here.
Using CVS and OS X
One final reference note for those developers who may be using CVS for source control: OS X has native support for CVS in both single user and multi-user environments. There is an excellent article on using CVS for Web development on the Apple Website. Using CVS does entail some ‘under the hood’ use of OS X via the included Terminal utility for command line operations.