Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
Downvoter - care to leave a comment to explain why you downvoted? –  Titanium Dec 26 '13 at 20:38

2 Answers 2

up vote 3 down vote accepted
+50

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/*;
share|improve this answer
    
did this solve the problem? –  ali haider Dec 27 '13 at 1:34
    
Thanks for your answer. I've completed everything you have suggested but now for any PHP files, rather than them being downloaded, I get a 502 Bad Gateway message. Any ideas? –  Titanium Dec 27 '13 at 6:54
    
did you change the root directive to point to the directory where your index.php resides? If your php application & index.php is not in /var/www/, then you should either move your project there or chaneg the root location in the default file. –  ali haider Dec 27 '13 at 14:08
    
I have tried this, and ensured this is located outside the location block. I know this path is correct as I have tested normal .html files and they load correctly. The issue seems to be php-fpm not linking correctly with nginx. Is there a way of testing this thoroughly to work out where the issue is exactly? –  Titanium Dec 27 '13 at 19:47
    
I am tempted to run a clean install of OSX to ensure apache isn't interfering. –  Titanium Dec 27 '13 at 19:51

I think the problem is you do not have a upstream to tell nginx where to send php files, something like this in your http block:

upstream php {
    server unix:/var/run/php-fpm.socket;
}

where unix:/var/run/php-fpm.socket has to be indicated in the directive listen of php config.

BTW: is not a good practice to have root inside a location block (http://wiki.nginx.org/Pitfalls#Root_inside_Location_Block) neither multiples index: (http://wiki.nginx.org/Pitfalls#Multiple_Index_Directives)

share|improve this answer
    
Unfortunately this hasn't worked. I assume this is trying to do the same thing as 127.0.0.1:9000 and I also notice that /var/run/php-fpm.socket doesn't exist. Should it? How would I go about testing this? –  Titanium Dec 27 '13 at 7:03
    
This file is created when modifying the parameter listen in your pools configuration, for example /etc/php5/fpm/pools.d/www.conf and set listen = /var/run/php-fpm.socket. But yes, it would be the same as 127.0.0.1:9000. Are you sure that php5-fpm is running in that port?, Maybe the notice [pool www] 'user' directive is ignored when FPM is not running as root, is causing the problem, try to start php5-fpm with service php5-fpm start as root. –  algui91 Dec 27 '13 at 7:49
    
I have tried running as root and get the same issue. With regard to it running on that port, when I run curl 127.0.0.1:9000 I get curl: (7) Failed connect to 127.0.0.1:9000; Connection refused. It's also worth mentioning /usr/local/var/run/php-fpm.sock does not exist. –  Titanium Dec 27 '13 at 19:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.