woozly
woozly

Reputation: 1369

nginx configuration. Yii project in separate directory [not in root]

I spent over 6 hours on this trouble.

I have nginx/1.2.7 server, and php-fpm on 127.0.0.1:9000. I have base nginx config:

server
{
    listen 80;
    server_name example.org www.example.org;

    index index.php index.html;

    access_log /srv/www/example.org/logs/access.log;
    error_log /srv/www/example.org/logs/error.log;

    location /
    {
        root /var/www/html/example.org/public_html;
        try_files $uri $uri/ /index.php;

        location ~ \.php$
        {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }

And it works fine! All php files work like they must.

But I have some separate yii-project, which need to execute in other than main root folder. And I have this configuration added at bottom:

Where /srv/www/example.org/yiitest — it is a root of yiitest project (with 'protected' folder and other inside it).

location /yiitest
    {
        root /srv/www/example.org/yiitest;
        try_files $uri $uri/ /index.php?$args;

        location ~ \.php$
        {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }

But it doesn't work. I've got 'File not found'. And maximum what I can get, it is: example.org/yiitest/ the main page works fine. And if I go to example.org/yiitest/site/contact/ I'll get file not found. :(

I can't understand, how to correctly setup yii-project in separate subdirectory of a server.

Upvotes: 1

Views: 2145

Answers (1)

Valery Viktorovsky
Valery Viktorovsky

Reputation: 6736

  1. create symlink

    cd /var/www/html/example.org/public_html
    ln -s ../yiitest/public yiitest
    
  2. configure nginx

    root /var/www/html/example.org/public_html;
    
    location / {
        ...
    }
    
    location /yiitest/ {
        index    index.php;
    
        # front end
        if (!-e $request_filename) {
            rewrite ^(.*)$ /yiitest/index.php last;
            break;
        }
    }
    
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1: 9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO        $fastcgi_script_name;
        include fastcgi_params;
    }
    
  3. Then configure yii framework. You should set 'basePath' in your config:

    <?php
    return array(
        'basePath' => 'yiitest',
        ...
    );
    

Upvotes: 1

Related Questions