0

I have nginx and php-fpm installed on Centos 7, when i try and go to a test php file it downloads it instead of running and rendering in the browser. All other html, image files etc display fine.

If this has been answered elsewhere on the site please excuse this post and I will delete, but all other posts I have seen are for Ubuntu and suggestions there do not work. I also tried temporarily disabling selinux and same thing.

In my /usr/share/nginx/html/ directory I have a php file with the phpinfo() function:

[root@www1 html]# ll /usr/share/nginx/html/
total 24
-rw-r--r--. 1 root root 3650 Oct  4 11:53 404.html
-rw-r--r--. 1 root root 3693 Oct  4 11:53 50x.html
-rw-r--r--. 1 root root 3700 Oct  4 11:53 index.html
-rw-r--r--. 1 root root   20 Jan  8 01:44 info.php
-rw-r--r--. 1 root root  368 Oct  4 11:53 nginx-logo.png
-rw-r--r--. 1 root root 2811 Oct  4 11:53 poweredby.png

My configs are below:

[root@www1 nginx]# cat /etc/php-fpm.d/www.conf

;listen = 127.0.0.1:9000

listen = /var/run/php-fpm/php-fpm.sock
listen.allowed_clients = 127.0.0.1

listen.owner = nobody
listen.group = nobody
user = nginx
group = nginx
pm = dynamic

[root@www1 nginx]# cat /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

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

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

[root@www1 nginx]# cat /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  my_ip_here;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
3
  • 1
    How are you accessing the site? What is the URL you used? Jan 8, 2016 at 0:26
  • If you tried what I suggested and it didn't work please post the results and additional information requested. Diagnosing this kind of thing usually takes time, you work through things until you find a solution.
    – Tim
    Jan 8, 2016 at 0:29
  • Thanks for time @Tim for you reply, stupidly it was the URL from which i was access it (mistmatched via a hostile entry on my machine) as soon as i access via server_name IP it works. I don't know why it didn't just come up with 404 not found but instead downloaded. Anyways is working so happy now!
    – morleyc
    Jan 8, 2016 at 6:35

1 Answer 1

-1

My best guess is one of these things: - The try_files could be affecting things - Your fasscgi_pass URL may be incorrect compared with where PHP is installed. You can try changing to the /var/run and /var/run/php directories to see what's there. - There may be some other problem with PHP.

Try changing to this, taking out any extra lines (especially the try_files within the PHP block). Report back what happens.

location ~ \.php$ {
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  fastcgi_index index.php;
  include fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

If that doesn't work try this

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

You can add this to any location block to help debug. Use Firefox and "Live HTTP Headers" to see the output

add_header Z_LOCATION "(describe your location block)";
add_header URI $uri; 

If neither work you'll have to post more details about your PHP setup and maybe some logs.

Here's my exact configuration, but note that it's set up to work with HHVM, a faster PHP interpreter than standard PHP

location ~ \.(hh|php)$ {
  fastcgi_keep_conn on;
  fastcgi_intercept_errors on;
  fastcgi_pass   php;
  include        fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

  add_header Z_LOCATION "PHP MAIN"; add_header URI $uri; # DEBUG
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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