Configuring OS X for Web Development Article

In a previous article, 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.

1263_image1

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.

Go to page: 1 | 2

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.