服务器上打开PHP文件出现下载界面

服务器上打开PHP文件出现下载界面

问题的根本原因是nginx默认不支持PHP文件。需要安转并开启php-fpm,然后在nginx的配置文件中做如下修改。

nginx安装成功后,在 /etc/nginx/目录下,有一个nginx.conf和一个nginx.conf.default,删掉nginx.conf,复制一份nginx.conf.default的默认配置

rm -rf nginx.conf
cp nginx.conf.default nginx.conf
vim nginx.conf

server里面的配置:
1,在location / 中index处增加index.php ;
2,在location / 中index下一行增加URL重写读取;
3,解开location ~ .php$的注释,修改root(网站根目录)和fastcgi的路径。

最终server部分配置内容为:

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm index.php; #1.增加index.php
	    	try_files $uri $uri/ /index.php$is_args$args; #2.这里新增url重写(path)
        }

        #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
        #
        #3.解开php支持的注释
        location ~ \.php$ {
            root   /usr/share/nginx/html; #4.修改网站的根目录
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; #5.修改fastcig的路径
            include        fastcgi_params;
        }

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

保存后重启nginx,在/usr/share/nginx/html目录下新建index.php文件打开就不会出现下载了。

猜你喜欢

转载自blog.csdn.net/weixin_43687896/article/details/87089296