Setting up new VirtualHost for Symfony 1.4 project

Posted on Category:Symfony

This post shows an example how to setup apache2 virtual host for symfony 1.4 based project. Although there’s already an documented VirtualHost example on official Symfony website, I think that this virtualHost setup is more practical for development purposes.

Here is my virtualHost file example:

# /etc/apache2/sites-enabled/001-mytestserver
<virtualhost *:80>
  ServerName myservername.test
 
  DocumentRoot "/home/tomislav/projects/myproject/trunk/web"
  DirectoryIndex index.php
  <directory "/home/tomislav/projects/myproject/trunk/web">
    AllowOverride All
    Allow from All
  </directory>
 
  Alias /sf /home/tomislav/projects/myproject/trunk/lib/vendor/symfony/data/web/sf
  <directory "/home/tomislav/projects/myproject/trunk/lib/vendor/symfony/data/web/sf">
    AllowOverride All
    Allow from All
    </directory>
  </virtualhost>

Be sure to configure hosts file, as this configuration won’t work without line:

# /etc/hosts
...
127.0.0.1   myservername.test
...

Also, if you care for performance, you might want to write the mod_rewrite rules in the VirtualHost file, like this:

<VirtualHost *:80>
 
  ServerName myservername.test
  ServerAlias *.myservername.test
 
  DocumentRoot "/usr/local/kliks/trunk/web"
  <Directory "/usr/local/kliks/trunk/web">
     ErrorDocument 404 /default/error404
     ErrorDocument 500 /errors/error500.php
 
  RewriteEngine On
  # API
  RewriteCond %{REQUEST_URI} ^/api$
  RewriteRule ^(.*)$ /api/ [L,R=301]
 
  # redirect to our front web controller
  RewriteCond %{REQUEST_URI} ^/api/
  RewriteRule ^(.*)$ api.php [QSA,L]
 
  # Admin application
  RewriteCond %{REQUEST_URI} ^/admin$
  RewriteRule ^(.*)$ /admin/ [L,R=301]
 
  # redirect to our backend web controller
  RewriteCond %{REQUEST_URI} ^/admin/
  RewriteRule ^(.*)$ backend.php [QSA,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]
 
  </Directory>
 
  Alias /sf /usr/local/kliks/trunk/lib/vendor/symfony/data/web/sf
  <Directory "/usr/local/kliks/trunk/lib/vendor/symfony/data/web/sf">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>