Thursday, January 20, 2011

How to install symphony php?


Prerequisites

Before installing symfony, you need to check that your computer has everything installed and configured correctly. Take the time to conscientiously read this chapter and follow all the steps required to check your configuration.

Software

First of all, you need to check that your computer has a friendly working environment for web development. At a minimum, you need a web server (Apache, for instance), a database engine (MySQL, PostgreSQL, SQLite, or any PDO-compatible database engine), and PHP 5.2.4 or later.

Command Line Interface

The symfony framework comes bundled with a command line tool that automates a lot of work for you. If you are a Unix-like OS user, you will feel right at home. If you run a Windows system, it will also work fine, but you will just have to type a few commands at the cmd prompt.

PHP Configuration

PHP configurations can vary a lot from one OS to another, or even between different Linux distributions, you need to check that your PHP configuration meets the symfony minimum requirements.

First, ensure that you have PHP 5.2.4 at a minimum installed by using the phpinfo() built-in function or by running php -v on the command line. some configurations, you might have two different PHP versions installed: one for the command line, and another for the web.
Then, download the symfony configuration checker script at the following URL:
http://sf-to.org/1.2/check.php
Save the script somewhere under your current web root directory.
Launch the configuration checker script from the command line:
$ php check_configuration.php

If there is a problem with your PHP configuration, the output of the command will give you hints on what to fix and how to fix it. also execute the checker from a browser and fix the issues it might discover. That's because PHP can have a distinct php.ini configuration file for these two environments, with different settings.

Initializing the Project Directory

Before installing symfony, you first need to create a directory that will host all the files related to your project:
$ mkdir -p /home/sfproject
$ cd /home/sfproject
Or on Windows:
c:\> mkdir c:\dev\sfproject
c:\> cd c:\dev\sfproject

Choosing the Symfony Version

Now, you need to install symfony. As the symfony framework has several stable versions, you need to choose the one you want to install by reading the installation page on the symfony website.

Choosing the Symfony Installation Location

You can install symfony globally on your machine, or embed it into each of your project. The latter is the recommended one as projects will then be totally independent from each others. Upgrading your locally installed symfony won't break some of your projects unexpectedly. It means you will be able to have projects on different versions of symfony, and upgrade them one at a time as you see fit.

Installing Symfony

Installing from an archive

The easiest way to install symfony is to download the archive for the version you choose from the symfony website. Go to the installation page for the version you have just chosen, symfony 1.4 for instance.
Under the "Download as an Archive" section, you will find the archive in .tgz or in .zip format. Download the archive, put it under the freshly created lib/vendor/ directory, un-archive it, and rename the directory to symfony:
$ cd lib/vendor
$ tar zxpf symfony-1.4.0.tgz
$ mv symfony-1.4.0 symfony
$ rm symfony-1.4.0.tgz
Under Windows, unzipping the zip file can be achieved using Windows Explorer. After you rename the directory to symfony, there should be a directory structure similar to c:\dev\sfproject\lib\vendor\symfony.

Installing from Subversion (recommended)

If you use Subversion, it is even better to use the svn:externals property to embed symfony into your project in the lib/vendor/ directory:
$ svn pe svn:externals lib/vendor/
If everything goes well, this command will run your favorite editor to give you the opportunity to configure the external Subversion sources.
If you are conservative, tie your project to a specific release (a subversion tag):
svn checkout http://svn.symfony-project.com/tags/RELEASE_1_4_0
Whenever a new release comes out (as announced on the symfony blog), you will need to change the URL to the new version.
If you want to go the bleeding-edge route, use the 1.4 branch:
svn checkout http://svn.symfony-project.com/branches/1.4/

Project Creation

From the sfproject/ directory, run the symfony generate:project task to actually create the symfony project:
$ php lib/vendor/symfony/data/bin/symfony generate:project PROJECT_NAME
On Windows:
c:\> php lib\vendor\symfony\data\bin\symfony generate:project PROJECT_NAME
The generate:project task generates the default structure of directories and files needed for a symfony project:


Directory
Description
apps/
Hosts all project applications
cache/
The files cached by the framework
config/
The project configuration files
data/
Data files like initial fixtures
lib/
The project libraries and classes
log/
The framework log files
plugins/
The installed plugins
test/
The unit and functional test files
web/
The web root directory (see below)
 
The generate:project task has also created a symfony shortcut in the project root directory to shorten the number of characters you have to write when running a task.
So, from now on, instead of using the fully qualified path to the symfony program, you can use the symfony shortcut.

Installation Verification

Now that symfony is installed, check that everything is working by using the symfony command line to display the symfony version (note the capital V):
$ cd ../..
$ php lib/vendor/symfony/data/bin/symfony -V
On Windows:
c:\> cd ..\..
c:\> php lib\vendor\symfony\data\bin\symfony -V
The -V option also displays the path to the symfony installation directory, which is stored in config/ProjectConfiguration.class.php.
If the path to symfony is an absolute one (which should not be by default if you follow the above instructions), change it so it reads like follows for better portability:
// config/ProjectConfiguration.class.php
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
That way, you can move the project directory anywhere on your machine or another one, and it will just work.
If you are curious about what this command line tool can do for you, type symfony to list the available options and tasks:
$ php lib/vendor/symfony/data/bin/symfony
On Windows:
c:\> php lib\vendor\symfony\data\bin\symfony
The symfony command line is the developer's best friend. It provides a lot of utilities that improve your productivity for day-to-day activities like cleaning the cache, generating code, and much more.

Configuring the Database

The symfony framework supports all PDO-supported databases (MySQL, PostgreSQL, SQLite, Oracle, MSSQL, ...) out of the box. On top of PDO, symfony comes bundled with two ORM tools: Propel and Doctrine.
When creating a new project, Doctrine is enabled by default. Configuring the database used by Doctrine is as simple as using the configure:database task:
$ php symfony configure:database "mysql:host=localhost;dbname=dbname" root mYsEcret

The configure:database task takes three arguments: the PDO DSN, the username, and the password to access the database. If you don't need a password to access your database on the development server, just omit the third argument.

 

Application Creation

Now, create the frontend application by running the generate:app task:
$ php symfony generate:app frontend
Because the symfony shortcut file is executable, Unix users can replace all occurrences of 'php symfony' by './symfony' from now on.
On Windows you can copy the 'symfony.bat' file to your project and use 'symfony' instead of 'php symfony':
c:\> copy lib\vendor\symfony\data\bin\symfony.bat .
Based on the application name given as an argument, the generate:app task creates the default directory structure needed for the application under the apps/frontend/ directory:
Directory
Description
config/
The application configuration files
lib/
The application libraries and classes
modules/
The application code (MVC)
templates/
The global template files

Directory Structure Rights

Before trying to access your newly created project, you need to set the write permissions on the cache/ and log/ directories to the appropriate levels, so that your web server can write to them:
$ chmod 777 cache/ log/
Tips for People using a SCM Tool
symfony only ever writes in two directories of a symfony project, cache/ and log/. The content of these directories should be ignored by your SCM (by editing the svn:ignore property if you use Subversion for instance).

Web Server Configuration

Now it is time to change your Apache configuration, to make the new project accessible to the world.
Locate and open the httpd.conf configuration file and add the following configuration at the end:
# Be sure to only have this line once in your configuration
NameVirtualHost 127.0.0.1:8080
 
# This is the configuration for your project
Listen 127.0.0.1:8080
 
  DocumentRoot "/home/sfproject/web"
  DirectoryIndex index.php
  
    AllowOverride All
    Allow from All
  
 
  Alias /sf /home/sfproject/lib/vendor/symfony/data/web/sf
  
    AllowOverride All
    Allow from All
  
The /sf alias gives you access to images and javascript files needed to properly display default symfony pages and the web debug toolbar.
On Windows, you need to replace the Alias line with something like:
Alias /sf "c:\dev\sfproject\lib\vendor\symfony\data\web\sf"
And /home/sfproject/web should be replaced with:
c:\dev\sfproject\web
This configuration makes Apache listen to port 8080 on your machine, so the website will be accessible at the following URL:
http://localhost:8080/
You can change 8080 to any number, but favour numbers greater than 1024 as they do not require administrator rights.
Configure a dedicated Domain Name
If you are an administrator on your machine, it is better to setup virtual hosts instead of adding a new port each time you start a new project. Instead of choosing a port and add a Listen statement, choose a domain name (for instance the real domain name with .localhost added at the end) and add a ServerName statement:
# This is the configuration for your project
  ServerName www.myproject.com.localhost
  
Add in the following line:
127.0.0.1 www.myproject.com.localhost

Test the New Configuration

Restart Apache, and check that you now have access to the new application by opening a browser and typing http://localhost:8080/index.php/, or http://www.myproject.com.localhost/index.php/ depending on the Apache configuration you chose in the previous section.




 
You should also try to access the application in the development environment (see the next section for more information about environments). Type in the following URL:
http://www.myproject.com.localhost/frontend_dev.php/



The web debug toolbar should show in the top right corner, including small icons proving that your sf/ alias configuration is correct.

 


No comments:

Post a Comment