# Nginx + PHP-FPM = File not found



## nx (Jul 27, 2012)

I've just installed *nginx*, got it working, but I can't get my index.php to resolve. 

I was getting 403 errors until I changed permissions, and now I get 404 errors. 
The 403 is in /var/log/nginx-error.log and 404 in /var/log/nginx-access.log.
So *nginx* is working fine.

But I'm not sure if *php-fpm* is working.

To test I comment out the block in the config that 'passes the PHP scripts to FastCGI server listening on 127.0.0.1:9000'.
Nginx then asks me to open/save the index.php it otherwise should be rendering as html after *php-fpm/fastcgi* executes the php.
But I can't seem to configure this block (uncommented) to get *php-fpm* to work and send back html to *nginx*.

I installed from source so I could use the third party auth digest module (which isn't working yet), so user, pid, and some other directives were configured then. The prefix I set was /usr/local/etc/ningx.

Here's my config:

```
#user  nobody;
worker_processes  2;mydomain

error_log  /var/log/nginx-error.log  warn;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
    use kqueue;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  off;

    server_tokens off;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  5;

    port_in_redirect off;

    #gzip  on;

    server {
        listen       8082;
        server_name  www.mydomain.com;
        server_name_in_redirect off;

        auth_digest_user_file /usr/local/www/passwd.digest;
        location /private{
        auth_digest 'These are not the interwebs you are looking for';
        }

        access_log  /var/log/nginx-access.log  main;

        location / {
            root   /usr/local/www/mydomain.com/public;
            index  index.php;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /usr/local/www/mydomain.com/public;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}
```

I've tried setting:


```
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
```

as


```
fastcgi_param  SCRIPT_FILENAME  /usr/local/www/mydomain.com/public$fastcgi_script_name;
```

and I've tried adding a trailing slash to end of root dir's but still no joy.


Here's the included fastcgi_params file:


```
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
```

I've tried turning fastcgi_intercept_errors on; but this doesn't show any more error info.


Can anyone see anything I've missed?

I installed following these howtos:
http://bin63.com/how-to-install-nginx-and-php-fpm-on-freebsd
http://interfacelab.com/nginx-php-fpm-apc-awesome/

and I've checked my config against the *nginx* docs.

The things I can think I've done wrong is not installing PHP with CLI, as per the first link, not enabling *php-fpm*, or I've screwed up a path in nginx.conf (above).


----------



## nx (Jul 27, 2012)

[Cmd=]ps -aux[/Cmd]

shows


```
USER        PID  %CPU %MEM    VSZ    RSS  TT  STAT STARTED    TIME COMMAND
root       1104   0.0  0.3   9616   1312  ??  SsJ   2:59PM 0:00.00 /usr/sbin/syslogd -ss
root       1193   0.0  1.8  45604   9376  ??  SsJ   2:59PM 0:00.01 php-fpm: master process (/usr/local/etc/php-fpm.conf) (php-fpm)
www        1195   0.0  1.9  45604   9412  ??  SJ    2:59PM 0:00.01 php-fpm: pool www (php-fpm)
www        1196   0.0  1.9  45604   9412  ??  SJ    2:59PM 0:00.00 php-fpm: pool www (php-fpm)
root       1200   0.0  0.4  14132   2280  ??  SsJ   2:59PM 0:00.00 nginx: master process /usr/local/sbin/nginx
webserver  1203   0.0  0.5  14132   2536  ??  SJ    2:59PM 0:00.01 nginx: worker process (nginx)
webserver  1204   0.0  0.5  14132   2536  ??  SJ    2:59PM 0:00.01 nginx: worker process (nginx)
root       1222   0.0  0.3   9644   1456  ??  SsJ   2:59PM 0:00.01 /usr/sbin/cron -J 15 -s
root       1350   0.0  0.4  10124   1816   0  SJ    2:59PM 0:00.04 login [pam] (login)
root       1360   0.0  0.5  10948   2560   0  SJ    2:59PM 0:00.05 -csh (csh)
root       1366   0.0  0.2   9680   1140   0  R+J   2:59PM 0:00.00 ps -aux
```

I tried reinstalling PHP with CLI, *ps -aux* gives the same output, and I still get a 404.

So it appears PHP is working (as *php-fpm* unless I'm mistaken) and *nginx* is obviously working.

Anyone have *nginx* + *php-fpm* setup and know how I can debug/fix this?


----------



## nx (Jul 27, 2012)

I've made some progress... mydomain.com is now showing:


```
Access denied
```

If I turn *php-fpm* off:


```
/usr/local/etc/rc.d/php-fpm stop
```

mydomain.com gives 502 Bad Gateway.

So *php-fpm* and *nginx* are working - just not as they should :\

To get this progress, here's my ammended *php-fpm* block at bottom of nginx.conf


```
location ~ \.php$ {
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                root           /usr/local/www/mydomain.com/public;
                fastcgi_index  index.php;
                if (-f $request_filename) {
                    fastcgi_pass 127.0.0.1:9000;
                }
        }
```

I've found this user with same issue posting in the *nginx* forum, but his fix isn't working for me:

http://forum.nginx.org/read.php?3,197023

If anyone has had problems getting PHP executed by *php-fpm* and rendered as html by *nginx*, and possibly got the Access denied page, please help!
I've wasted a few hours on this today.


----------



## nx (Jul 27, 2012)

I think I may have found the issue and it is permissions.

But I'm not sure how to assign them as per this excerpt:



> This effectively means that not only should the file have read permission, but the entire directory structure should have execute permission so that the PHP user can traverse the path. An example of this:
> 
> Say you have an index.php file in /var/www. /var/www/index.php must have read permission and both /var and /var/www must have execute permissions.



from http://blog.martinfjordvald.com/2011/01/no-input-file-specified-with-php-and-nginx/

I have *nginx* running as user and group webserver, and part of webadmin group, which has read + execute group permissions and owns (webadmin:webadmin) the documentroot dir down.

How would I extend this to *-R /usr/local/www/mydomain.com/* without changing root:wheel as owner of everything else in /usr/local/www/ and all subdirectories of?

Or am I misreading that quote?


----------



## nx (Jul 28, 2012)

I'm going around in circles...

If I turn off *php-fpm*:


```
/usr/local/etc/rc.d/php-fpm stop
```

*nginx* shows page with:


```
503 Bad Gateway
```

If turn *php-fpm* on again, *nginx* shows page with:


```
Access denied.
```

and /var/log/nginx-access.log shows a 430 Forbidden:


```
GET / HTTP/1.1" 403 25
```

I've read forum posts that say this is due to a setting in /usr/local/etc/php-fpm.conf:


```
security.limit_extensions =
```

which should have all extensions used in the location index parameter:


```
location / {
            root   /usr/local/www/mydomain.com/public;
            index  index.php;
```

eg


```
security.limit_extensions = .html .php
```

with the default being .php if this directive is commented out.

I've tried with it commented out, uncommented with .php, and with .html and .php (while adding index.html to the index parameter).

And it hasn't fixed this issue.


I've set all permissions on the documentroot (root) liberally for testing to 777.

I've turned autoindex on in the http 'stanza' (not shown in my config above) to see if *ngnix* is seeing the right directory.

If the root is set to html, which is the example directory included in the *nginx* directory, then it shows an index of that directory's files.

But this isn't working for the documentroot that I set /usr/local/www/mydomain.com/public

So my guess is this really is a permissions problem.

I just can't get them right.

Will anyone ever read this thread and help???


----------



## freebuser (Jul 30, 2012)

Didi you try disabling authentication in nginx config file?


----------



## nx (Jul 30, 2012)

You mean the auth digest?

I have that working now..., but yes I did disable it to try to get php working.

Thanks


----------



## freebuser (Aug 2, 2012)

I can't see much different in the config, other than the 8082 port.

Did you try with usual www port 80?


----------

