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.
I have done my best to follow these instructions exactly, but I keep getting “Warning: Invalid argument supplied for foreach() in …/app/views/gallery/index.thtml on line 7″. Any idea what this might be?
Hi there - couple things:
1. Are you using Cake 1.1 or 1.2? This looks like a 1.1 setup - might want to switch over to 1.2 as things have changed quite a bit and you might as well start learning the newer version.
2. It sounds like Cake is spitting out a template error, which makes me think this is a Cake issue rather than a virtual host setup issue. Anyways, make sure that what you are passing into the foreach loop is indeed an array, and that the array is being passed correctly from the controller. If you set your debugging level to 3 (please see the manual on this), then you can see a dump of all variables and values being passed in from your gallery controller.
Hope that helps!
Hi there
Im new in Cakephp, I downloaded cakephp 1.2 version since its recommened to dowload it and its new.Im runnging everything under XAMPP(MySQL/PHP/Apache).I’ve been following the Blog tutorial on Cookbook:http://book.cakephp.org/view/326/the-cake-blog-tutorial. I’ve done all the steps now,
Im getting this error now:
http://localhost/cake/app/views/posts/index.php
Can somebody help me. As I am so new in Baking. How do I resolve this problems?
@Darkie - could you please post the full error message? Also, did you try to get your virtual hosts setup?
here is the whole error: Id Title Created
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\cake\app\views\posts\index.php on line 9
what do you mean about about virtal host setup? Im not clear.
@Darkie - the virtual hosts is largely what this article is about, but you’re on a PC so that doesn’t matter as this article pertains to OS X. I would post your issue to the CakePHP Google group - you’ll likely have some better luck diagnosing there.
Thanks. This is exactly what I’ve been looking for for the past week.