Combo Pack: CakePHP and Leopard Virtual Hosts

I've been checking out CakePHP, and while I haven't really fully explored it yet, I'm pysched to have learned how to get numerous test sites up and running under Leopard's default installation of Apache and PHP. It was a bit of an adventure, so I'm taking notes here that will a) remind me how I did it and (b) hopefully save somebody else some time. This assumes some technical skill at the command line, and use of a trusty text editor.

Step One: Get MySQL, PHP, and Virtual Hosts running on Apache

I'll be running all my applications from my home user "Sites" directory (~/Sites). If you haven't already done so, install MySQL - I followed these directions with a strong cup of coffee and got it working.

Next, I followed this excellent Cake Tutorial, which includes how to get PHP and Cake up and running (as well as the Cake Command Line tool), and begins to cover virtual hosts set-up (which we'll modify a bit later). It also runs over how to get MySQL up and running, but I didn't try it as I already had it installed. Getting PHP and VHosts up and running is as easy as commenting out two lines in httpd.conf.

Note that the Cake tutorial will say that you'll be able to view the website right after editing httpd-vhosts.conf - this is not true. You will need to complete the next step and edit the /etc/hosts file as well, and then you can view the site.

Why Virtual Hosts Are Cool

Because you can simulate domains on your local computer. For example, you can create and view a development site like http://www.caketest.dev right in your browser, with full PHP/Database functionality, as opposed to something less fun like http://127.0.0.1/~username/caketest - this sort of URL breaks many of CakePHP's references, which means your external CSS and JavaScript files won't get loaded.

Give Me More Domains, Dammit

OK, by now you might want to have several testing domains setup - a great way to avoid trying out new things on a live website. After a bunch of research and experimentation, here's how I got it working.

For reference, my user folder is "mmayes" - I'll be referring to it in the following commands.

We'll need to open some buried files, so let's use the command line. The "mate" command here opens the file in Textmate, but you can use vi, vim, emacs, nano, etc.

sudo mate /etc/hosts
sudo mate /private/etc/apache2/extra/httpd-vhosts.conf

Stop web sharing in System Prefs -> Sharing.

I have two cake test sites up and running: caketest and cakeblog. Here's what my httpd-vhosts.conf looks like. Note the <Directory ... > options are to allow .htaccess to work:

NameVirtualHost *
 
# default folder when none specified
<VirtualHost *>
  DocumentRoot "/Users/mmayes/Sites"
</VirtualHost>
 
<VirtualHost *>
  DocumentRoot "/Users/mmayes/Sites/caketest"
  ServerName caketest.dev
  ServerAlias www.caketest.dev
  <Directory "/Users/mmayes/Sites/caketest/">
    Options FollowSymLinks
    AllowOverride All
  </Directory>
</VirtualHost>
 
<VirtualHost *>
  DocumentRoot "/Users/mmayes/Sites/cakeblog"
  ServerName cakeblog.dev
  ServerAlias www.cakeblog.dev
  <Directory "/Users/mmayes/Sites/cakeblog/">
    Options FollowSymLinks
    AllowOverride All
  </Directory>
</VirtualHost>

And here's what /etc/hosts looks like:

### 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
127.0.0.1   www.cakeblog.dev
127.0.0.1   www.caketest.dev
255.255.255.255    broadcasthost
::1             localhost
fe80::1%lo0    localhost

Restart web sharing in System Prefs, and you should be able to view each domain.

I assume to add more domains, I can just proceed along these lines. Hope this makes sense! Cheers.

References:
Apache 2 Only Servers First Virtual Host | alexking.org
Installing CakePHP on OS X Leopard | KeithMedlin.com
Installing MySQL on Mac OS X | HiveLogic.com
CakePHP.org

UPDATE 11/11/08:
To get your Mac virtual hosts working on your Parallels Desktop machine, follow these easy instructions.

This entry was posted in Apache, OS X, PHP. Bookmark the permalink.

9 Responses to "Combo Pack: CakePHP and Leopard Virtual Hosts"

Leave a reply