Tik’s Blog

ร้อยแปดพันเก้า

Setting up Virtual Hosting on Leopard

leave a comment »

This is how I set up virtual hosting to point to a “virtual” domain on my localhost for development and testing.

The problem actually comes from having to set RewriteBase as mentioned in the previous post. This is very inconvenient, as the .htaccess being deployed in production will be different from the .htaccess on my machine. The one on my machine requires a RewriteBase to /~Me/cake, whereas the one on the production server does not require this.

Since all files are under version control, setting RewriteBase and undoing the changes when deploying is not much fun at all.

After digging around, I found one (probably overengineered) solution. Hell, it works now, so this is a reminder in case I need to do all this again.

  1. First thing is to enable virtual hosting in /private/etc/apache2/httpd.conf by uncommenting the line “Include /private/etc/apache2/extra/httpd-vhosts.conf”.
  2. Then edit httpd-vhosts.conf. The setting looks something like:

NameVirtualHost *:80

<VirtualHost *:80>

    DocumentRoot “/Library/WebServer/Documents”

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot “/Users/Me/Sites/cake/”

    ServerName production

</VirtualHost>

The first VirtualHost block is the default host. I added this so that the old behavior works (as recommended in Apache’s documentation).

The second VirtualHost is the new “virtual” domain for local use. The domain name is “production”, so I can access the special web site using “http://production/”.

Since “production” is a domain name I made up, I must define a special lookup in /private/etc/hosts by adding a line to the file: “127.0.0.1 production”. What this means is when the OS looks up the domain “production”, it will resolve to IP 127.0.0.1.

Restart Apache by “sudo apachectl restart” and that should do it. Pointing to “http://production” should now show your CakePHP app site. RewriteBase is no longer needed. Deploying should be a bit easier.

Written by sukita

พฤษภาคม 1, 2008 ที่ 5:12 pm

บันทึกโพสใน Uncategorized

ใส่ความเห็น