Setting up virtual hosts on OS X Leopard
Tuesday, March 11th, 2008A couple of months ago I explained how to set up PHP 5 on OS X Leopard - it was very easy! We also configured PHP so that the default folder was your Users/home folder, instead of Library/Webserver.
If you have lots of sites to test, you might want to be able to type a url into the browser, and see the site directly. To do that you need to modify your Mac’s hosts file, and set up a Virtual Server.
1 - Preparation
Decide upon the URL you want to use, I’m going for mark-kirby.dev in this example.
Decide where you want the files to go that will appear for this url, and place them there on your computer. I’m using /Users/username/Sites/mark-kirby for this example.
2 - Edit the hosts file
Open the file
/private/etc/hosts
Since this file is hidden, you will need to need to check the option “open hidden files” if your text editor has one, or use the terminal to open the file directly.
mate /private/etc/hosts
Alternatively, you could:
- Open finder
- click on go
- select “Go to folder”
- enter in /private/etc/
You will see the following:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost
Underneath this, enter your local IP address (127.0.0.1) and the name of the site you want to set up, e.g.
127.0.0.1 mark-kirby.dev
Save the file - you will have to authenticate - and now when ever you type http://mark-kirby.dev into a browser it will give you your local folders - EVEN if mark-kirby.dev is an actual website on the internet. Browsers always check the hosts file first.
2 - Add the virtual host in apache
If you type mark-kirby.dev into your browser now, you will still see your default folders, you need do some more work.
Open the file
/private/etc/apache2/extra/httpd-vhosts.conf
using the methods described above.
You will see this first -
# # Use name-based virtual hosting. # NameVirtualHost *:80
Don’t touch it!
Then you will see this -
# # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <virtualhost> block. # </virtualhost><virtualhost *:80 > ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/www/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common </virtualhost>
<virtualhost *:80 > ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/www/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common </virtualhost>
First I’ll quickly explain what each line means:
- ServerAdmin - the email address of the server admapachestrator e.g. mark-kirby@mark-kirby.dev, this will show up on error pages
- DocumentRoot - the location of the files to show e.g. /User/username/Sites/mark-kirby
- ServerName - the domain name you want to use e.g mark-kirby.dev
- ServerAlias - an alternative name which will point to the same place e.g. www.mark-kirby.dev
- ErrorLog - the location of a log file to output errors to, e.g. “/User/username/Sites/logs/mark-kirby-error-log”
- CustomLog - the location of a file where all other logs should be written, followed by a format - just stick with common - e.g. “/User/username/Sites/logs/mark-kirby-access-log common”
So really, you only need DocumentRoot and ServerName if this is a test server only you will access.
Firstly comment out the examples - very important, don’t miss this step!
Then add a default entry so localhost will still take you to the root of your Sites folder:
<virtualhost *:80 > DocumentRoot "/Users/username/Sites" ServerName localhost </virtualhost>
Finally, add your entry, e.g.
<virtualhost *:80 > DocumentRoot "/Users/username/Sites/mark-kirby" ServerName mark-kirby.dev </virtualhost>
Save this, you’ll need to authenticate.
3 - Activate virtual hosts in apache
If you haven’t done so, by default the file you just edited, won’t be enabled in Apache.
To change this open up another hidden file - /private/etc/apache2/httpd.conf
Find the line
# Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf
And uncomment the include so it looks like this:
# Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf
Save the file - you may need to authenticate.
4 - Restart Apache
This still won’t work, until you restart apache.
To restart apache:
- Go to system preferences
- Select “sharing”
- Uncheck the box “Web Sharing” - apache will stop
- Check it again - apache will start
Test your site - you should be in luck!
5 - Permission problems
If you have any problems with permissions on your new site, you could try Jeremy Keiths solution - he adds a directive to his httpd-vhosts.conf file which allows everything, it looks like this:
<directory /Users/*/Sites/> Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews AllowOverride All Order allow,deny Allow from all