Hello everybody!
Usually I have direct access (shell) in all the servers that host my Zend Framework applications, but, some clients already have a shared host, so I have to deploy the application there too.
I was thinking how can I never thought this before! There are a lot of ways to deploy a Zend Framework application, but in a shared host we can’t have access to create Virtual Hosts on Apache.
Folder structure
On a shared host you have your own folder, which you can upload your PHP files. That’s my client’s folder structure:
Take a look on the public_html folder. It’s the folder where usually we upload our PHP files. Some hosts call it htdocs too.
Zend Framework’s structure
Basically, the default ZF’s structure has 5 folders: application, docs, library, public and tests.
Uploading
Inside ZF’s public folder we have images, javascript files, css files, etc, all that have direct access through url. In other words, we put only the public folder’s content in the public_html folder on our shared host.
Let’s create another folder and let’s call it php_apps. Inside it let’s create another folder, to host our application files (app1 folder). That folder will receive all the ZF’s folders (except public folder, of course).
So let’s put our application, docs, library and tests folder inside it.
New, all we need is tell the index.php to access the new folder. Let’s edit it (line 4):
<?php // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../php_apps/app1/application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); // Ensure library is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), get_include_path(), ))); /** Zend_Application */ require_once 'Zend/Application.php'; // Create application, bootstrap, and run $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini' ); $application->bootstrap()->run();
Now, accessing your public_html folder (or http://www.domain.com) we’ll call the index.php file, that calls our application folder (inside php_apps/app1).
To work with images, css, etc, in your view files, is exactly the same:
<?php echo $this->baseUrl('/img/my_image.gif'); ?>
That’s all! Thanks!
Hi Junior!
This is the best solution I have found, thanks for your posting.
I am doing something wrong… because I get the public folder listed in the browser, and if I browse it, I get this error:
Fatal error: require_once() [function.require]: Failed opening required ‘Zend/Application.php’ (include_path=’:.:/usr/local/lib/php:/usr/local/php5/lib/pear’) in /home/jayhsueh/wejo.co/public/index.php on line 16
I really appreciate your help, thanks!
hi @damian! you have to use the
public
dir as your public shared host directory, maybehtdocs
or evenwww
. this will be yourpublic
dir and others will be “behind” it. this error you’ve noticed is because you have to add the dir where your Zend is in the php path (include_path
function). thanks for the comment!Hello Sir,
I have a problem related to redirecting one action to another on live site.(http://marwarishaadicentre….
I create my site in zend framework-2 and deployed on Linux Server but at the time of redirecting it should not work. It will stop on previous action.
ex:- At the time of authentication it can authenticate successfully but on redirect it will stop.
$this->redirect()->toRoute(‘admin’); this code is not work and in whole project any where redirect is not working.
toRoute(‘admin’) or toUrl(‘http://marwarishaadicentre…. these functions are not work.
Please help me.
Thanks in advance.
Hi Prashant!
To help you I need more information about your code. If possible send me some file codes using http://pastebin.com. This will be important to try to find your solution.
Many thanks. Regards.
Hi. Is it possible to simply put application, library, docs and test folder in the root of subdomain, next to public_html folder. Is it necessary to create php_apps folder?
Hi Ivan. Yes it is. I think put inside another folder is better if you have more than one app, but you can use root too. Thanks.
Hi Junior,
Aaah, I see, `php_apps` is a sibling (not a child!) of `public_html`, so it’s out of the web root. I misread the hierarchy. ;-(
Of course, that’s safer than mucking about with an `.htaccess` file and it’s just as easy to use: it’s still pointing to the app folder, it just sensibly puts it outside the web root.
Cool! 😉
Thank you for the comment David. =)
Thanks for your quick answer, something i did not mention is i want to make it a subdomain. i develop web app for a client, he has to make some modification of content, browse entire website, correct things before we definitly publish so i think about http://demo.example.com .
Thank again
Hi Marcel…
Assuming you’re on a shared host you only hava public access on /home/example.com/public_html, right? So if you’ve created the demo subdomain, I think it is on /home/example.com/public_html/demo. Considering the project below:
– application (controller|views|…)
– docs
– library
– tests
– public (images|js|styles|…)
So your new structure will be:
– /home/example.com/my_zf_project/a…
– /home/example.com/my_zf_project/docs
– /home/example.com/my_zf_project/l…
– /home/example.com/my_zf_project/t…
– /home/example.com/public_html/dem…|js|styles) // you’ve put the public content inside demo folder…
Remember that you have to change your /public_html/demo/index.php file and change application path like ../../my_zf_project/application
I hope help you.. Thanks…
coz i got 403 error i can’t figure a way out to resolve it
Marcel. You’re getting 403 error because you zf public folder is not really the public folder that your domain open files. For example http://www.example.com open my /home/example.com/public_html. So you must have all folders inside /home/example.com except public. The public content must be on public_html folder, what the browser will open.
I hope help you…
Thanks 4 your tutorial.
I have a question if i may? what would be the config if i place my folder project in subfolder of root directory.
Hi Marcel…
First… you must have the public dir (zf project folder) inside the public_html or htdocs. Inside public dir you have the index.php that is the file called every request.
Others folders like application, library, test (php and ini files) you must protect them.
Is it ok? Thanks for the comment.
Hi Junior,
Nice approach. 😉
But isn’t there a security issue here? If a visitor visits the url:
http://example.com/php_apps…
won’t he get that file served up directly? The standard .htaccess at the root checks for matching files and directories before routing to the ZF app.
Don’t you need to add an
.htaccess
with code>Deny Allto thephp_apps
directory? Or perhaps tweak the.htaccess
at the web root to look for specific file types – .js, .css, etc – before routing to ZF?Cheers!
Hi David!
The Zend’s public directory is the called public_html on a shared host. So if you visit http://www.example.com/php_apps the Apache will search for a directory called php_apps inside public_html. There is no way to visit php_apps because your domain’s Virtual Host é pointed to /home/example.com/public_html, not /home/example.com.
I hope help you… Thanks!
Hi Junior,
Great article. There are lots of other similar articles but your approach is easy and works best. Thanks for posting…..
Hi Jay. Thanks for the comment! I’m glad helping you…