The definitive guide of Symfony 1.1

3.4. Configuring the Web Server

The scripts of the web/ directory are the entry points to the application. To be able to access them from the Internet, the web server must be configured. In your development server, as well as in a professional hosting solution, you probably have access to the Apache configuration and you can set up a virtual host. On a shared-host server, you probably have access only to an .htaccess file.

3.4.1. Setting Up a Virtual Host

Listing 3-1 is an example of Apache configuration, where a new virtual host is added in the httpd.conf file.

Listing 3-1 - Sample Apache Configuration, in apache/conf/httpd.conf

<VirtualHost *:80>
  ServerName myapp.example.com
  DocumentRoot "/home/steve/myproject/web"
  DirectoryIndex index.php
  Alias /sf /$sf_symfony_data_dir/web/sf
  <Directory "/$sf_symfony_data_dir/web/sf">
    AllowOverride All
    Allow from All
  </Directory>
  <Directory "/home/steve/myproject/web">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

In the configuration in Listing 3-1, the $sf_symfony_data_dir placeholder must be replaced by the actual path. For example, for a PEAR installation in *nix, you should type something like this:

Alias /sf /usr/local/lib/php/data/symfony/web/sf

Note The alias to the web/sf/ directory is not mandatory. It allows Apache to find images, style sheets, and JavaScript files for the web debug toolbar, the admin generator, the default symfony pages, and the Ajax support. An alternative to this alias would be to create a symbolic link (symlink) or copy the /path/to/symfony/data/web/sf/ directory to myproject/web/sf/.

Restart Apache, and that's it. Your newly created application can now be called and viewed through a standard web browser at the following URL:

http://localhost/frontend_dev.php/

You should see a congratulations page similar to the one shown earlier in Figure 3-1.

3.4.2. Configuring a Shared-Host Server

Setting up an application in a shared host is a little trickier, since the host usually has a specific directory layout that you can't change.

Caution Doing tests and development directly in a shared host is not a good practice. One reason is that it makes the application visible even if it is not finished, revealing its internals and opening large security breaches. Another reason is that the performance of shared hosts is often not sufficient to browse your application with the debug tools on efficiently. So you should not start your development with a shared-host installation, but rather build your application locally and deploy it to the shared host when it is finished. Chapter 16 will tell you more about deployment techniques and tools.

Let's imagine that your shared host requires that the web folder is named www/ instead of web/, and that it doesn't give you access to the httpd.conf file, but only to an .htaccess file in the web folder.

In a symfony project, every path to a directory is configurable. Chapter 19 will tell you more about it, but in the meantime, you can still rename the web directory to www and have the application take it into account by changing the configuration, as shown in Listing 3-2. These lines are to be added to the end of the config/ProjectConfiguration.class.php file.

Listing 3-2 - Changing the Default Directory Structure Settings, in lib/ProjectConfiguration.class.php

class ProjectConfiguration extends sfProjectConfiguration
{
   public function setup()
   {
     $this->setWebDir($this->getRootDir().'/www');
   }
}

The project web root contains an .htaccess file by default. It is shown in Listing 3-3. Modify it as necessary to match your shared host requirements.

Listing 3-3 - Default .htaccess Configuration, Now in myproject/www/.htaccess

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
  RewriteEngine On

  # uncomment the following line, if you are having trouble
  # getting no_script_name to work
  #RewriteBase /

  # we skip all files with .something
  #RewriteCond %{REQUEST_URI} \..+$
  #RewriteCond %{REQUEST_URI} !\.html$
  #RewriteRule .* - [L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

You should now be ready to browse your application. Check the congratulation page by requesting this URL:

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