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.
- Download the Symfony2 Standard Edition
- Add a new bundle called
CompanyExampleBundlewhere company is the name of your company, group, or project. Don’t forget to add
Companyto the auto-loader in
app/autoload.php, and register the bundle in
- Create a new controller at
src/Company/Controller/HelloWorldController.phpwhich renders a view
CompanyExample:HelloWorld:index.html.twig. In the view, put in a nice little
_welcomeroute’s pattern to
/welcome. Now go to
app/routing.ymland add a route like so:
- 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:
- 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.
- Login to PHP Fog and select Create a New App.
- On Step 1 of the application wizard choose “Custom App” from the bottom left.
- 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.
- 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.
- Add the public key from the machine you are working on
~/.ssh/id_rsa.pubto the accepted SSH keys in your Account Settings.
- Now go to the Source Code tab from your apps PHP Fog admin panel and copy the git repository location.
- Finally, from the console, add phpfog as a remote and push. Like so:
- Now test that your code made it up by accessing the
robots.txtfile 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…
- Symfony2 caches all your configuration files to the
appdirectory, and therefor needs write access. Go to the “Permissions” tab of your app’s admin panel and add the pathname
appand save changes.
- 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.
- 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.phpwith the code like so:
- Now when you go to
http://company.phpfogapp.com/clean_prod_cache.phpthe 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.