dan
dan

Reputation: 360

Deploy on Shared Web Host

I have a simple question but I just can't find a straightforward answer anywhere.

I'm not very familiar with apache .htaccess and now I should deploy a ZF2 app on a web host that doesn't allow me to change the website root folder.

I have a standard project structure so my index.php file is located inside the /public folder, like this:

www.mydomain.com

root/ <--- I can't set root to /public folder
    /config
        ...
    /module
        ...
    /public
        .htaccess
        index.php
        ...
    /vendor
        ...
    ...

This is from the ZF2 skeleton app.

Please I need a workaround, I'm sure this is a pretty common problem that many ZF2 developers already tackled. I think I should add a .htaccess file to /root but I don't know how to write one that would work in this case.

As always any help is much appreciated,

Dan

Upvotes: 0

Views: 134

Answers (1)

dan
dan

Reputation: 360

In order to make this work you have to take everything from the /public folder and put it inside your web root. The file index.php from the skeleton app must be edited to take account of its new position.

First remove the line:

chdir(dirname(__DIR__));

Because its purpose is to set the current directory to the web root which we are already inside.

Second you have to either:

Replace the line require 'init_autoloader.php' with require 'vendor/autoload.php' (if using composer).

OR

Inside your .htaccess set the ZF2_PATH env variable to where zf2 is installed on your server:

#First line of your .htaccess
SetEnv ZF2_PATH path/to/zf2

Now it should be working.

Make it better

With the above setup you'll have to put all your public folders inside the web root. If you, like me, don't like that just continue reading.

You can put your public folders (e.g., /js, /css) inside /public (index.php and .htaccess still need to be in the root) by simply telling zf2 your new base_path. This is what is used by the basePath() view helper inside your view scripts. You simply need to add the following to your config:

'view_manager' => array(
    'base_path' => './public',
),

And the basePath() view helper will output the correct urls.

Make it even better

The last problem with this setup is that your app files are accessible from the web. To fix this I put everything I want to hide inside the /private folder. You end up with a project structure similar to this:

/root
    /private
        /config
        /data
        /module
        /vendor
        .htaccess     <-- You have to create this one
        composer.json
        composer.lock <-- Created by composer after install
        composer.phar
    /public
        /css
        /js
    .htaccess
    index.php

Inside the /private folder you have to create a new .htaccess file and put this line inside it:

Deny from all

This makes everything inside the folder not accessible from the web.

With this change you have broken your app. Infact inside /private/config/application.config.php you have to adjust the module_paths config:

'module_listener_options' => array(
    'module_paths' => array(
        './module',
        './vendor',
    ),
),

You have to prepend /private to the paths:

'module_listener_options' => array(
    'module_paths' => array(
        './private/module',
        './private/vendor',
    ),
),

This will make your app run once again. Now you can deploy your ZF2 apps to shared web hosts and retain a clean project structure.

Upvotes: 1

Related Questions