ScaleScaleScaleScale

Tips / Nginx


Nginx Yii Framework Configuration

Yii Framework is a popular high performance PHP framework that helps developers to build fast Web 2.0 apps. Some of it’s features are: MVC, DAO/ActiveRecord, I18N/L10N, caching, authentication and role-based access control, scaffolding, testing, among others.

On this tutorial I will share you a basic Yii +Nginx + PHP-FPM configuration. For this to work you need to have php-fpm configured to work with sockets, otherwise, you should need to change fastcgi_pass_unix to 127.0.0.1:9000, that is the default port and interface for php-fpm. You may need to change yoursite.com with your own domain name and root directory to the real directory of your website.

Sample host configuration with Yii + Nginx + PHP-FPM

server {

    set $host_path "/var/www/yoursite.com";
    access_log  /var/www/yoursite.com/log/access.log  main;

    server_name  yoursite.com www.yoursite.com;

    root   $host_path/htdocs;
    set $yii_bootstrap "index.php";

    # define charset
    charset utf-8;

    location / {
        index  index.html $yii_bootstrap;
        try_files $uri $uri/ /$yii_bootstrap?$args;
    }

    # deny access to protected directories
    location ~ ^/(protected|framework|themes/w+/views) {
        deny  all;
    }

    #avoid processing of calls to unexisting static files by yii
    location ~ .(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }

    # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
    location ~ /. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # php-fpm configuration using socket
    location ~ .php {
        fastcgi_split_path_info  ^(.+.php)(.*)$;

        #yii catches the calls to unexising PHP files
        set $fsn /$yii_bootstrap;
        if (-f $document_root$fastcgi_script_name){
            set $fsn $fastcgi_script_name;
        }

        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

        #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fsn;

## Tweak fastcgi buffers, just in case.
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
    }

}

Need additional information? Read more at Yii website

Popular search terms:

  • yii2 nginx cofnig
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1829 5JkKgUVJ6FoPTltby3gXoLAckyruIgQ7LjdD8YXLQ5ASEkPG-fyHO66JMp9H7lLz 403ea7144eae8d9319f6ec04c3ab835ff5fb1aff&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1831 kWV-BpAwy2Ok53LGnK9B9mNkd5xdhKY-k3mwQygiw502re8qhxB9YgAbRmTYbYfm 86efe3dc3cbb668adc505e4412b7b8b731a742dc&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1833 luEBDXQj-ktnhkbiheU2Wa-1EXvCOMWf08nArgEwmiuQzvWcG-kiB9ysRqllQBtp e48b8895d4a204fe310815091e040aa2ea7ed075&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking