Search This Blog

Loading...

Monday, March 16, 2009

How to Deploy Symfony Application Across Multiple Servers

Symfony is one of the most famous PHP MVC framework. It has been around for since 2005 and is used in several high profile websites such as delicious and Yahoo Bookmark . Unlike a lot of open source frameworks, it is quite well-documented.
It's possible to deploy a Symfony application across multiple servers. You will normally find yourself in this situation when your website takes off and is receiving hundreds of thousands of hits per hour and your high power server is stressing under the load. So you have to fork out additional dollars to buy more servers. Of course, usually you don't have to worry about multiple server deployment unless you absolutely have to . But should you have to, and you are using Symfony, here's what you might find useful.
To deploy a single Symfony application across multiple servers, with the sessions shared among different computers, you have to use a common database for that purpose. You have to:
  1. Define your session table in your schema.yml:

    propel:
      session:
        sess_id:                     { type: varchar(64), required: true }
        sess_data:                   { type: clob, required: true }
        sess_time:                   integer
    
  2. Define the following yml schema in your factories.yml :

    all:
      storage:
        class: sfPDOSessionStorage 
        param:
          db_table:    session              # Name of the table storing the sessions
          database:    propel               # Name of the database connection to use
          # Optional parameters
          db_id_col:   sess_id              # Name of the column storing the session id
          db_data_col: sess_data            # Name of the column storing the session data
          db_time_col: sess_time            # Name of the column storing the session timestamp
          session_name: example 
          session_cookie_domain: .example.com 
    
  3. Make sure that the table is shared across different servers by assigning the proper privileges and credentials. 
  4. For your databases.yml, you have to make sure that the database connection name is set to propel.



    dev:
      propel:
        param:
          classname: DebugPDO
    test:
      propel:
        param:
          classname: DebugPDO
    all:
      propel:
        class: sfPropelDatabase
        param:
          classname: PropelPDO
          dsn: 'mysql:dbname=bpodb;host=localhost'   #where the db is located
          username: root
          password: #ur password
          encoding: utf8
          persistent: true
          pooling: true
    
  5. Build your Symfony model layer, deploy your applications on multiple servers.
  6. Make sure that you access your Symfony application via a domain name, in this case, it's .example.com. Note that if you access the application through machine name or IP address, the session sharing won't work
  7. And that's it! You will find that the sessions are transfered back and forth seamlessly between different webpages. 

1 comment:

Mark said...

Thanks for writing this post.

I work at a startup called dotCloud, where we provide a service that automates this for you. If you're interested in checking it out, I'd be happy to send you a beta code.

We have a Symfony tutorial at http://docs.dotcloud.com/tutorials/symfony/.