How to Create a Staging Environment with PHPFog + Symfony2: Symfony Advent Calendar 2011 JP - Day 13 -

Tadashi Shigeoka ·  Tue, December 13, 2011

Hello! This is day 13 of “Symfony Advent Calendar JP 2011”.

Taking the baton from yesterday’s @77web, today I’ll introduce “How to Create a Staging Environment with PHPFog + Symfony2.”

What are PHPFog and Symfony2?

Now, for today’s blog theme, as the title suggests, I’ll write the procedure for creating an environment where Symfony2 works on PHPFog in great detail.

There’s no need to explain Symfony that everyone loves - it’s a web application framework for PHP5.

・Official: High Performance PHP Framework for Web Development - Symfony  

“PHP fog” is a PaaS-type cloud that provides PHP execution environments. It recently announced that up to 3 applications would be permanently free, making it a PHPer-friendly service.

・For details, see this article: ずっと無料で使えるPaaS型クラウドのまとめ。2011年版 - Publickey

・Official: PHP Fog — Rock Solid Cloud Platform for PHP  

Using Heroku to easily create staging environments was wonderful, so I wanted to quickly create a staging environment for Symfony2.

By the way, how do you all handle staging environments?

  • Contract a VPS for staging
  • After checking in the development environment, straight to production deployment!
  • Staging environment? What's that? Is it tasty?

There are various approaches.

I’ve been developing with Heroku + Ruby on Rails for a while, and I was impressed that you can easily create not only production environments but also staging environments.

So, couldn’t we do the same with Symfony2 using PaaS?

So I tried it.

(For those who want to create a staging environment with Heroku + Rails, check out this article too)

・Reference: Heroku + Rails の認証付きステージング(staging)環境を構築する方法 | CodeNote.net  

Initially, I thought "Let's use Heroku since PHP has recently become available there!" but it was impossible

It’s not widely known, but Heroku has supported PHP for a while now.

So I thought, “Let’s try with the familiar Heroku!” and gave it a try, but it was impossible as a Symfony2 runtime environment as shown below.

http://symfony2.herokuapp.com/  

Since it's Symfony2, let's use PHP Fog!

Since Heroku wasn’t available, I had to consider other PaaS options.

Symfony2 is a PHP framework, and sushi.

Let’s use PHP Fog!

So, drawn by the name, I decided on PHP Fog.

Other candidates like fluxflex are also run by Japanese people and looked quite good, but that’s for another opportunity.  

Steps to create a staging environment with PHP Fog + Symfony2

Finally, the procedure.

  1. Create a new app on PHP Fog
  2. Push the Symfony2 application to PHP Fog
  3. Configure the PHP Fog App for Symfony2
  4. Set up Basic authentication for Symfony2

Let’s get started!  

1. Create a new app on PHP Fog

First, create a new application on PHP Fog.

 

Next, on the application selection screen, click “Custom App.”

(They have Cake, Zend, CodeIgniter, but no Symfony!)

 

Configure the “MySQL password” and “domain settings.”

If you don’t use a custom domain, you can use a subdomain of phpfogapp.com.

 

App creation on PHP Fog is complete.

 

Finally, configure SSH public key settings.

 

2. Push the Symfony2 application to PHP Fog

After creating the app on PHP Fog, now it’s time to deploy the Symfony2 application.

First, git clone the repository from PHP Fog to local and delete the initial files.

$ git clone [email protected]:symfony2.phpfogapp.com
Cloning into symfony2.phpfogapp.com...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

$ cd symfony2.phpfogapp.com
$ git rm index.php

    Also, add a remote Git repository with the name phpfog for easy access.

$ git remote add phpfog [email protected]:symfony2.phpfogapp.com

 

Next, deploy the Symfony2 files and edit app_dev.php and config.php to comment out the IP access restriction parts.

$ vim web/app_dev.php 
$ vim web/config.php

・Reference: Symfony2 ドキュメント日本語版 | Symfony2日本語ドキュメント  

After editing, deploy the Symfony2 app on PHP Fog.

$ git add .
$ git commit -m 'symfony2 init'
$ git push phpfog master
Counting objects: 3975, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3781/3781), done.
Writing objects: 100% (3974/3974), 3.75 MiB | 290 KiB/s, done.
Total 3974 (delta 1060), reused 0 (delta 0)
remote:  -----> Deploying to the cloud
To [email protected]:symfony2.phpfogapp.com
   5708d64..adab3a0  master -> master

 

Check the Symfony2 runtime environment from /config.php.

Write the timezone setting in app/AppKernel.php.

ini_set('date.timezone', 'Asia/Tokyo');

PHP accelerator can’t be installed so skip it.

Similarly, short_open_tag setting can’t be changed so skip it too.  

3. Configure PHP Fog App for Symfony2

Since PHP Fog doesn’t allow running commands from the console, we need to make commands like cache:clear executable via web app.

$ vim web/clean_prod_cache.php

 

Also, configure the document root setting.

This allows the URL http://symfony2.phpfogapp.com/web/app_dev.php/ to become

http://symfony2.phpfogapp.com/app_dev.php/

by omitting “web”, so let’s set this up.  

Finally, give write permissions to the cache and logs directories.

 

Set up Basic authentication for Symfony2

Since this is a staging environment, we want to set up at least Basic authentication.

Let’s quickly write it in security.yml.

$ vim app/config/security.yml
security:
    encoders:
        Symfony\\Component\\Security\\Core\\User\\User: plaintext

    providers:
        in_memory:
            users:
                admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

    firewalls:
        secured_area:
            pattern:    ^/
            http_basic:
                realm: "Secured Area"

    access_control:
        - { path: ^/, roles: ROLE_ADMIN }

The app created up to this point can be accessed from the link below with ID: admin / PASS: adminpass.

http://symfony2.phpfogapp.com/app_dev.php/

That’s all from the Gemba.

I haven’t created any of the MVC components so it feels somewhat lacking, but that’s it for today.

If you have recommendations for “This is recommended for Symfony2 staging environments!”, I’d be happy to receive advice via Twitter or blog comments.

I’m also actively seeking advice from other languages like “This is how we handle staging environments!”  

Next time preview

Tomorrow, 2011/12/14(Wed), it’s @ganchiku, Mr. Ohno.

・Mr. Ohno’s profile page: GANCHIKU.com

I’m sure it will be very informative content, so please look forward to it!

Reference Information