You need to update you httpd-vhosts.conf
to something like this:
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/www.site1.dev"
ServerName www.site1.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/www.site2.dev"
ServerName www.site2.dev
</VirtualHost>
Having each domain separately in your config allows you to have separate server options for each, e.g. Rewrite config, separate log files, etc.
Then, update your /etc/hosts
and add this at the end:
127.0.0.1 www.site1.dev
127.0.0.1 www.site2.dev
This step is particurarily important as this is where your OS looks first when resolving domain names. Otherwise it will not know what www.site1.dev is.
Then simply restart the server by disabling Web Sharing and enabling it again.
To make sure your config works you can check apache logs (access_log
and error_log
) via Console.app (Files > /private/var/log > apache2).
When accessing websites on your "localhost", there are several permissions which are required. The localhost content may be in your Sites directory, in which case, these commands may help prevent any "Forbidden" messages.
Ensure the Users directory allows read directory access:
cd /
sudo chmod -v 755 Users
Ensure the username directory allows read directory access:
cd Users
sudo chmod -v 755 username
Ensure your Sites directory allows read directory access:
cd ~
chmod -v 755 Sites
Every subdirectory of Sites needs read access:
cd ~/Sites
find ~/Sites -type d -print -exec chmod 755 {} \;
Every file in Sites and subdirectories needs read access:
cd ~/Sites
fing ~/Sites -type f -print -exec chmod 644 {} \;
Apache uses the _www group so, to give Apache full access to everything in the Sites directory, set the extended attributes with this:
chmod -R +a "group:_www allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit" ~/Sites
Best Answer
This is called a "Virtual Host". Here's how I set them up:
Edit
/private/etc/apache2/httpd.conf
, and changeto be:
Edit
/private/etc/apache2/extra/httpd-vhosts.conf
and add an entry that's something like this:Edit
/private/etc/hosts
and add this entry:Restart your webserver (I use
sudo apachectl restart
)There are some example virtual host definitions in the
httpd-vhost.conf
file that are there to show you what the syntax looks like. They look like this:Add
#
signs to the beginning of those lines to comment them out.An alternative to this is to use an app like VirtualHostX to do this for you ($35).
Edit 5 Dec 2011:
Here's a new blog post by the makers of Alfred on how they set this up:
http://preppeller.com/2011/12/04/setting-up-virtual-hosts-on-your-local-os-x-apache/