PHP Fog Blog

Using Symfony2 on PHP Fog Right Now

This is another post in our series of guest posts about using PHP Fog. The original post by @developly is reprinted here with permission.

PHP Fog is a new PHP PaaS service. It’s dazzling. It takes the weight of deploying applications to the cloud off your shoulders and allows you to focus on writing code. You can deploy your app’s with a simple git push phpfog master. This is great, but PHP Fog is new and there is no built-in considerations for Symfony2. That makes sense – Symfony2 is still (at the time of writing) in beta. But I want to deploy Symfony2 apps now!

In this article I’ll show you how you can use PHP Fog to host a Symfony2 application. For the purpose of ease we are going to use Github but you could just as easily host your code with Beanstalk, or use PHP Fog’s Git repository alone.

Step 1, setup a Symfony2 app in Git.

If you already have your application in Git and have at least one production route, then you can skip to the next step.

  1. Download the Symfony2 Standard Edition
  2. Add a new bundle called CompanyExampleBundle where company is the name of your company, group, or project. Don’t forget to add Company to the auto-loader inapp/autoload.php, and register the bundle in app/AppKernel.php.
  3. Create a new controller at src/Company/Controller/HelloWorldController.php which renders a view CompanyExample:HelloWorld:index.html.twig. In the view, put in a nice little Hello world!.
  4. In app/routing_dev.yml change the _welcome route’s pattern to /welcome. Now go toapp/routing.yml and add a route like so:
  5. At this point, verify that you can get to the new controller by traveling to  http://localhost/yourproject/web/app_dev.php/ in your browser. If you see a Hello world!, then you’re gravy. Now substitute app_dev.php for just app.php to access the production front controller and make sure you see the same thing. If you get a 400 or things appear to be as they were before, you might need to blow production cache. This can be done from the command line like so:
  6. You’ve setup a new Symfony2 app and are ready to get going! Commit and push your application to Github then continue on to the next step.

Step 2, Create your app on PHP Fog.

  1. Login to PHP Fog and select Create a New App.
  2. On Step 1 of the application wizard choose “Custom App” from the bottom left.
  3. On Step 2 it’s important to note that if you want to use Assetic, then you need a paying box – this is because the shared tier does not support open_basedir. If you aren’t going to use Assetic, go ahead and choose the free shared tier.
  4. On the final step choose a URL. If you decide to use a custom URL you will have to alter DNS settings, so for a quicker finish we’d suggest a phpfog url. Once you’ve done this hit the “Create Application” button and you’re done! Once the application has been provisioned you will receive an email and can continue onto the next step.

Step 3, Pushing your Symfony2 application to PHP Fog

Pushing your app to PHP Fog is really easy. It’s perhaps my favorite thing to do.

  1. Add the public key from the machine you are working on ~/.ssh/id_rsa.pub to the accepted SSH keys in your Account Settings.
  2. Now go to the Source Code tab from your apps PHP Fog admin panel and copy the git repository location.
  3. Finally, from the console, add phpfog as a remote and push. Like so:
  4. Now test that your code made it up by accessing the robots.txt file that comes with Symfony2 by default. Should be at a url like so:http://company.phpfogapp.com/web/robots.txt. If this resolves, you’ve successfully deployed your code to the cloud!

You’re not done yet, however, you still need to tweak you PhpFog app to Symfony2’s liking.

Step 4, Configure your PHP Fog app for Symfony2

You’re almost there. There’s just a couple things left…

  1. Symfony2 caches all your configuration files to the app directory, and therefor needs write access. Go to the “Permissions” tab of your app’s admin panel and add the pathname app and save changes.
  2. Go to the “Settings” tab and change the Base Directory to web. This will tidy up your url, you won’t have to type in /web before accessing resources.
  3. Finally, you need a way to clear production cache, while in production. PHP Fog doesn’t give console access so the simplest way to accomplish this is by creating a php script called web/clean_prod_cache.php with the code like so:
  • Now when you go to http://company.phpfogapp.com/clean_prod_cache.php the cache will be wiped and warmed. All ready to go.

I hope that this guide helps both developers looking to use Symfony2 on PHP Fog early as well as the PHP Fog team in implementing Symfony2 support.

Powered by Olark