Ansible Symfony and nginx


In this video we will use Ansible to build ourselves a Symfony development server running on nginx.

The nice thing about using Ansible for this process is that it allows you to spin up a brand new, fully working Symfony development environment in a matter of minutes, which in turn allows you to get immediately focussed on the important part - development, instead of the usual time consuming part - operations.

You may be wondering why bother with a tool like Ansible for this process, when you are already more than capable of setting up an nginx server, uploading your Symfony site, and so on.

It's a good question - and having that knowledge is certainly very valuable.

For me, it is really all about saving time. Once you have built and deployed an nginx server, uploaded your Symfony files, set up the relevant directory permissions, and of course, debugged the whole set up because things never work perfectly the first time... well if you can bypass that every subsequent time in the future, then in my opinion, you would be crazy not too.

The other big point for me is that before this, I would work in quite a similar fashion. I would build a server manually, install nginx and MySQL, then power down the machine, and clone it for future projects.

Whenever I needed a new Symfony box, I would clone that base image, boot it up, customise it and crack on. But that was still more effort than I would have liked. And it still meant images diverged over time. There was no standardisation, or living documentation.

Ansible has changed all that.

Ansible nginx

Whilst Apache is great for a development environment, I can say from bitter experience that as your web site grows, should you be lucky enough to gather a sizeable volume of traffic then you are inevitably going to hit problems.

To mitigate that, it is better to switch to nginx early, even if you are far more comfortable currently with Apache.

And the good news with using nginx through Ansible is that you can read through the exact process ahead of time, and / or refer back to the exact task list used to build your nginx environment.

As mentioned in the video, the nginx role I use is a customisation on the nginx role from Chris Fidao's Servers for Hackers ansible-nginx role, and also from the book of the same name (highly recommended reading for any developer who is also interested in servers).

You can find all the code for the Ansible Nginx and Symfony roles on the CodeReview GitHub account.

Disable the Default Site

Update: this bug is now fixed. I had forgotten to also remove the default site from sites-available. I also added in a call to the Restart Nginx handler at the last step in the nginx.yml file.

One issue I keep running in to with this set up is that the Default site (/etc/nginx/sites-enabled/default) won't properly remove. This, in turn, will cause your Symfony site not to load.

The solution here is to log in to your built Symfony development server and manually remove this file (rm /etc/nginx/sites-enable/default) and that problem should go away.

You will also need to restart the nginx server: sudo service nginx restart

Code For This Course

Get the code for this course.

Episodes