Deploying Symfony 2 on OpenShift is real easy and it’s free. You can even build a scalable application, that will add servers automatically when the load increases and remove servers when the load drops. OpenShift offers a Symfony 2.3 cartridge, no setup required, but in this tutorial we will setup Symfony 2.7 for deployment.
The Quick Way
If you have an account with OpenShift – create a new PHP 5.4 application with my symfony2-openshift GitHub repo in the Source Code field.
https://github.com/neattutorials/symfony2-openshift.git
That’s it!
Extended Tutorial
If you wan’t to follow step by step or learn about the specifics – the extended tutorials is for you.
Create OpenShift account
You only need an email account to sign up for openshift. You get 3 small gears (virtual servers with 1 core 2.5GHz, 512MB RAM and 1GB of storage) for free. On free accounts gears will go idle (fall asleep) after 24 hours of inactivity, but they wake back up once someone accesses you application.
Now add your public key so you could pull from and push to your openshift git repository.
If you don’t have a public key donwload PuTTYgen and generate a new key.
Create PHP 5.4 application
Clone the repository
OpenShift provides a link to your applications git repository.
Install git if you haven’t already and use this command to clone your repository:
git clone ssh://jf09543lsdgfa0jmv8a243hj@www-neattutorials.rhcloud.com/~/git/www.git/
Symfony
Create a new symfony project and put all the symfony files inside the folder with your openshift repository files. Now let’s edit the controller that comes with symfony so we won’t get a 404 when we access our application. We will change the @Route from “/app/example” to “/”.
class DefaultController extends Controller { /** * @Route("/", name="homepage") */ public function indexAction() { return $this->render('default/index.html.twig'); } }
Deploy action hook
Now we need to make a deploy action hook. It’s a file with commands, that will be executed after our files are pushed to the server, but before Apache is started back up. During this period we need to install dependencies using composer install.
We need to place it in to .openshift/action_hooks/ the file should be called deploy.
#!/bin/sh #.openshift/action_hooks/deploy export SYMFONY_ENV=prod cd $OPENSHIFT_REPO_DIR #If you are planning to upload files - they have to be stored in $OPENSHIFT_DATA_DIR #Otherwise they will be deleted on every deployment. #ln -s $OPENSHIFT_DATA_DIR/uploads web composer install --no-dev --optimize-autoloader #if you are using DoctrineMigrationsBundle #php app/console doctrine:migrations:migrate
In order for our deploy hook to work we have to make it executable with this command:
git update-index –chmod=+x .openshift/action_hooks/*
This will only work if the file was added in git beforehand.
Database parameters
If you are planning on using a MySQL database you need to extract all the MySQL parameters from environment variables. We can do this in a php config file using getenv().
<?php # app/config/parameters.php if (getenv("OPENSHIFT_MYSQL_DB_HOST") != '') { $container->setParameter('database_host', getenv("OPENSHIFT_MYSQL_DB_HOST")); $container->setParameter('database_port', getenv("OPENSHIFT_MYSQL_DB_PORT")); $container->setParameter('database_name', getenv("OPENSHIFT_APP_NAME")); $container->setParameter('database_user', getenv("OPENSHIFT_MYSQL_DB_USERNAME")); $container->setParameter('database_password', getenv("OPENSHIFT_MYSQL_DB_PASSWORD")); } ?>
It is important that we include parameters.php after parameters.yml, but before everything else.
#app/config/config.yml imports: - { resource: parameters.yml } - { resource: parameters.php }
Done
That’s it. Now we can git commit
and git push
our app to openshift and see if it works.
Zero Downtime Deployment
Each time you push updates Apache gets stopped. When all the preparations are made Apache gets started back up. You can enable hot deployment by creating a file called hot_deploy
in the .openshift/markers/
folder. This will prevent Apache from going down during deployment.
If you want zero downtime hot deployment isn’t the best option. Here’s how to get real zero downtime during deployment.
Now during deployment one gear will handle the traffic while the other receives an update. Once the first gear is updated it goes online then the second gear will go offline to receive the update.