Nginx - Downloading PHP instead of executing

2019-08-29 10:32发布

问题:

I am using CentOS and I have two web pages which im using for a piece of work. The first is a .html which has a form which uses a .php script to send data to my mySQL database, this works fine. The second is just a .php page which is supposed to display the contents of the table within the database. The problem is, my browser downloads the file instead of displaying it. I have seen a few questions similar to this but their nginx confs look very different to mine (and i think thats where the problem lies).

My nginx config (Im using the default one created, I havent added anything to it):

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:

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  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   /usr/share/nginx/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;
#}
}

Im looking for which lines I should add and where to try and fix my problem. Thanks for your time.

回答1:

You must uncomment this part:

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

Edit the file /var/run/php5-fpm.sock and change listen = 127.0.0.1:9000 to listen = /var/run/php5-fpm.sock.

And to allow the server to only process the exact file path edit /etc/php5/fpm/php.ini and make sure cgi.fix_pathinfo=0.



标签: php mysql nginx