How to Set up Symfony

How to Set up Symfony

FFW Marketing
Thought byFFW Marketing
May 08, 2014
Laptop with a bullseye on the screen

First things first...before we start we need a working AMP stack such as Mamp for the Mac, Xampp, WampServer or Wamp for Windows, or Ampps for Windows, Mac and Linux. You could also install Acquia Dev Desktop which provides an Amp stack....next we need to make sure we have at least php 5.3.3 or greater, which is the minimum required for Symfony to work. This article does not cover the installation of the Amp stack.

The next thing we need is to install Composer from https://getcomposer.org/ --it's the recomended way to install Symfony and its different components. Composer is a package manager for Symfony that will resolve dependencies automatically.

I like to put my utilities in a local lib directory and make it available via a currently accessible path by making a symlink to it in the /usr/local/bin which I've already have to my path, so I'll navigate there before I install Composer:

$ cd /usr/local/lib
$ curl -sS https://getcomposer.org/installer | php
$ cd ../bin
$ ln -s ../lib/composer.phar composer

We need to set a new host in our hosts file:

127.0.0.1 symfony.air<

and a new virtual host in our apache config:

<VirtualHost *:80>
  ServerAdmin wes@wes.air
  DocumentRoot "/var/www/symfony/web"
  ServerName symfony.air
  ServerAlias symfony.air
  <Directory "/var/www/symfony/web">
    AllowOverride All
    Options All
    Order allow,deny
    Allow from all
  </Directory>
  ErrorLog "/private/var/log/apache2/symfony.air-error_log"
  CustomLog "/private/var/log/apache2/symfony.air-access_log" combined
</VirtualHost>

so I will navigate to /var/www/ and create a Symfony project

$ php composer create-project symfony/framework-standard-edition thepathtoyourprojectname/ 2.4.4

I named mine symfony. The filesystem should look like this:

path/to/webroot/ <- your web server directory (sometimes named htdocs or public)
  Symfony/ <- the new directory
    app/
      cache/
      config/
      logs/
    src/
      ...
    vendor/
      ...
    web/
      app.php
      ...

We need to make sure that the permissions are set properly for symfony to write to certain files and directories

$ rm -rf app/cache/*
$ rm -rf app/logs/*

then

$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`

$ sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs

$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs

OR

$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`

$ sudo setfacl -Rn -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs

$ sudo setfacl -dRn -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs

next run check.php to make sure everything is good

$ php app/check.php

Now we can navigate to our web root, and we can start our Symfony journey.

symfony.air/config.php

in your browser. This does another check of your system and notifies you if you have any problems, or it gives you the link to configure your app and a link to go to the welcome page. Before we click on the config link, let's make sure that the file it will write to is writeable.

To do this, we must first make sure that we use the group that the apache server is running under. We can check which group that is by looking in the app directory to see what the cache and logs directories were changed to.

$ cd app/config
$ chgrp _www parameters.yml
$ chmod 775 parameters.yml

When we click on the config link "Configure your Symfony Application online", we see this screen:
 

Configure Symfony Application Online

 
If for some reason you see error screens, just fix whatever you can. It's usually a group or permissions problem. I wouldn't go changing group or permissions willy nilly, just the ones that are giving us a problem.

Now we're given the ability to configure our database. Fill in the values that suit your environment and click 'next step.' You should see this screen:
 

Configure Symfony Application Online Next Steps

 
Now we should enter or generate a secret (seed) for encryption purposes, and click next step and we get a confirmation that our parameters.yml file has been written for us to use in our app:
 

Symfony Configuration parameters.yml

 
Let's check things out! Go to http://symfony.air/app_dev.php/ in your browser to see the welcome page:
 

Symfony Welcome Page

 
That should do it! 


Want more Symfony2? Join us for our DrupalCon Austin training class, "Introduction to Symfony2: Getting ready for D8," where FFW Center of Excellence trainers will be joined by Jesus Manuel Olivas (jmolivas) to get you ready for this big change to Drupal development.