Skip to main content
Image for post: Deploy a Symfony 2 app on a shared hosting

Deploy a Symfony 2 app on a shared hosting

  • Move all the contents of the directory called web on the root of the Symfony2 project
  • Rename app_dev.php in your index.php
  • Edit the .htaccess file replacing all app_dev.php with index.php
  • Edit your index.php simply updating the require_once paths to let it include the files on the right paths:


    use Symfony\Component\HttpFoundation\Request;

    // If you don't want to setup permissions the proper way, just uncomment the following PHP line
    // read for more information

    // This check prevents access to debug front controllers that are deployed by accident to production servers.
    // Feel free to remove this, extend it, or make something more sophisticated.
    if (isset($_SERVER['HTTP_CLIENT_IP'])
       || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
       || !in_array(@$_SERVER['REMOTE_ADDR'], array('', 'fe80::1', '::1'))
    ) {
       header('HTTP/1.0 403 Forbidden');
       exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');

    $loader = require_once __DIR__.'/app/bootstrap.php.cache';
    require_once __DIR__.'/app/AppKernel.php';

    $kernel = new AppKernel('dev', true);
    $request = Request::createFromGlobals();
    $response = $kernel->handle($request);
    $kernel->terminate($request, $response);
  • That's all! Here is my .htaccess file:

    DirectoryIndex index.php

    <IfModule mod_rewrite.c>
       RewriteEngine On

       # Determine the RewriteBase automatically and set it as environment variable.
       # the following 2 lines to eliminate the overhead.
       RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$

       # Redirect to URI without front controller to prevent duplicate content
       # redirect -> request -> ...).
       # environment variable, you have 2 choices:
       # - disable this feature by commenting the following 2 lines or
       #   following RewriteCond (best solution)
       RewriteCond %{ENV:REDIRECT_STATUS} ^$
       RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

       # If the requested filename exists, simply serve it.
       RewriteCond %{REQUEST_FILENAME} -f
       RewriteRule .? - [L]

       # Rewrite all other queries to the front controller.
       RewriteRule .? %{ENV:BASE}/index.php [L]

    <IfModule !mod_rewrite.c>
       <IfModule mod_alias.c>
           # and the generated links can still be used.
           RedirectMatch 302 ^/$ /index.php/
           # RedirectTemp cannot be used instead

Sounds good! Hope this will work :)