How to deploy and run a zend project on a server?

canimbenim picture canimbenim · Apr 3, 2011 · Viewed 10.9k times · Source

I have a question about deploying Zend project on a server. On localhost I used virtual host to set the document root to the public/index.php folder.

  • How should I deploy it now? I have copied whole my project on the server but it is not working as all my paths are wrong (as all are set up relative to the public folder).
  • What should I do? Is is possible to set all my paths relative to the my home path on the server? How can I redirect my application to any controller in this situation? Or maybe I just need to create virtual host on the server but I do not have permission to do it:/?

I need some advices, thank you


Application.ini :

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"


resources.view[] =

resources.db.isDefaultTableAdapter = true
resources.db.adapter = PDO_MYSQL
resources.db.params.charset = "utf8"
resources.db.params.host = HOST
resources.db.params.username = "p"
resources.db.params.password = "***"
resources.db.params.dbname = "p"

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

Index.php:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

defined('DESTINATION_IMAGES') || define('DESTINATION_IMAGES', './usersImages/');
defined('TRUE_STORY_EMAIL') || define('TRUE_STORY_EMAIL', '[email protected]');
defined('TRUE_STORY_NAME') || define('TRUE_STORY_NAME', 'True story team');

// 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();

.htaccss:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

phpinfo();

w Loaded Modules I have mod_rewrite so probably is enabled.

-----------------------------

--------EDIT 04.05.2011 -----

I have deployed the project once again on the server and I have set RewriteBase /~user2/Project2/public

And now the URL mapping is working (probably the first time was an error when I deployed it that is why it was not working).

But I still have problems with the project. When I go to the Auth controller :

public function indexAction() {
        // action body

        $form = new Application_Form_Login();
        $request = $this->getRequest();
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                try {
                    if ($this->_process($form->getValues())) {
                        $userDT = new Application_Model_DbTable_User();
                        $idUser = Zend_Auth::getInstance()->getIdentity()->id;
                        if ($userDT->isConfirmed($idUser)) {
                            $this->_helper->redirector->gotoRoute(
                                    array(
                                        'controller' => 'index',
                                        'action' => 'index'
                                    )
                            );
                        } else {
                            $form->setDescription('Your account is not confirmed!');
                            Zend_Auth::getInstance()->clearIdentity();
                            $this->_helper->redirector->gotoRoute(
                                    array(
                                        'controller' => 'Registration',
                                        'action' => "step",
                                        'idUser' => $idUser
                                    )
                            );
                        }
                    } else {
                        $form->setDescription('There was an error. Try to log in once again please');
                    }
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            }
        }
        $this->view->form = $form;
    } 

On the page I can see only :  and I do not know what it means as I check that redirecting in different Controller is working what mean that my project see the zend libraries so I can not understand why it does not create the form in the view ?

The view:

<?php $this->headTitle('Login'); ?>
<h1>Login</h1>
<?php echo $this->form->setAction($this->url()); ?>

Answer

Upvote picture Upvote · Apr 4, 2011

Simply copy your project on your server and set the domain directory pointer to the public folder. In most cases that should do the job.

Another approach is to setup a vhost file and set the document root. You need this if you want a zf project to be accessible with a subdomain.

But this is more a server configuration question.

Application.ini:

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ''

resources.frontController.params.displayExceptions = 1
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.frontController.defaultControllerName = "home"
resources.frontController.params.prefixDefaultModule = "1"

//some db setup

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

I hope you didn't change the index.php file because you shouldn't:

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../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();