Posts Tagged Shared Hosting

Hacking Symfony Application in A Shared Hosting

In a shared hosting environment, you may not have a full access to the server. Everything has already been installed. But a least they provide a secure shell (SSH).

I have successfully deploy a syfmony 1.2 application in a shared hosting, surely with a hacking.

PDO Hack

Symfony 1.2 at least need PHP 5.2.4 and above. So if your PHP version is 5.2.0 and you’re using MySQL database, make sure to activate PDO::MYSQL_ATTR_USE_BUFFERED_QUERY for buffered MYSQL Api.

Edit your databases.yml:

dev:
  propel:
    param:
      classname:  DebugPDO

all:
  propel:
    class:        sfPropelDatabase
    param:
      dsn:        mysql:dbname=your_db;host=localhost
      username:   root
      password:   your_password
      encoding:   utf8
      persistent: true
      pooling:    true
      classname:  PropelPDO
      options:
        MYSQL_ATTR_USE_BUFFERED_QUERY:  {value: on}

Symfony CLI Hack

Usually, we can customize the php.ini file for web server, but not for the PHP CLI. Unfortunately, the PDO extension for PHP CLI is not enabled by default. As in shared hosting environment, there are no write access to system configuration.

Try this:

$ php -i | grep PDO

If you got an output telling about PDO extensions has been enabled then, you don’t need this hack.

First, we can create our own php.ini file to be passed to the PHP CLI, and put in a writable directory, for example in the home directory:

# ~/php.ini
extension=pdo.so
extension=pdo_mysql.so
memory_limit=64M

Wrap the symfony command to make sure PHP CLI is using our custom php.ini, create the script and name it symfony.sh. Put in home directory for ease the access:

# ~/symfony.sh
cd /path/to/myproject
php -c ~/php.ini -f symfony -- $*

Make sure the script is executable:

$ chmod u+x ~/symfony.sh

Now you can launch the symfony CLI from within your home directory by issuing:

$ ./symfony.sh [command]

, , , , ,

6 Comments

Bringing Up Symfony Application into Linux Shared Hosting

Our application development has been finished, now the time we put the application alive on the web. On shared hosting, directory structure for a html files has already been specified according to the server.

First step, is to figure out what it looks like and where to put html files. Below is a sample:

root/                         <- your hosting root
  home/
  etc/
  public/
  sites/
    www.example.com/
      www/                    <- this is where html files reside,
                                 it can also be html instead www

As we know, the only published part of symfony application is the web folder. So be prepared to put the symfony library and our application files into a folder. You can put the files in home folder for example, or in the public folder (see structure). Lets assume we are using the public folder. The directory structure should looks like:

root/                         <- your hosting root
  home/
  etc/
  public/
    myproject/                <- here are our application files
    symfony/                  <- the symfony library
  sites/
    www.example.com/
      www/                    <- this is where html files reside,
                                 it can also be html instead www

Before transferring all those files, make sure your project config file include the symfony library using relative path.

Symfony 1.0 (myproject/config/config.php):

<?php

// symfony directories
$sf_symfony_lib_dir  = realpath(dirname(__FILE__).'/../../symfony/lib');
$sf_symfony_data_dir = realpath(dirname(__FILE__).'/../../symfony/data');

Symfony 1.1 and up (myproject/config/ProjectConfiguration.class.php):

<?php

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

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

Transfer all files to those folder using either ftp client, rsync (which I am prefer) , or using your Hosting Control Panel. For Windows users, you can try using CwRsync which already include a ssh client.

The visible files for symfony application are:

  • index.php, production front controller.
  • .htaccess, for url rewriting and others apache stuffs.
  • images, the images folder.
  • css, the stylesheets folder.
  • js, the javascripts folder.
  • sf, the symfony assets folder.
  • uploads, the uploads folder.
  • Plus the plugin assets.

So now you can open a remote shell to symlink all those files and directory to be published under the web using a ssh client. Most of web server hosting already include a secure shell server, but you can check if it’s already enabled in your Control Panel. As an alternative client, you can use putty either. Here are the sample command to be issued:

> ssh myuser@example.com

$ cd /path/to/sites/www.example.com/www
$ ln -s /path/to/public/myproject/web/index.php index.php
$ ln -s /path/to/public/myproject/web/.htaccess .htaccess
$ ln -s /path/to/public/myproject/web/images images
$ ln -s /path/to/public/myproject/web/css css
$ ln -s /path/to/public/myproject/web/js js
$ ln -s /path/to/public/myproject/web/uploads uploads
$ ln -s /path/to/public/myproject/web/sfPlugin sfPlugin <- the plugin assets
$ ln -s /path/to/public/symfony/data/web/sf sf

Repeat the command for all others files needed by your application, for example, plugins assets. Then, let’s continue by fixing the file permission.

$ cd /path/to/public/myproject
$ chmod u+x symfony                    <- make symfony executable
$ ./symfony fix-perms                  <- symfony 1.0
$ ./symfony project:permissions        <- symfony 1.1 and up

Some times, we also must fix the permission for the front controller, 0755 is satisfied.

$ chmod 0755 index.php
$ chmod 0755 .htacess

You’re done, try browsing your server.

Most of shared web hosting will redirect error log to stdout neither to a log file, so the error will be visible to the user. To avoid this, you need to comment the error_log() method call in symfony/lib/exception/sfException.class.php and symfony/lib/exception/sfError404Exception.class.php (symfony 1.1 and up).

Next time if you update your application, just transfer the changes (which handled by rsync), fix the permissions, and no need to do symlink again.

, , ,

5 Comments