Christopher Shennan's Blog

A day in the life of…

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.

About Christopher Shennan

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for Line Digital in Edinburgh, 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!

Connect with me via Twitter, Google+ or LinkedIn

  • #1 | Written by Scott Mugford about 1 year ago.

    nice work, very concise and easy to follow.

    The only thing i’d add is a little more information for those that are crazy enough to develop on a windows machine (or that have no choice).

    the same setup of WAMP or XAMPP that worked with symfony 1.x will work with Symfony 2. (as long as the php/mysql version meet the S2 requirements ) just google “setup symfony on windows”

    the other two requirements for dev on the windows machines are:

    msysgit and cygwin

    That being said if you CAN develop on Linux don’t bother with Windows.

  • #2 | Written by Christopher Shennan about 1 year ago.

    Hi Scott

    Thank you for your feedback.

    So far I’ve managed to stick to a linux web server (although my local machine is Windows) which is mainly so that it mirrors the live server as closely as possible and hopefully reduces those unexpected issues what arise due to developing on Windows and running on a live linux server. I even ran linux in VirtualBox on my local machine at one point so that I didn’t have to develop on WAMP.

    I’ll see if I can have a look at configuring Symfony2 on Windows (and what challenges it throws at me) and post up another article dealing with that.

    Many thanks.

    Chris

  • #3 | Written by Suresh Khanal about 1 year ago.

    Thank you for the conscise and clear procedure. I hope I’ll be able to install and configure it now. I am using Windows machine and guess needs to digg a bit more to run with my IIS.
    Suresh Khanal´s last [type] ..Tricks on Blog Commenting, You’ve Not Heard Of

  • #4 | Written by Arnold about 1 year ago.

    appreciated post ! waiting for your article on configuring Symfony2 on Windows

    SEO Services

No trackbacks yet.

Leave a Comment

Subscribe to comments

CommentLuv badge