Symfony CMS - day 01 - Symfony installation

Sep 25th, 2013

A quick Symfony base installation.

First day of our Build CMS Application with Symfony articles suite.

Pre-requisites

Let’s go for the base install

Verify the last version of Symfony on the Download page

Create a tuto.dev virtual hosts

cd /home/www/tuto.dev

# here the last parameter "2.4.4" is the version previously checked
composer create-project symfony/framework-standard-edition . 2.4.4

During this install you will be prompted for some settings (database, etc …). You can validate all the default, we’ll take care of this later.

Setting files permissions

I use this code to set permissions on my dev box. Other ways can be found in Symfony install doc.

cd /home/www/tuto.dev
rm -rf app/cache/*
rm -rf app/logs/*
mkdir app/data # will hosts our database
APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd' | grep -v root | head -1 | cut -d\  -f1`
sudo setfacl -R -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs app/data
sudo setfacl -dR -m u:$APACHEUSER:rwX -m u:`whoami`:rwX app/cache app/logs app/data

You can now point to http://tuto.dev/config.php/ to check our configuration. Then make appropriates change to your /etc/php5/apache2/php.ini file.

Note : On ubuntu 12.04 xdebug config is done in /etc/php5/apache2/conf.d/20-xdebug.ini.

sudo service apache2 restart

You also have to make the check on CLI side as the php.ini file used is different.

# can be useful
chmod 777 app/check.php
php app/check.php

Then make appropriates change to your /etc/php5/cli/php.ini file.

If you want to upgrade your php to a newest version you can read my Install PHP 5-5-4 on Ubuntu 12-04.

One more

sudo service apache2 restart

and your done. Nearly.

You can already see your page at http://tuto.dev/app_dev.php.

Some more setup

Database, mail and various parameters

For performance reasons during tests, we want to use Sqlite database. This will allow us to use in memory db and to copy paste a fresh db between each test suite.

Setup is the following :

# app/config/parameters.yml
# This file is auto-generated during the composer install
parameters:
    database_driver: pdo_sqlite
    database_host: null
    database_port: null
    database_name: null
    database_user: null
    database_password: null
    database_path: '%kernel.root_dir%/data/database.sqlite'
    database_memory: true
    mailer_transport: smtp
    mailer_encryption: ssl
    mailer_host: in.mailjet.com
    mailer_user: mailjetuser
    mailer_password: mailjetpassword
    locale: en
    secret: areallylongsecretkey

You also need to uncomment the path line in app/config/config.yml and to add the memory line.

# app/config/config.yml
[...]
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
        path:     %database_path%
        memory:   %database_memory%

Create the database.

    app/console doctrine:database:create
    chmod 664 app/data/database.sqlite

Finally uncomment translator line :

# app/config/config.yml
[...]
framework:
    #esi:             ~
    translator:      { fallback: %locale% }

Hacking composer.json

Two little things about composer and composer update :

  • Composer makes a hard copy of your assets (css, js, img) in the web/ folder. When you work on Linux you can just make symlinks. So we will add a “symfony-assets-install”: “symlink” directive in composer.json.
  • Composer verifies that your parameters are conform to config.yml.dist template. So it deletes path and memory lines in our parameter.yml. We will add a “keep-outdated”: true incenteev-parameters line in composer.json in order to keep our sqlite settings (path and memory). If you don’t do this, doctrine.dbal.path and doctrine.dbal.memory are deleted from your parameter.yml file each time you do a composer update.

Add two lines in composer.json

    [...]
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml",
            "keep-outdated": true
        },
        "branch-alias": {
            "dev-master": "2.3-dev"
        },
        "symfony-assets-install": "symlink"
    }
    [...]

Version the code

Yes we are supposed to work with versioning even when working alone. I personally work with git-flow.

    git init
    git add .
    git commit -m "Symfony initial install"

Et voilà ! Next step will be to install Behavior Driven Development tools


Comments

comments powered by Disqus