zen of coding

Run multiple CakePHP apps side-by-side

  • 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:

NameVirtualHost 127.0.0.1

<virtualHost 127.0.0.1>
    DocumentRoot C:\cake\app\webroot
    ServerName app

    <directory &quot;C:\cake\app\webroot&quot;>
      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)…

  1. 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.
  2. 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/
  3. 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:

  1. A new skeleton (empty) app will be placed in C:\cake\mousetrap
  2. We’ll add a new entry to our hosts file: 127.0.0.1 mousetrap
  3. 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)
  4. Let’s not forget to change the ServerName to “mousetrap”
  5. Restart the web server
  6. 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.

  • Anton

    Why bother with hosts file editing, just drop all your apps to webroot and use apache index page to access them all quickly like

    http://yourhost/firstapp/
    http://yourhost/secondapp/

    and for every new project just cake bake in root with your project name.

  • @Anton

    That’s exactly what I wanted to (and prefer to) avoid, see third bullet in the very beginning ;)

  • Anton

    the setup is the same anyway but from my experience: if you have a team of 10 developers to follow and multiple projects to manage the configuration overhead that you suggest in this article becomes a hell pretty soon :)

    heres how i prefer it:

    1. svn checkout cake
    2. set cake in PATH
    3. cake bake /appname
    4. .htaccess (allow andy shady peter)

    in that way every developer who accesses the index can browse only apps allowed to them and i can even use the same user/password file for svn access also.It becomes easier to backup and restore your setups if you mess with config files as little as possible.

    but of course i should have read your third bullet more carefully :) and i can only suggest you a script that takes your app dirs and prints out or rewrites a separate config file that you include to your main apache configuration to automate things for you :)

    it’s odd that you hate subfolders :D as with host file as you have to find it in subfolder of a subfolder of a subfolder etc :D

  • @Anton

    Actually I have a shortcut to the hosts file in my quick launch bar ;)

    Either setup works just fine… and after all it’s a matter of preference (I like brunettes, some like blonds) :)

    Thanks for sharing your quick steps to setup, hopefully it will give whoever is reading this more food for thought and ideas.

  • “Actually I have a shortcut to the hosts file in my quick launch bar”
    BRILLIANT! LOL! I can’t believe I never thought to do that…

    Also…I too prefer brunettes. :P

    For anyone that needs to have an alternative folder structure, you may want to check out David Golding’s video tutorial on running cake outside the root folder. http://www.davidgolding.net/screencasts

  • @Brendon Kozlowski

    Thanks for that link, can’t believe I haven’t seen those before… oh well, you live you learn :)

  • skiedr

    i use similar solution several years. but for each app we have just 2 parameters: host and path to webroot. so i want to avoid of so much record in virtual host apache config. i would like to have config with pairs of hosts and webroots. It this possible to implement with apache?

  • @skiedr

    Without the “Directory” setting, it doesn’t work quite well anymore. I recall it did at some point, but perhaps apache had changed their security settings. (Anyways, if there is a simpler solution, please share… a little less copy/pasting never hurt ;))

  • Pingback: Run multiple CakePHP apps side-by-side | Dailytuts.net - Daily tutorial for peoples()

  • Great website, very useful. Just noticed a minor display issue, there’s an unclosed tag on list item 2 of step 6.

  • @James

    Thnx. Fixed ;)

  • Zaka

    thanks for this nice post. this perfectly works for me

  • Pingback: Одно ядро CakePHP – много проектов « freedev.asia()

  • Simply Awesome and helpful, thanks.

%d bloggers like this: