- Working with a few different clients?
- Need to run a couple of apps on the same development server?
- Tired of dealing with sub directories? (i.e. www.example.com/app1/, www.example.com/app2/)
… Well then you’ve stumbled onto the right place.
In this quick, introductory how-to, I’ll show you my approach for using a rather simple setup to run as many CakePHP apps as you’d like on the same development server.
The directions provided below are for windoze (yeah, yeah… I know), but I’m sure you’ll figure out how to make the same approach work for your favorite OS.
Step 1 – Get your Apache/PHP/MySQL running
I prefer to use XAMPP to get my development environment up and running. To keep things simple (and to deal with some Microsoft crap), I like to install it into C:\xampp.
With a quick download and a couple of installer clicks, you should be good to go within a few minutes.
Step 2 – Grab the CakePHP core
Since we are talking about a development setup, I recommend you get the nightly (or HEAD revision) from SVN Git. If you don’t have a SVN Git setup, you can just download the nightly zip from cakephp.org.
Again, to keep it simple I install the CakePHP core in C:\cake\cake. The second “cake” dir is the actual location of where all of your core files/libs are located. (All of your apps are going to live under C:\cake, we’ll see that in a minute).
Step 3 – Get the first app prepared
Now that we have a web server a database and the CakePHP core installed, we are ready to get our first app up and running.
It’s a good idea to get the skeleton (or empty) app from cakephp.org as well, since every so often there are subtle changes made to the app files to keep up with the latest and the greatest. I like to setup an SVN checkout a Git repo to always get the latest “app” and then copy it over to the new location, that is, whenever I’m starting the work on a new application…
So, with that little preface, let’s assume you’ve got the skeleton app downloaded (or checked out from SVN cloned from Github) and you’ve placed it (as mentioned in Step 2) in C:\cake\app.
Step 4 – Add an entry to your hosts file
Ultimately we’d like to be able to access our app by going to http://app/, for this we need to add an entry to the “hosts” file. Please take a second to google where you can find this little file, which will certainly depend on what OS you are using.
That being said, all we need to do is add a new line, just like so:
127.0.0.1 app
Step 5 – Setup a new virtual host for the app
Last, but not least, we’ll use Apache’s virtual hosts for each one of our apps. By following this rather simple method we can get as many apps as our dev server can handle, all up and running without much pain.
Assuming that the location of our XAMPP install is as described above, the httpd-vhosts file will be found in C:\xampp\apache\conf\extra.
Let’s add the following to entry to this file:
<virtualHost 127.0.0.1>
DocumentRoot C:\cake\app\webroot
ServerName app
<directory "C:\cake\app\webroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</directory>
</virtualHost>
Let me quickly explain what’s going on here (although, I hope it is a little obvious)…
- The document root is where our images, javascript, css and other web accessible files are located. C:\cake\app\webroot is a standard cake app location, so we went ahead and specified it in our vhosts file.
- We gave our server a “very creative” name: “app”. This is also needed so we can go ahead and access the application by going to http://app/
- We’ve used the Apache’s “Directory” directive to specify some options for this app, namely AllowOverride All
And that’s about it… let’s restart (or start) the web server and if all went according to plan you should now be able to go to: http://app/ and observe CakePHP’s default homepage.
Step 6 – Getting additional apps up and running
So we’ve gone through all this trouble to get the first app running, but the good news is that setting up additional apps now should be a … piece of cake (pun intended).
Let’s say we are now ready to get a new app setup called “mousetrap”.
Just repeat steps 3 – 5, and you’ll be ready to rock.
Let’s sum things up really quickly:
- A new skeleton (empty) app will be placed in C:\cake\mousetrap
- We’ll add a new entry to our hosts file: 127.0.0.1 mousetrap
- Copy/paste our existing entry for “app” in the vhosts file and modify the relevant paths to C:\cake\mousetrap (Note, NameVirtualHost is only needed once in the file, so don’t copy that part)
- Let’s not forget to change the ServerName to “mousetrap”
- Restart the web server
- Go to http://mousetrap and see the new app ready to go
And that’s the end of this story… Hopefully the approach that I’m using for my dev environment makes sense and serves as good reference for CakePHP beginners and those that are starting to deal with multi-app setups.