Deploying Zend Framework applications on a shared host

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!

Published by

Junior Grossi

senior software engineer & stutterer conference speaker. happy husband & dad. maintains Corcel PHP, elePHPant.me and PHPMG. Engineering Manager @ Paddle

18 thoughts on “Deploying Zend Framework applications on a shared host”

  1. 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!

    1. hi @damian! you have to use the public dir as your public shared host directory, maybe htdocs or even www. this will be your public 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!

  2. 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.

    1. 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.

  3. 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?

    1. 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.

  4. 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! 😉

  5. 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

    1. 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…

  6. 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.

    1. 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.

  7. 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 the php_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!

  8. Hi Junior,

    Great article. There are lots of other similar articles but your approach is easy and works best. Thanks for posting…..

Leave a Reply to Prashant Patle Cancel reply

Your email address will not be published. Required fields are marked *