Installing and configuring Symfony2

By

23rd February 2012 | No Comments

In this first part of my mini series about installing and configuring the Sonata Project bundles I will address the prerequiste which is to ensure we have a working Symfony2 project set up and configured.  This will focus on 2 main parts:-

  • The Bare Essentials - Just enough to get it going
  • The Optional Extras - One or 2 nice little touches to help smooth over issues later on.

Hopefully this will be helpful to more than just me :)

The Bare Essentials

Getting a Symfony2 project up and running is fairly easy but I did come across a few bumps in the road when I first attempted it so I am hoping to smooth those bumps out a bit for some of you first timers.  The basic process is:-

Step 1 - Create a project directory

Make a folder on your web server for your Symfony2 project.  I will assume we are using /home/chrisshennan/public_html/symfony2.chris.home.internal/ as our root directory for this tutorial.

Step 2 - Download & Extract the Symfony2 project files

Download the Symfony2 Standard Edition without vendors and extract into the your Symfony2 project folder.  When extracting the Symfony2 project it will create a folder called "Symfony".  Move the contents from "Symfony" into the root directory and delete the "Symfony" folder.   You should now have the main project folders located :-

apps = /home/chrisshennan/public_html/symfony2.chris.home.internal/apps

bin = /home/chrisshennan/public_html/symfony2.chris.home.internal/bin

src = /home/chrisshennan/public_html/symfony2.chris.home.internal/src

etc etc

Step 3 - Install the vendor libraries

To install the vendor libraries, open a command prompt, change directory to your Symfony2 project directory run the following command below:-

php bin/vendors install
At the time of writing, the above command seemed to intermittently produce errors like the one below
Cloning into /home/chrisshennan/public_html/symfony2.chris.home.internal/vendor/symfony... error: The requested URL returned error: 403 while accessing http://github.com/symfony/symfony.git/info/refs fatal: HTTP request failed
This is easily fixed by editing the deps file in a text editor and doing a search and replace and replacing all instances for "http://" with "git://".  Once this has been done then re-run the command above and it should install the vendor bundles properly.  There is quite a lot of data to download and install so this could take several minutes to complete.

Tada

And that is basically it.  If you have configured your web server to run the Symfony project via http://localhost/web then you should be able to go there now and see:-

You can now configure some additional options (like database connection) via the configuration wizard which can be found at http://localhost/web/config.php

The Optional Extras

Apache vhost Configuration

Like many of you, my local machine is not the same as my web server so using localhost is no good to me.  For this tutorial lets assume my web server is running on a host with an IP of 192.168.0.250 but I have many sites in development running on that server so I can't just use http://192.168.0.250 to render the Symfony2 site as I have to consider how to keep the other sites working too.  How can I set up my Symfony2 site to work from my web server? The solution I use works in 2 parts:-
  • Set up a local hosts configuration value to resolve symfony2.chris.home.internal to 192.168.0.250
  • Set up a apache vhost of your web server so that it can deal with requests to symfony2.chris.home.internal

Local Hosts Configuration Value

  • If your local machine is running Windows then edit C:\Windows\System32\drivers\etc\hosts
  • If your local machine is Linux or Mac then edit /etc/hosts
In both cases add the following line to the end of the file
192.168.0.250        symfony2.chris.home.internal

Apache vhost Configuration

Next we need to add a vhost configuration for symfony2.chris.home.internal on our web server and I have included an example of this below.  If you need more information on setting up a vhost configuration on your web server then you should have a read of Apache Virtual Hosts on Ubuntu - Part 1.  This will be slightly biased towards Ubuntu configuration but the general configuration is covered well here.
NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /home/chrisshennan/public_html/symfony2.chris.home.internal/web
    ServerName symfony2.chris.home.internal<code>

    # Custom log file
    Loglevel warn
    ErrorLog /home/chrisshennan/wwwlogs/symfony2.chris.home.internal-error.log
    CustomLog /home/chrisshennan/wwwlogs/symfony2.chris.home.internal-access.log combined

    <Directory /home/chrisshennan/public_html/symfony2.chris.home.internal/web>
        AllowOverride None

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ app.php [QSA,L]

    </Directory>
</VirtualHost>
Although there is a .htaccess file in the web folder which will direct all the requests to app.php, I prefer to disable the .htaccess files and put the configuration in the vhost.  The main reason for this is so that I don't accidentally check in a .htaccess used for a development environment which ends up getting deployed to a live site and cause problems. I have also defined the document root as "/home/chrisshennan/public_html/symfony2.chris.home.internal/web" which means that we will be able to access our Symfony2 project via http://symfony2.chris.home.internal rather than http://symfony2.chris.home.internal/web. The above example is for a web server which is on a remote host but it will work fine if you wish to run multiple sites on localhost as well... simply substitute 127.0.0.1 instead of 192.168.0.250 as appropriate:-

The Development Environment

The basic installation of the Symfony2 project directs all requests to app.php but if you are working on a development site it can be a pain to remember to explicitly type app_dev.php in the URL to use to the development environment and can lead to some confusion when things don't display as you expect. To get around this, edit the vhost file you created in the section above and update the last statement from
RewriteRule ^(.*)$ app.php [QSA,L]
to
RewriteRule ^(.*)$ app_dev.php [QSA,L]
This will now mean that when you access symfony2.chris.home.internal you will always using the development environment unless you explicitly enter app.php in the URL which will more likely suit your needs. However, If you try and access http://symfony2.chris.home.internal now you will be presented with a 403 - Forbidden message which is because app_dev.php is only accessible via localhost by default.  You will need to make a small amendment to the app_dev.php file to allow you to access it from your local machine which is to change
if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
))) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
to
if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1','::1',)) && substr($_SERVER['HTTP_HOST'], -14) != '.home.internal' ) {

    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

And that should be you.  By this point you should be able to:-

  • Install Symfony2 and the vendor libraries
  • Set up a hostname to allow access to your Symfony2 site from a remote web server (or localhost if desired)
  • Set up the development environment as the default environment

I hope this was helpful and I look forward to seeing your comments.

Comments

comments powered by Disqus