Symfony2 Tutorial Part 1: Project Setup

About this tutorial:

In this tutorial I will show you how to setup a new symfony2 project on a local webserver. I use Linux Mint 17 with the following packages installed:

apache2 apache2-mpm-prefork curl mysql-server php-apc php5 php5-cli php5-intl php5-sqlite php5-xdebug php5-xsl phpmyadmin

Lets get started.

Install symfony2

  1. Install composer:
    http://getcomposer.org/download/
    I use the following command as root:

    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin
  2. Create a MySQL database:
    On your MySQL console:

    CREATE DATABASE symfony;
    GRANT ALL ON symfony.* TO symfony@'localhost' IDENTIFIED BY 'pa$$word';
    
  3. Get symfony2:
    Open a terminal and type

    composer.phar create-project symfony/framework-standard-edition /your/symfony/dir '2.5.*'

    HINT: You can replace '2.5.*' with your preferred symfony2 version. For example use '2.3.*' if you want to use the LTS version.

  4. Configure ACL:
    How to install and configure acl: http://wiki.ubuntuusers.de/ACL
    Open a terminal and type in the following commands.

    cd /your/symfony/dir
    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 -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
    sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
    

Setup your webserver

  1. Create /etc/apache2/sites-available/symfony.conf as root with the following content.
    <VirtualHost *:80>
    	ServerName symfony
            ServerAlias symfony.yourhostname
    
    	ServerAdmin webmaster@localhost
    	DocumentRoot /your/symfony/dir/web
    
    	<Directory />
    		DirectoryIndex app.php
    		Options FollowSymLinks
    		AllowOverride All
    		Require all granted
    	</Directory>
    	<Directory "/your/symfony/dir/web">
    		Options Indexes FollowSymLinks MultiViews
    		AllowOverride All
    		Require all granted
    	</Directory>
    
    	ErrorLog ${APACHE_LOG_DIR}/symfony_error.log
    	CustomLog ${APACHE_LOG_DIR}/symfony_access.log combined
    
    </VirtualHost>
    
    
  2. run
    sudo a2ensite symfony

    and

    sudo service apache2 reload
  3. edit /etc/hosts and add:
    127.0.0.1 symfony.yourhostname symfony

HINT: The apache mod rewrite is not enabled by default. You enable it with:

sudo a2enmod rewrite
sudo service apache2 restart

HINT: you can check if you have configured your server correctly with:

cd /your/symfony/dir
php app/check.php

In most cases the date.timezone parameter in both /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini is not set. In my case it’s: date.timezone = "Europe/Berlin".
You can find a list of different timezones here: http://php.net/manual/en/timezones.php

Access your symfony2 application

Thats it. You can now access your symfony2 application (development) via
http://symfony.yourhostname/app_dev.php
And the production environment via
http://symfony.yourhostname

At this point you will get an 404 error because you don’t have any content to be shown yet. Don’t worry we will add something in the next part.

One thought on “Symfony2 Tutorial Part 1: Project Setup”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">