My third post on this topic covers the final step which is to set up our php framework of choice, Symfony 2. You can read part one here and part 2 here if you want to know how I arrived at this point.
Thanks to projects like Packagist & Composer, maintaining dependencies and getting an application environment up and running for PHP has become very easy. The rest of this article will explain how to create a new Symfony project based on the Symfony standard edition with Composer and install all the necessary software to run a Symfony application. The following will assume some experience of working with the Symfony framework.
Install Composer and Symfony
The latest and greatest version of the Symfony framework is available through composer, so first we should install composer on our guest machine. I prefer to install it globally. Again, the following commands should be run on the guest machine
Now composer has been installed we can create a new project:
This command will create the folder
my-app and download the Symfony framework, plus all the dependencies from Packagist. You will get an interactive prompt to create the
parameters.yml file and you can accept the defaults and give it the database credentials for the root user you set up earlier.
Install extensions and configure PHP for Symfony
Symfony 2 has some required and optional requirements for running an application. Most of these should be covered by the LAMP stack we have installed, and to verify that the standard edition comes equipped with a tool for checking those requirements:
A PHP accelerator is listed by the checker as an optional extension, however I would consider an opcode cache a requirement for any PHP application being released into the wild. We want to replicate our production environment as closely as possible so lets install it.
$ sudo apt-get install php-apc php5-intl
I sneaked the intl extension in there, which is a must if you are doing any translations. If you run
php app/check.php now we should only have configuration related errors/warnings.
php.ini file can be a little overwhelming at first but don’t worry, it’s pretty easy to update. Symfony asks for the php
short_open_tag setting to be turned off, and a timezone specified via the
We have two
php.ini files, one for the cli, and one for Apache; so we want to edit both.
In both these files you need to edit the relevant setting (in nano you can use
Ctrl + W to search). Make sure you un-comment (remove
;) the date.timezone line.
After editing any apache/php configuration files you will need to restart/reload Apache:
$ sudo service apache2 [restart|reload]
Create a VirtualHost
Apache isn’t yet doing what it’s supposed to do – we need to give it an application to serve! We do that by creating a virtual host, giving the application a name (domain) and giving Apache the path to the files.
Apache VirtualHosts are found in
/etc/apache2/sites-available and Apache comes with two command line tools for enabling/disabling them
Create a new file for the VirtualHost
$ sudo nano /etc/apache2/sites-available/my-app.com
Fill it with the following
And then enable it and reload Apache
$ sudo a2ensite my-site.com $ sudo service apache2 reload
Edit & access your app from your host machine
The final piece of this puzzle is accessing the app in your browser and editing the files on your (host) machine.
In order to access your application in your browser you’ll need to tell your OS that the server name we have told Apache to use
local.my-app.com is located on our Debian installation on VirtualBox. This is done by adding an entry to
/etc/hosts the server name, and the IP of the guest.
$ sudo nano /etc/hosts
Because the files are all on your local machine (and loaded by the guest via NFS) you can open up your favourite IDE/text editor and edit the files (my app can be found at
Symfony has some rudimentary checks in place to prevent people from accessing the development or config scripts on a live site, you’ll need to edit
web/app_dev.php to allow access (check my comments).
Now we can check your installation of Symfony is all correct by visiting http://local.my-app.com/config.php. This is the web version of the
php app/check.php script we ran earlier, if you’ve followed all the instructions then this should be green! Check out the demo page of your app for a quick introduction to Symfony.
Getting rid of app.php (install mod_rewrite)
The production environment of a symfony application is by default made available through the
web/app.php front controller. Now we do not necessarily want that cluttering up our urls and to remedy that Apache comes with a module called
This can be installed with the following command:
$ sudo a2enmod rewrite $ sudo service apache2 restart
The Symfony standard edition comes packed with the
web/.htaccess for configuring mod_rewrite and we specified
AllowOverride All in our virtual host, so now if we go to http://local.my-app.com
app.php should have disappeared.
I hope you’ve found this final instalment useful, please drop me a line with any comments.