Configuring OS X for Web Development
About the Author
Blane Warrene
Blane is a writer and researcher focusing on Apple and Open Source technologies. Prior to this, he helped found a commercial software and consulting venture, and worked in the financial services sector as a director of technology and in varying technical roles. Blane maintains Open Sourcery: SitePoint's Open Source Blog.
By: Blane Warrene
December 17th, 2003
In a previous article [1], we laid out the framework for using OS X in Web development. With more than 60% of the world’s Web servers running Apache, having the same local, database-driven development environment can accelerate Web development, design and testing. In this piece, we will tackle getting an OS X systems configured for localhost Web development.
Getting Started
A default OS X install includes the majority of what you will need to get a basic Web server up and running. Since we previously discussed the LAMP, or Linux, Apache, MySQL and Perl/PHP, platform, this is the base configuration we will cover here. I’ll also include links at the end of this article to information on installing Python, as this is a growing and popular language for Web scripting, along with some notes on using CVS.
Apache
Your OS X system is somewhat pre-configured to run a static Website with Apache pre-installed and pre-configured. The primary Website document root, which would be accessed at http://127.0.0.1 (or http://localhost; which you use is up to you) is found in the /Library/WebServer/Documents/. You will find a number of index files in here, due to Apache’s ability to negotiate content for localization, i.e. serving up the same page in different languages. You may remove these files if you will be focusing on one language.
Additionally, each user on OS X can, by default, serve Web pages from their home directory using their short name, i.e. http://127.0.0.1/~shortname (i.e. Blane Warrene uses the shortname bwarrene). Content for these local sites should go in the local user’s existing Sites folder within their home directory (/Users/shortname/Sites/).
To enable browsing of these sites, you will need to start the Apache Web server, which is simply done in the System Preferences menu by selecting Sharing and turning on Personal Web Sharing. If you prefer to FTP your content locally, rather than copying the files into directories, you should also turn on FTP Access within this preference pane.

Apache on OS X Tip
As with Apache on Linux, additional modules or services can be added in, or Apache can be recompiled. These actions normally require that you restart Apache. You can return to the System Preferences menu, select the Sharing pane and simply stop and start Personal Web Sharing to restart Apache.
If you require multiple local sites under development at once, you can easily establish a folder per project in the Sites directory in your Home folder. You can then access these for testing via browser using http://localhost/~shortname/project1, http://localhost/~shortname/project2, and so on.
You can custom configure Apache to use custom urls, such as http://project1, or http://project2, with a combination of edits to the httpd.conf (found in /etc/httpd/httpd.conf) and Hosts files on your system, which can be found in /etc/hosts/. The rudimentary example below shows two sites.
Using Vi or your favorite text editor, edit the httpd.conf file as follows:
- Uncomment the NameVirtualHost line and replace the * with 127.0.0.1
- Add two virtual host containers:
<VirtualHost project1> ServerName project1 DocumentRoot /Users/shortname/Sites/project1 </VirtualHost> <VirtualHost project2> ServerName project2 DocumentRoot /Users/shortname/Sites/project2 </VirtualHost>
Save and close the httpd.conf file. This will entail an Apache restart, which is done in the System Preferences menu under the Sharing pane.
Finally, to ensure that we’re resolving locally, we will edit your local hosts file. Using the Terminal utility, issue the following command:
sudo vi /etc/hosts
After entering your admin password, press i for insert mode, move the cursor to the end of the file, and add these lines:
127.0.0.1 project1
127.0.0.1 project2
Then press the esc key, type :wq, and press return.
Your configuring is complete! You should be able to access these separate sites in your Web browser at http://project1 and http://project2. Remember to put some content in those directories when you first browse to them!
Some Final Notes on Apache
There are numerous features you can utilize to create a mirror test environment to your production Apache Web servers. Take some time to peruse the httpd.conf file, which contains substantial documentation that can assist you in enabling features like server side includes.
In addition, while we’re specifically discussing setting up OS X as a localhost Web server in this piece, you should note that it only takes a few steps to serve a Website directly from your workstation to the Internet for collaboration or testing activities. There are 2 pieces to this puzzle, assuming you have access to a static IP address and a broadband connection.
If you prefer to use a fully qualified domain name or sub-domain (i.e. test.domain.com), you will need to insure DNS service is pointing to your static IP address on your local workstation.
You will need to go into the System Preferences, select the Sharing pane, and ensure the “Network Address” section of this screen is correctly filled in with a publicly accessible IP address. Then stop and start Personal Web Sharing to restart Apache.
Perl
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:
CD /Library/WebServer/CGI-Executables
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.
PHP
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 [2]. You will find that the binary offers the convenience of built-in support for numerous configurations, including MySQL (and PostgreSQL), PDF and XML.
MySQL
Getting MySQL onto your system is just as easy! MySQL provides a binary package installer for OS X that can be found here [3]. 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 [4].
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 [5] 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.
[1] http://www.sitepoint.com/article/1245
[2] http://www.entropy.ch/software/macosx/php/
[3] http://www.mysql.com/downloads/mysql-4.0.html
[4] http://homepages.cwi.nl/~jack/macpython/macpython-osx.html
[5] http://developer.apple.com/internet/macosx/cvsoverview.html