Development

InstallingSymfonyOnSharedHostNoSsh

You must first sign up to be able to contribute.

Installing symfony on a shared host with no pear and command-line access

Requirements

  • A shared hosting with PHP5
  • FTP access
  • Symfony installation with PEAR on local development environment

Overview

This guide is for people who wish to install Symfony on an environment where they have minimal control and permissions.

Security Issues

Please read and understand Why Symfony is not safe in many shared hosting environments, a patch for some but not all shared hosts, and alternatives to shared hosting before following this HOWTO. Not all shared hosting environments can be made safe for Symfony. Others require a patch to Symfony. "Virtual machine" environments should be OK.

Directory Layout

A typical directory layout of a user in a shared hosting environment is like this:

/
/stats
/logs
/html (the document_root of our virtual host. it may have a different name)
etc..

With this guide the directory layout will be like this (one can customize this layout for his own needs):

/
/html
/pear_libs
  /symfony
  /data
    /symfony
/projects
  /myproject
    /apps
    .
    .

First thing to do will be FTP-uploading the locally installed framework files to our host. There are 2 directories to upload. These are

In Local Environment To Host Environment
%PEAR_DIR%/symfony /pear_libs/symfony
%PEAR_DIR%/data/symfony /pear_libs/data/symfony

Now for uploading our project(s):

In Local Environment To Host Environment
myproject /projects/myproject
myproject2 /projects/myproject2

And lastly the web folder of project to webroot of our host:

In Local Environment To Host Environment
myproject/web /html

One more directory we should have in the webroot is /sf directory of symfony's shared js and css files. There are two ways to do this. Either simply copy the directory or make a symlink to it.

Target Link
pear_libs/data/symfony/web/sf /html/sf

Copying is straightforward but making a symlink is a better solution considering any future upgrades to the symfony framework (so you won't have to re-copy the contents on each upgrade). How can we make the symlink when we have no command line access. The answer is with a small php script. Create a new file named "makesymlink.php" under /html (your webroot folder) and its contents as:

<?php
/*
Allow write permissions on below directories before executing..
You should restore permissions after symlinks are created...
*/
$rootdir = realpath(dirname(__FILE__).'/..');
$webdir = realpath(dirname(__FILE__));
symlink($rootdir.'/pear_libs/data/symfony/web/sf', $webdir.'/sf');
?>

Using your ftp client, give full write permissons to the /html (or your document_root) and run the script by browsing to the makesymlink.php. After that refresh the directory view in your ftp-client and see the symlink created. Now you can restore the original permissions to be safe and delete the makesymlink.php file.

Final modifications

Now we have all the framework and project(s) uploaded it's time to make the necessary changes to be able to run our project with our custom installation. In this stage there are two ways to accomplish it without modifying the project code (modifying the config.php file of all the applications of a project is possible too).

1. Fake a pear-installation by adding the \pear_lib to include_path. This is what I prefer for my installations:

Modify index.php and myapp_dev.php, and make it like:

<?php
$vhost_root_dir = realpath(dirname(__FILE__).'/..');
$project_dir = $vhost_root_dir.'/projects/myproject';

//define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_ROOT_DIR',    $project_dir);
define('SF_APP',         'frontend');
define('SF_ENVIRONMENT', 'prod'); // for myapp_dev.php 'prod' should be 'dev'
define('SF_DEBUG',       false);  // for myapp_dev.php this should be true

// include path
set_include_path(
  $vhost_root_dir.'/pear_libs'.PATH_SEPARATOR.
  get_include_path()
);

unset($vhost_root_dir, $project_dir);

require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');

sfContext::getInstance()->getController()->dispatch();

?>

And modify the file /pear_libs/symfony/pear.php to make the paths correct: (You must remodify this file after an upgrade to the symfony framework.)

<?php
$sf_symfony_lib_dir  = realpath(dirname(__FILE__));
$sf_symfony_data_dir  = realpath($sf_symfony_lib_dir.'/../data/symfony');
$sf_version          = '0.6.2';
return 'OK';

?>

2. Or you can make it work like the sandbox by making symlinks as:

Target Link
pear_libs/symfony projects/myproject/lib/symfony
pear_libs/data/symfony projects/myproject/data/symfony

php code for creating the symlinks is:

$rootdir = realpath(dirname(__FILE__).'/..');
/*
Allow write permissions on below directories before executing..
You should restore permissions after symlinks are created...
---------------------------------------------------------------
/projects/myproject/lib
/projects/myproject/data
*/
// symfony_lib
symlink($rootdir.'/pear_libs/data/symfony', $rootdir.'/projects/myproject/data/symfony');
// symfony_data
symlink($rootdir.'/pear_libs/symfony', $rootdir.'/projects/myproject/lib/symfony');

Linking to Symfony Libraries

Unless the proposed library structure matches your own, will you also have to update ProjectConfiguration.class.php, which is located in the /config directory, to match it:

<?php

require_once dirname(__FILE__).'/../lib/symfony/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
  }
}

Setting up permissions

You should give full write permission (if the server runs php with suPHP this may not be necessary) to the cache and log directories so they can be written with no errors. The directories are:

  • /projects/myproject/cache
  • /projects/myproject/log

DRAFT