PHP in the real world http://sebwebdev.posterous.com It can't be that easy! - What if it is? posterous.com Wed, 04 May 2011 13:25:00 -0700 Running Symfony2 on Nginx + PHP-FPM http://sebwebdev.posterous.com/51840968 http://sebwebdev.posterous.com/51840968

I have been using and playing around with Nginx lately on my local dev machine (Ubuntu 11.04) since I plan on using it in a live environment.

Nginx is a lightweight, high-performance HTTP server. PHP scripts are proxied to FastCGI using a Unix socket or TCP connection (which I am using in the config below). I am using PHP-FPM in this case for processing my scripts.

Recently I needed to run a Symfony2 application through Nginx.

Here is my virtual host configuration:


server {
        listen  *:80;
        server_name     symfony.local www.symfony.local;
        root /var/www/Symfony/web;
        index app_dev.php;

        ## add www. for SEO
        if ($host != 'www.symfony.local') {
                rewrite ^/(.*)$ http://www.symfony.local/$1 permanent;
        }

        ## expires headers on known static files and serve directly
        location ~* \.(css|js|jpeg|jpg|gif|png|ico)$ {
                expires 30d;
                break;
        }

        ## matches any query, since all queries begin with /
        location / {
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /app_dev.php?q=$1 last;
                }
        }

        ## pass php scripts to fastcgi
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass    127.0.0.1:9000;
                fastcgi_index   app_dev.php;
                include         /etc/nginx/fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME /var/www/Symfony/web$fastcgi_script_name;
        }

        ## disable viewing .htaccess & .htpassword
        location ~ /\.ht {
                deny  all;
        }
}

Obviously you will have to adapt the FastCGI settings and paths to your needs. And in a live environment you would want to use "app.php" instead of "app_dev.php" (or whatever your front controller name is), so no profiling information is collected and exposed to the outside world.

The management of virtual host configurations on Nginx is analog to Apache2. On a Debian/Ubunutu host your virtual hosts files reside in /etc/nginx/sites-available.

In this case I saved the above code in a file /etc/nginx/sites-available/Symfony and created a softlink to the sites-enabled folder:
ln -s /etc/nginx/sites-available/Symfony /etc/nginx/sites-enabled/Symfony

Finally a server restart activates your configuration: /etc/init.d/nginx restart

Hope this helps someone.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1205005/41671_581786562_9012_n_reasonably_small.jpg http://posterous.com/users/he6wQJnpVqwMa Sebastian Knüll sebwebdev Sebastian Knüll