Symfony: 1 – Installation

PHP 5.4 installation

Using the Symfony Installer is the only recommended way to create new Symfony applications. This installer is a PHP application that has to be installed in your system only once and then it can create any number of Symfony applications.

sudo curl -LsS http://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

PHP 5.3 installation

Go to getcomposer.org. Then go to Symfony. Run in terminal:

curl -s https://getcomposer.org/installer | php
php composer.phar create-project symfony/framework-standard-edition symtest/ 2.4.2

Advanced installation

Symfony packages “distributions”, which are fully-functional applications that include the Symfony core libraries, a selection of useful bundles, a sensible directory structure and some default configuration. When you download a Symfony distribution, you’re downloading a functional application skeleton that can be used immediately to begin developing your application. Start by visiting the Symfony download page at http://symfony.com/download. On this page, you’ll see the Symfony Standard Edition, which is the main Symfony distribution. There are 2 ways to get your project started:

Composer
Composer (http://getcomposer.org) is a dependency management library for PHP, which you can use to download the Symfony Standard Edition. Composer takes care of downloading the correct version and all its dependencies and provides an autoloader. Beside downloading your dependencies, Composer generates a vendor/autoload.php file, which takes care of autoloading for all the files in the Symfony Framework as well as the files mentioned in the autoload section of your composer.json. Start by downloading Composer anywhere onto your local computer. If you have curl installed, it’s as easy as:

curl -s https://getcomposer.org/installer | php

Composer is an executable PHAR file, which you can use to download the Standard Distribution. To download the vendor files faster, add the –prefer-dist option at the end of any Composer
command.

$ php composer.phar create-project symfony/framework-standard-edition /path/to/webroot/Symfony 2.4.*

By default, the assets will be copied into your web directory. Instead of copying your Symfony assets, you can create symlinks if your operating system supports it. To create symlinks, add an entry in the extra node of your composer.json file with the key symfony-assets-install and the value symlink:

{
  "require": {
    "symfony/symfony": "2.4.*"
  },
  "autoload": {
    "files": ["model.php","controllers.php"]
  },
  "extra": {
    "symfony-app-dir": "app",
    "symfony-web-dir": "web",
    "symfony-assets-install": "symlink"
  }
}

When passing relative instead of symlink to symfony-assets-install, the command will generate relative symlinks.

Setup assets manually
The assets:install command is used to install the web assets (CSS, JavaScript, images) for the production application. When executed without options, the command copies into web/ all the files found in the Resources/public/ directories of your application and bundles. Most of the time it’s better to execute it with the –symlink option. This makes a symbolic link of your assets instead of actually copying their files.

# make a hard copy of the assets in web/
$ php app/console assets:install
 
# if possible, make absolute symlinks in web/ if not, make a hard copy
$ php app/console assets:install --symlink
 
# if possible, make absolute symlinks in html/ if not, make a hard copy
$ php app/console assets:install html --symlink
 
# if possible, make relative symlinks in web/ if not, make a hard copy
$ php app/console assets:install --symlink --relative

Download an archive
You can also download an archive of the Standard Edition. Here, you’ll need to make two choices:

  • Download either a .tgz or .zip archive – both are equivalent, download whatever you’re more comfortable using
  • Download the distribution with or without vendors. If you’re planning on using more thirdparty libraries or bundles and managing them via Composer, you should probably download “without vendors”
wget -O symfony.tgz http://symfony.com/download?v=Symfony_Standard_Vendors_2.4.0.tgz
tar -zxvf symfony.tgz

All public files and the front controller that handles incoming requests in a Symfony2 application live in the Symfony/web/ directory. So, assuming you unpacked the archive into your web server’s or virtual host’s document root, your application’s URLs will start with http://localhost/Symfony/web/. If you’ve downloaded “without vendors”, you’ll Composer to download these because a Symfony project depends on a number of external libraries. Symfony2 has a tester available at the following url:

http://www.example.com/config.php

If you run into the following error: “This script is only accessible from localhost”, it means you are trying to access that file remotely. Open the config.php file found in the /web folder and Find the following block:

if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
))) {

Here you need to add a new array value with the IP you would like Symfony to allow access for:

if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
    'YOUR IP',
))) {

At this point, all of the needed third-party libraries now live in the vendor/ directory. You also have a default application setup in app/ and some sample code inside the src/ directory.
Symfony2 comes with a visual server configuration tester to help make sure your Web server and PHP are configured to use Symfony. Use http://localhost/config.php to check your configuration. If there are any issues, correct them now before moving on. When everything is fine, click on “Go to the Welcome page” to request your first “real” Symfony2 webpage (http://localhost/app_dev.php). To get nice and short urls you should point the document root of your webserver or virtual host to the Symfony/web/ directory.

httpd.conf

<VirtualHost *:80>
    ServerName symfony.local
    DocumentRoot /var/www/symfony/web
    DirectoryIndex app.php
    <Directory "/var/www/symfony/web">
        AllowOverride All
        Allow from All
    </Directory>
</VirtualHost>

One common issue is that the app/cache and app/logs directories must be writable both by the web server and the command line user. On debian / ubuntu:

apt-get install acl
APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data' | grep -v root | head -1 | cut -d\  -f1`
setfacl -R -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX app/cache app/logs
setfacl -dR -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX app/cache app/logs

Restart apache and go to http://symfony/app_dev.php. Go to http://symfony/config.php. Make sure you don’t have any Major Problems listed, and follow the optional recommendations listed. Symfony2 comes with the console component tool that you will use for different tasks. To see a list of things it can do for you type at the command prompt:

php app/console list

Leave a Reply