Multiple CakePHP applications on a single domain in a shared LAMP hosting environment
- One domain on a shared Linux/CPanel hosting environment
- Multiple CakePHP applications running from separate directories WITHIN the public webspace (referred to as public throughout this tutorial)
- Separate databases
- Single shared CakePHP core served from above the public web space (referred to as private throughout this tutorial)
- Separate CakePHP app directories (within a single APPS directory) also served from above the public web space (referred to as private throughout this tutorial)
- Given all of the above: NO STUPID WORKAROUNDS
BACKGROUND:
I first worked out this process on a WAMP machine, and I may cover that in another article. But since the actual end game is for my public space on the web (shared LAMP / CPanel hosting), I am going to cover this first. Specifically I use WAMP for development and testing, LAMP for publishing.
My LAMP hosting is with http://www.NetworkRedux.com. They have mod_rewrite already available. NO FURTHER modifications to the hosting environment were made by me other than what is outlined here, the basis for which is already in the Cake PHP manual.
As you go through this and build your application, notice that the models, controllers, and views folders are all in the APPS/cakeApp1 folder in the private server space above the public webspace. The /APPS/ directory will be shared by all the CakePHP applications as a common home.
ACTION ITEMS:
TEN STEPS TO DEPLOYING YOUR FIRST APPLICATION (cakeApp1)
- Download the current CakePHP distribution.
- Unpack - for this example, we will be creating an application called cakeApp1.
- Create three folders to split up the files from the distribution.
?zw(u)b&kܢjGޝ+%u{hʋqXZ^v+-&*'rr{h+ڵ- cake (this goes ABOVE the public webspace)(private and shared with all CakePHP apps)
- APPS/cakeApp1 (this goes ABOVE the public webspace)(private and specific to this application)
- cakeApp1 (this goes INTO your public webspace)(public and specific to this application)
- Open (from the private side) /APPS/cakeApp1/config/database.php.default (or database.php if you have previously setup your database config)
- Edit class DATABASE_CONFIG with your connection info for the current application.
This last step is what points THIS app to a particular database. When you setup the next app, the info here can point it to a separate database on the same server - different apps / different source folders / different databases / one hosting account on a shared public server
Don't forget that the models, controllers, and views folders are all in the APPS/cakeApp1 folder in the private server space above the public webspace.
FOUR STEPS TO DEPLOYING A SECOND APPLICATION (cakeApp2)
- Put above the public webspace (private): Download code
?zw(u)b>[+a۶bsynnXr^tɕѥ̽ݹ}ĽЈݹ屔而(4Ȁ4Ȁ왹왹왹켹х4Ȁ왹왹왹콍4Ȁ왹왹왹콍̹4Ȁ왹왹왹콙٥4Ȁ왹왹왹콙4Ȁ왹왹왹콥4Ȁ왹왹왹콥4Ȁ왹왹왹콩4Ȁ왹왹왹4Ȁ(
/APPS/cakeApp2
/config
/controllers
/index.php
/models
/plugins
/tmp
/vendors
/views
- Edit /cakeApp2/index.php in the public webspace to have the correct server paths as you did in cakeApp1 above (step 8).
- Edit /APPS/cakeApp1/config/database.php (from the private side) to contain your database connection information.
Enjoy your cake!
Jeff Keith, aka Dunnottar
Comments
Comment
1 Autodeploying multiple applications
I have another problem - I need to develop a Cake application which would deploy separate instances of itself for each user.
The simplest example - an imagined blog service where every user gets his own instance of a blog app under "/blogs/
I think I know how to do so with a script that would create user directories and populate them with copies of a sample application (adjusting config files on the fly). But there is a problem of maintainability - if in future I'd change a controller or a model in the main sample app, I don't want to repeat that fix for each user instance.
Currently I have two solutions - the first is a script for re-populating all instances to keep them in sync with the sample app after it is changed. And the second one - to use Linux files linkage instead of copying script files. I don't like both and hope maybe there is more elegant and efficient way to do so.
Sorry if it's a dumb question, I am new in Cake programming.
Question
2 How about multiple cake apps under a subdirectory
Thanks for your article!
Do you or anyone have any suggestion on how to develop/deploy multiple Cake apps under a subdirectory of the http document root? I need to have a structure like this (showing on the browser address bar):
("/" refers to the document root of a single domain, i.e. what's shown on request of http://sampledomain.com)
/subdir
/cakeapp1
/mainapp
/subapp
/cakeapp2
/cakeapp3
I don't want all the model, view, controller files dumped in single directories for the different apps and the sub-apps. And the structure should also work when a domain name is mapped to "subdir", in which case the urls would be shown as follows:
(after http://sampledomain2.com:)
/cakeapp1
/mainapp
/subapp
/cakeapp2
/cakeapp3
This kind of flexibility is necessary for development time and deployment.
Is there any way to achieve this?
Thanks a lot!
celia
Question
3 With corrections to the previous post
The directory structure in my post was screwed up, and my edits didn't seem to take effect. So here it is again. Please ignore the previous message:
Do you or anyone have any suggestion on how to develop/deploy multiple Cake apps under a subdirectory of the http document root? I need to have a structure like this (showing on the browser address bar):
("/" refers to the document root of a single domain, i.e. what's shown on request of http://sampledomain.com)
/subdir
/cakeapp1
/mainapp
/subapp
/cakeapp2
/cakeapp3
I don't want all the model, view, controller files dumped in single directories for the different apps and the sub-apps. And the structure should also work when a domain name is mapped to "subdir", in which case the urls would be shown as follows:
(after http://sampledomain2.com:)
/cakeapp1
/mainapp
/subapp
/cakeapp2
/cakeapp3
This kind of flexibility is necessary for development time and deployment.
Is there any way to achieve this?
Thanks a lot!
celia
Comment
4 Sorry this is a lame system
Comment
5 Multiple apps sharing the same base app
There is no reason why the entire app can't be shared. One way would be to set a variable to use as a unique identifier. Then in app_model, set $useDbConfig equal to this value. Once that's done, the step 3 of your second app can be that all index files point to the same app source.
Maybe set Layout equal to this too, or easier yet - css. Then all users can have the same app, but different databases, layouts, styles, or anything you'd like.
Now that I've written this, I can think of many many ways to simplify it still, but this will get you started...
Comment
6 I think it cool
Unless you know how to use it :)
This should allow spaces
does it?
Question
7 How about windows
Comment
8 Notes
To fix CSS (and other paths) you may need add:
define('WEBROOT_DIR', 'cakeApp1');
(this may not be required in all cases, it seems to be the case if using symlinks to create the structure)
You can install (for example) cake 1.2 parallel to the top cake directory, for example:
/cake
/cake12
/APPS
If you do that, you can then test your app just by changing the core path:
define('CAKE_CORE_INCLUDE_PATH', DS.'home'.DS.'username'.DS.'cake12');
Great for testing out compatibility between two (or more) versions of the framework.
Comment
9 Windows settings
C:\wamp\apps
\cakeApp2
\config
\controllers
\models
\plugins
\tmp
\vendors
\views
c:\wamp\cake
c:\wamp\cakeApp2
I used this to get it to work:
if (!defined('ROOT')) {
define('ROOT', 'c:'.DS.'wamp'.DS.'apps'.DS.'cakeApp2');
}
if (!defined('APP_DIR')) {
define('APP_DIR',dirname('cakeApp2'));
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
define('CAKE_CORE_INCLUDE_PATH', 'c:'.DS.'wamp'.DS.'cake');
}
Bug
10 Article not displayed correctly
Great article.
Exactly answers an issue that I am currently wrestling with. However, it appears to be corrupted. Five of the first 10-step points and one of the second 4-step points display as unprintable characters.
I don't have the skills in cake nor php to interpolate the missing bits on my own. Has anyone saved a copy of this one they could re-post?
Thanx, McS