Symfony2 under Nginx on Ubuntu 12.04

Here at LeaseWeb we use the Symfony2 framework a lot. While many people run Apache2 as their web server, some prefer running Symfony2 under Nginx. At this moment Nginx is very popular among high traffic websites. It is actually the second most popular web server on the internet. Unfortunately the default Nginx configuration will not work with Symfony2 since it does not support the .htaccess format. However, with some small tweaks to the configuration you can make  your server run Symfony2 under Nginx.

For this guide, we used a fresh Ubuntu 12.04 installation, downloaded Symfony Standard 2.2.1 (latest) and installed Nginx 1.1.19 and PHP-FPM 5.3.10 from the standard repositories.

Install Nginx

Firstly, you must install Nginx and start the service.

sudo apt-get install nginx
sudo service nginx start

Install PHP5

Since Symfony2 is a PHP framework, you need to install that as well. To make PHP work with Nginx you must install the PHP-FPM package.

sudo apt-get install php5-fpm

Configure Nginx virtual host

Create a new file at /etc/nginx/sites-available with the name of your domain or project, this will be the Nginx configuration for your Symfony2 project. Edit the file as required.


server {

    # Listen on port 80
    listen 80;

    # Server name being used (exact name, wildcards or regular expression)
    server_name *;

    # Document root, make sure this points to your Symfony2 /web directory
    root /var/www/;

    # Logging
    error_log /var/log/nginx/;
    access_log /var/log/nginx/;

    # Handle static files, redirect to app.php if not found
    location / {
        try_files $uri /app.php;

    # Pass the PHP scripts to FastCGI server
    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS off;

Now test the configuration.

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Add to your hosts file

If you are following this guide for production you can skip this step. For development you need to add the domain to the hosts file to make sure the current machine reacts when you type in the address. You can do that by running this command:

echo "" | sudo tee -a /etc/hosts

Enable site and reload the Nginx configuration

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo service nginx reload

That should do the trick! Check your, and locations to verify that everything works correctly.

Using TCP/IP for FPM (optional)

If you prefer using TCP/IP for FPM, open the PHP-FPM pool config file: /etc/php5/fpm/pool.d/www.conf


listen = /var/run/php5-fpm.sock

By line:

listen =

Then change your Nginx site configuration.


fastcgi_pass unix:/var/run/php5-fpm.sock;

By line:


You must reload php-fpm and nginx for the changes to take effect. Note that with TCP/IP you can build a more scalable setup where you have multiple FPM machines and one Nginx as the load balancer in front of them.


3 thoughts on “Symfony2 under Nginx on Ubuntu 12.04”

  1. @Beren: You are absolutely correct and this line is in the nginx.conf file by default when nginx is installed using apt-get on Ubuntu. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *