Categorized: Apache, Apple

Updated for a fresh Snow Leopard install 2/26/2010.

The Absolute Least You Need to do to use Virtual Hosts on OS X Leopard

Enable Web Sharing in System Preferences > Sharing.

Edit these files:

  • /etc/apache2/httpd.conf
  • /etc/hosts
  • /etc/apache2/users/your-username.conf

(I keep these files in a TextMate project so I can get at them quickly and don’t have to remember which files I need.)

In httpd.conf, uncomment this line:

LoadModule php5_module        libexec/apache2/libphp5.so

In hosts, add your local sites in the format:

127.0.0.1   your-site-name.dev

In /etc/apache2/users/your-username.conf, add this line to the very top of the file:

NameVirtualHost *:80

Change None to All in this line (to allow for things like mod_rewrite):

AllowOverride All

Add FollowSymlinks to this line:

Options Indexes MultiViews FollowSymlinks

Add new sites in the same file in this format:

<VirtualHost *:80>
   ServerName your-site-name.dev
   DocumentRoot "/Users/your-username/Sites/your-site-name/"
</VirtualHost>

Any time you make changes to any of these files, you’ll need to run this command:

sudo apachectl graceful

Check out this script for an easy way to add new sites (and feel free to fork it and adapt it to your uses).

Source

Further reading

  • Refer to the 456 Berea Street article to see how to configure PHP for MySQL.
  • Installing MySQL

Comments

Jason Tan → March 3rd, 2009 at 4:00 pm

I recently created a script to add a new vhost:

#!/bin/sh

DIR=/Users/your-username/Sites/$1
if [ ! -d $DIR ]
    then
    sudo -u your-username mkdir $DIR
    sudo -u your-username touch $DIR/index.php
    echo "<h1>$1</h1>" >> $DIR/index.php
fi

VHOST=/etc/apache2/extra/httpd-vhosts.conf
echo "\n<VirtualHost *:80>" >> $VHOST
echo "\tDocumentRoot \"$DIR\"" >> $VHOST
echo "\tServerName $1.dev" >> $VHOST
echo "</VirtualHost>\n" >> $VHOST

echo "127.0.0.1\t$1.site" >> /etc/hosts

apachectl restart

mate $DIR

So now, when inspiration for a new site strikes, I just type:

sudo vhost my-new-idea

It updates the hosts and vhosts files (it assumes there is at least one line break at the end of both files), restarts apache, and launches TextMate.

Trey → March 3rd, 2009 at 4:46 pm

That is awesome. It would be cool if it could get your username without having to specifically enter it. You can at least save having to write it 3 times like this:

#!/bin/sh

ME=your-username
DIR=/Users/$ME/Sites/$1
if [ ! -d $DIR ]
    then
    sudo -u $ME mkdir $DIR
    sudo -u $ME touch $DIR/index.php
    echo "<h1>$1</h1>" >> $DIR/index.php
fi

VHOST=/etc/apache2/extra/httpd-vhosts.conf
echo "\n<VirtualHost *:80>" >> $VHOST
echo "\tDocumentRoot \"$DIR\"" >> $VHOST
echo "\tServerName $1.dev" >> $VHOST
echo "</VirtualHost>\n" >> $VHOST

echo "127.0.0.1\t$1.dev" >> /etc/hosts

apachectl graceful

mate $DIR
Morgan → March 3rd, 2009 at 5:32 pm

Greetings,

If you’re on Leopard, you don’t need to edit /etc/hosts. There’s a command line to interface with Mac OSX’s Directory Service.

sudo dscl localhost -create /Local/Default/Hosts/{hostname} IPAddress 127.0.0.1

Want to remove it?

sudo dscl localhost -delete /Local/Default/Hosts/{hostname}

– Morgan

Trey → March 3rd, 2009 at 5:43 pm

@Morgan

That’s interesting, but I don’t think that saves any time over adding a line in /etc/hosts with 127.0.0.1 whatever.dev. I suppose that might simplify Jason’s script, though.

Stefan → March 4th, 2009 at 4:02 am

Thank you! This is very useful. I added another line after opening TextMate, which opens the page in your browser:

open http://$1.dev/
Trey → February 1st, 2010 at 3:12 pm

And now a new version that just downloads from my site-template project and uses the $SUDO_USER environment variable to keep you from having to hard code your username in the file:

#!/bin/sh
# Original script by Jason Tan: http://solutions.treypiepmeier.com/2009/03/03/virtual-hosts-on-osx-leopard/#comment-41787

DIR=/Users/$SUDO_USER/Sites/$1

if [[ ! -d $DIR && `id -u` -eq 0 ]]; then
    echo ""
    echo "Downloading site-template repository ..."
    sudo -u $SUDO_USER git clone --quiet git://github.com/trey/site-template.git $DIR
    cd $DIR
    echo "Deleting unneeded parts ..."
    rm -rf .git/
    rm .gitignore
    mv html5.html index.html
    rm html4.html
    rm html4_lean.html
    rm xhtml.html
    rm xhtml_full.html
    rm README.markdown
    rm MIT-License.txt

    VHOST=/etc/apache2/users/$SUDO_USER.conf
    echo "<VirtualHost *:80>" >> $VHOST
    echo "\tDocumentRoot \"$DIR\"" >> $VHOST
    echo "\tServerName $1.dev" >> $VHOST
    echo "</VirtualHost>\n" >> $VHOST
    echo "VirtualHost created in $VHOST ..."
    echo "127.0.0.1\t$1.dev" >> /etc/hosts
    echo "/etc/host updated ..."

    apachectl restart
    mate $DIR
    open http://$1.dev

    echo ""
    echo "Done!"
else
    echo "usage: sudo treyvhost directory"
fi

What do you think about that?

Elsewhere in the empire: Home, Blog, APOD