Installing and configuring Symfony2

23rd February 2012

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 it into 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 the 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 https://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 "https://" 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 webserver to run the Symfony project via https://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 https://localhost/web/config.html

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 an 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 webserver 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.html
        RewriteRule ^(.*)$ app.html
        RewriteRule ^(.*)$ app_dev.html
        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.');
        }

        ...
        </Directory>
</VirtualHost>

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

Liked this article?

Subscribe to my newsletter and get the latest content direct to your inbox