nginx showing blank PHP pages

前端 未结 17 2237
走了就别回头了
走了就别回头了 2020-12-04 05:56

I have setup an nginx server with php5-fpm. When I try to load the site I get a blank page with no errors. Html pages are served fine but not php. I tried turning on disp

17条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 06:17

    Many users fall in this thread expecting to find a solution for blank pages being displayed while using nginx+php-fpm, me being one of them. This is a recap of what I ended up doing after reading many of the answers here plus my own investigations (updated to php7.2):

    1) Open /etc/php/7.2/fpm/pool.d/www.conf and check the value of parameter listen.

    listen = /var/run/php/php7.2-fpm.sock
    

    2) Parameter listen should match fastcgi_pass parameter in your site configuration file (i,e: /etc/nginx/sites-enabled/default).

    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    

    3) Check the file actually exists:

    $ file /var/run/php/php7.2-fpm.sock 
    /var/run/php/php7.2-fpm.sock: socket
    

    4) If it doesn't exist that means php7.2-fpm is not running, so you need to restart it:

    $ sudo /etc/init.d/php7.2-fpm restart
    [ ok ] Restarting php7.2-fpm (via systemctl): php7.2-fpm.service.
    


    With regard to the location section in /etc/nginx/sites-enabled/default:

       # pass PHP scripts to FastCGI server
       #
       location ~ \.php$ {
          include snippets/fastcgi-php.conf;
    
          # With php-fpm (or other unix sockets):
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
       }
    

    Check the file snippets/fastcgi-php.conf exists at location /etc/nginx/:

    $ file /etc/nginx/snippets/fastcgi-php.conf
    /etc/nginx/snippets/fastcgi-php.conf: ASCII text
    

    This file contains a list of variable definitions required by php7.2-fpm. The variables are defined directly or through an include of a separated file.

     include fastcgi.conf;
    

    This file is located at /etc/nginx/fastcgi.conf and it looks like:

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    ...
    fastcgi_param  REDIRECT_STATUS    200;
    

    nginx includes two possible parameter files: fastcgi_params and fastcgi.conf. The difference between both is the definition of variable SCRIPT_FILENAME:

    $ diff fastcgi_params fastcgi.conf 
    1a2
    > fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    

    To make a long story short, fastcgi.conf should always work. If for some reason you're set up is using fastcgi_params, you should define SCRIPT_FILENAME:

    location ~ \.php$ {
      include snippets/fastcgi-php.conf;
    
      # With php-fpm (or other unix sockets):
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    
      fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    }
    

    Now reload nginx configuration:

    $ sudo nginx -s reload
    

    And check a php file is displayed correctly. For instance:

    /var/www/html/test.php

    Where /var/www/html is the path to the document root.

    If despite all that, you're still seeing a blank file, make sure your php.ini has short_open_tag enabled (if you're testing a PHP page with short tags).

提交回复
热议问题