nginx with php-fpm downloading php files rather than executing them on mac os x (local environment)

I'm having trouble getting PHP files to execute on my local development machine. I seem to have successfully installed nginx and php-fpm also seems to be correctly installed, and running but can't quite work out why PHP files are being downloaded rather than being executed.

After many, many hours of frustration, I thought it best to ask someone who had maybe done this before! I've done my best to provide all the information but if there is anything that may be helpful or that I have missed, please do not hesitate to ask in the comments.

Please note: Read carefully about the issue I am having. The reason I say this is because I have read through nearly every possible article related to this issue Google can give me and have tried several different ways, fixes, suggestions, reinstalls, configurations, etc. Every one of them has not been able to help fix or even debug the issue I am experiencing. In other words, this is definitely not a duplicate question. I have spent a few hours reading to make sure!

I have successfully installed nginx and php-fpm using https://github.com/josegonzalez/homebrew-php. The good ol' trusty brew doctor confirms everything is up to date and I have everything necessary installed (XQuartz, Xcode command line tools, etc).

Here are some excerpts of files that may be useful whilst trying to understand my setup:

php-fpm log

tail -f /usr/local/var/log/php-fpm.log

[24-Dec-2013 00:05:59] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[24-Dec-2013 00:05:59] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[24-Dec-2013 00:05:59] NOTICE: fpm is running, pid 31745
[24-Dec-2013 00:05:59] NOTICE: ready to handle connections

Please correct me if I'm wrong but this seems to show php-fpm is running correctly

the only change in my php-fpm config file

/usr/local/etc/php/5.4/php-fpm.conf from line 145

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = myusername
group = staff

The reason I have changed this to myusername:staff is because that's how the user:group is set in my ~ directory. This may be part of the problem, I'm not sure.

I have indeed come across all the usual file permission issues and can confirm all my files within /clients/mywebsite/local have fixed this using chown -R myusername:staff ./ and chmod -R 0755 ./. With that in mind, hopefully, this shouldn't be a permissions issue.

nginx config file

/usr/local/etc/nginx/nginx.config

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

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

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

The only parts that I have changed from original file located in /usr/local/etc/nginx/nginx.conf.default is add index.php to the location / { block and uncommented the location ~ \.php$ { block to allow php-fpm processing for .php files

It's also worth mentioning I have created a /usr/local/etc/nginx/conf.d/mywebsite.conf file and added 127.0.0.1 mywebsite.local to my hosts file which allows me to access http://mywebsite.local.

Nginx seems to be set up correctly as I can access http://mywebsite.local/test.html absolutely file in my /clients/mywebsite/local/web/test.html folder but for PHP files, this is a different story. They are just downloaded by the browser as a PHP file, they're not being executed at all.

mywebsite config file

/usr/local/etc/nginx/conf.d/mywebsite.conf

server {
    listen   80;
    listen   [::]:80 default ipv6only=on;
    server_name  mywebsite.local;

    location / {
        root   /clients/mywebsite/local/web;
        index  index.php index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

I'm assuming any parts here will be overwritten for http://mywebsite.local and anything that is not in here is taken from the normal /usr/local/etc/nginx/nginx.conf file.

It's also worth saying my /usr/local/var/log/nginx/error.log file isn't returning any errors. Each time I make a change to the .conf files, I restart nginx using the command sudo nginx -s reload and can confirm php-fpm and nginx processes are running.

I have also read about changing 127.0.0.1:9000 to /var/run/php5-fpm/php5-fpm.sock which does not seem to be in my configuration. I have tried using find even using ack to search for it's existence but it is not on my machine. I also read about changing this port to something other than :9000 if it is already being used. As this is my first installation, I'm pretty sure this isn't the case but how would I go about testing this?

I have also read about another file located in /usr/share/nginx/html but again this does not exist.

Well, you've read this far so thank you! If you are able to help out in anyway, I really appreciate your time in doing so.


Instead of specifying the PHP section in the nginx.conf file, can you specify a server location in the default file (within sites-available directory) and either reload or restart nginx:

server { 
        listen 80;
        listen [::]:820; #ipv6only=on;
        root /var/www/;  ##assuming your PHP application is in /var/www/
        index index.php index.html index.htm;
        server_name PHPApp;

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

Also, make sure that your nginx.conf file has the following directive:

include /etc/nginx/sites-enabled/*;