10

Iv'e setup an Nginx php server on a linux REHL machine. When accessing html files all goes well, but trying to access php file, the file is downloaded instead of being executed.

This is my nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

...and this is the server block:

server {
    listen       80;
    server_name  {mywebsitename};

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/{mywebsitename}/;
    }

    location /ngx_status_2462 {
      stub_status on;
      access_log   off;
      allow all;
    }

    location ~ \.php$ {
#                fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/{mywebsitename}$fastcgi_script_name;
        include fastcgi_params;
        }

        error_page  404              /404.html;

        location = /404.html {
            root   /usr/share/nginx/html;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
1
  • So this is small point, you wrote root /usr/share/nginx/html/{mywebsitename}/; so I assumed {mywebsitename} doesn't contain a trailing /, so {mywebsitename}$fastcgi_script_name; should be {mywebsitename}/$fastcgi_script_name; right ? (added a slash) Commented Jan 14, 2014 at 12:20

4 Answers 4

9

It might be because of the mimetype you're sending:

default_type  application/octet-stream;

See: http://mimeapplication.net/octet-stream

Sign up to request clarification or add additional context in comments.

1 Comment

Ran into this problem but only for the root URL. Spent 2-3 hours trying to fix this and finally came across your answer. If I could I'd give you all the upvotes.
7

I just had this exact same problem. I was using Ubuntu 12.04 and Linux Mint 14 so different OS but likely to have the same issues.

A couple of issues may happening. Firstly, you need to have php5-fpm installed (FastCGI Process Manager). I was trying to run it with my standard version of PHP but it was not working - http://www.php.net/manual/en/install.fpm.php

I also had Apache installed, and even if it weren't running it must have had some conflict because once I uninstalled Apache I was able to execute the PHP files.

I would also look at this line

fastcgi_pass 127.0.0.1:9000;

And consider changing it to

fastcgi_pass   unix:/var/run/php5-fpm.sock;

Here is a detailed guide to installation of Nginx and PHP5-FPM for RHEL (and other OS's)

http://www.if-not-true-then-false.com/2011/install-nginx-php-fpm-on-fedora-centos-red-hat-rhel/

3 Comments

That was that. Thanks alot!
Uninstalling apache2 was what fixed it for me, after that, PHP scripts executed instead of downloaded ... I wonder apache2 somehow fiddled with MIME types or something...
An alternative to uninstalling apache2: When the root serving directory (/var/www/html for me) was the same for both nginx and apache2, nginx would download the php file (even if apache wasn't running, just like you). However, moving nginx's root directory to a different directory (I created one at /var/www/nginx/) caused nginx to function correctly.
1

You need to change the user to nginx instead of apache in this file a/etc/php-fpm.d/www.conf

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group = nginx

and of course restart service php-fpm restart and service nginx restart

Comments

1

Comment out default_type application/octet-stream;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.