CONFIGURE NGINX FOR PHP SYMFONY PROJECT ON OSX LOCALHOST

Let’s say you installed LEMP successfully on your MAC OSX’s localhost

Now you need nginx configuration. Assuming that you have a separate virtual site under sites-available called api

$ sudo vi /usr/local/etc/nginx/sites-available/api

  • point the root to the web folder of symfony.
  • For rewrite rule, switch from app.php to app_dev.php or vice versa.
  • Symfony app_dev.php allows you to see the bottom debug bar tool. You can test them both
server {
        listen       80;
        listen       [::]:80;
        server_name  api;
        root       /var/www/api/web;

        access_log  /usr/local/etc/nginx/logs/default.access.log  main;

        location / {
                # try to serve file directly, fallback to rewrite
                try_files $uri @rewriteapp;
        }

        location @rewriteapp {
                # rewrite all either to app.php or app_dev.php
                rewrite ^(.*)$ /app_dev.php/$1 last;
        }

        # Deny all . files
        location ~ /\. {
                deny all;
        }

        location ~ ^/(app|app_dev|config)\.php(/|$) {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /usr/local/etc/nginx/fastcgi_params;
        }

        error_page  404     /404.html;
        error_page  403     /403.html;
}

Do not forget to point your localhost to a custom domain name, in this case we chose api

$ sudo vi /etc/hosts
127.0.0.1    api

Now just open the url:

http://api/

YEEEYYYY!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s