Article

Installing and configuring Symfony2

Symfony2 is a fantastic framework for developing your web applications and services and installing and configuring Symfony2 is extremely easy.  With the use of composer and the Symfony installer you can get started in just a couple of minutes and with only a few simple commands.

Installing Composer

Symfony2 uses composer as the package manager for managing all of the dependencies within your applications and is a prerequistie to developing with Symfony2 so we will install this first.

Github Gist Placeholder

Creating your Symfony2 Application

Next we will create your Symfony2 application.  This can be done using the following steps:

1) Download the Symfony installer

Github Gist Placeholder

2) Create your Symfony2 Application

Github Gist Placeholder

3) Set the appropriate permissions

Symfony2 has various files which must be writeable by both the command line and the web server, however these often run as different users and you can end up with file permissions issues as a result.  To resolve that you need to allow your user account and the web server user account (apache, nginx etc) to be able to write and overwrite each others file.  This can be done using the following commands:-

Github Gist Placeholder

If you system does not support the chmod command you can find alternatives in the Symfony2 installation documentation.

4) Launch your application with the built-in PHP web server (PHP >= 5.4.0)

With our file permissions configured we can now use PHP's built in web server to quickly check that the site works correctly and Symfony2 provides a nice wrapper command to run your application with it.

Github Gist Placeholder

Now open up http://localhost:8000 in a browser and you should see the Symfony2 welcome page.

Symfony2 Welcome Screen

5) Setup database connection

Your application will likely need database access and Symfony2 comes set up to use Doctrine2 with MySQL straight out of the box.  To get your application to use a MySQL database you simply need to add the correct details for your MySQL server & database in app/config/parameters.yml .

Once the settings have been updated you can verify they are correct by running the following command.

Github Gist Placeholder

If it is is all configured correctly then you should be presented a screen similar to the one below

database-success.jpg

And if there is a problem with your database configuration it will be fairly obvious to see:

database-error.jpg

While this has tested the database connection works we have not tried to insert or retrieve any data (as we do not have any database tables set up yet) and that is a topic for another day.  With the following step complete you now have a basic installation of Symfony2 up and running.

Installing the Symfony2 Demo Application

Symfony2 can be quite complicated, especially if you are trying it out for the first time.  Thankfully there is a demo application which can be downloaded which is designed to help make the transition into Symfony2 a bit easier.  The demo application has loads of comments and notes to help you understand what is going and you can install it using the commands below:-

Github Gist Placeholder

And if you now open a browser and go to http://localhost:8000 you should get:

symfony2-demo-bundle.jpg

Once the demo application is installed you only need to run the last 2 steps to get it running in future.

Please note that the demo application is not a bundle within your application (like the AcmeDemoBundle was) but it is an application all of it's own which you can poke and prod and disassemble as much as you like so you can gain a better understanding on how everything works together.

Set up an Apache / nginx Web Server

Using PHP's built-in web server is extremely useful to quickly test your setup and is recommended for development with Symfony2, however I have always preferred to configure a proper web server such as Apache or nginx to run my application and use a fully qualified domain name rather than using http://localhost:8000.

It is important to understand how to set up your application on a webserver such as Apache or nginx as even if you are happy using the build in web server for development, it is not recommended to use it in a production enviroment.  

Granted there is a little more configuration involved but it does provides me with a development environment which matches my production environment as closely as possible and allows me to rule out mis-configurations with things like

  • running multiple applications in development i.e. a main application and one or more micro services 
  • issues with web server directives i.e. htaccess works fine in development but production is running nginx so does not use the .htaccess file

In my next post I will cover how to set up a development enviroment with a fully qualified domain name using Apache or nginx.

me.jpeg

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for the The Drum in Glasgow, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!

Want more like this?

Keep up-to-date with latest news from Inside the Agency using any of the following services

Recent articles

Behind the Paywall - An Insider Point of View

Over the years, a number of publishers have tried their hand at implementing a paywall. These efforts have been met with varying degrees of success:

paywall.jpg

The Sun introduced a paywall on the 1st August 2013, partially removed it in the summer of 2015 and removed it completely on the 30th November 2015. The Financial Times introduced its paywall in 2007, however, they chose to relax the paywall in 2015 in an effort to combat sluggish growth and The Times introduced its paywall in 2010, which recent news suggests it be performing well.

Read more

Four things that suck about project management

Project management is not a glamorous job.   The better you are, the less you’re noticed: things just hum along nicely, with no major conflicts or serious misunderstandings.  If you’re diligent, skilled, dedicated and competent, someone else will get credit.  The software, application, website or rebuild you so lovingly slaved over will be attributed to your boss, your client, or – if you’re lucky – to your developers or creatives.

In fact, the only time a project manager can rely on getting any real attention is when things go wrong.  A host of complex issues may be present – problematic management behaviour, shifting requirements, lack of necessary cooperation from the wider business, underinvestment in skill or resource – but the blame is ultimately yours.  You’re the project manager.

Read more

“My boss doesn’t get it.” The puzzle of motivation.

Management roles have traditionally focused on issuing assignments and offering rewards.  For much of the industrial age, this worked well.  In the vast majority of situations, people do work harder if they know they’ll get a financial bonus or more recognition.  Primed with 100 years of business advice, managers feel they have the tools they need to get things done, retain their staff and hit their targets.

carrot.jpg

But when they find themselves at the helm of a team of developers, or a mixed team of designers and developers, managers get confused.  The promise of rewards only goes so far.   Sometimes, incentives fail to generate any interest and may even lead to resistance.  Developers who never voice any dissatisfaction put in their notice and move on without any explanation, leaving you scratching your head.  It’s really confusing, and there are no easy answers. 

Read more

Advertisement

Digital Ocean

Chris's Reading List

www.thoughtworks.com

BFF @ SoundCloud

Our friends at SoundCloud embarked on a journey a few years ago that changed their system and moved it from a monolithic Rails application towards an ecosystem of microservices. Read more

The Drum

The Drum

Google'€™s AMP is bringing some much needed speed to mobile but the transition won'€™t be easy for publishers

A week after launching, the Google-led Accelerated Mobile Pages (AMP) initiative has raised more questions than answers for publishers... Read more

SitePoint

SitePoint

Quick Tip: How to Permanently Change SQL Mode in MySQL

MySQL 5.7 introduced some awkward changes for older codebases and tends to break apps. Here's how to temporarily (or permanently) lower the strictness level Read more

SitePoint

SitePoint

How to Build Your Own Dependency Injection Container

Andrew Carter builds a Dependency Injection container from scratch, showing you how that rocket science is really just fireworks strapped to lego figures! Read more

AuthorityLabs

AuthorityLabs

13 Reasons Your Organic Traffic is in Decline | It's Not a Penalty

The landscape of search engine optimization has changed drastically in the last 3-4 years. Even as it changes, a lot stays the same. It has been very challenging to see a multitude of friends and colleagues approach me with problems that often seem beyond repair with clients that they have worked with for years. Read more

TechCrunch

TechCrunch

WordPress Sites Now Support Google’s AMP To Make Mobile Pages Load Much Faster

Google has some big plans when it comes to making the web faster on your mobile phone. The company just added AMP-enabled pages in its mobile search results... Read more