nginx showing blank PHP pages
Also had this issue and finally found the solution here. In short, you need to add the following line to your nginx fastcgi config file (/etc/nginx/fastcgi_params in Ubuntu 12.04)
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
For reference, I am attaching my location
block for catching files with the .php
extension:
location ~ \.php$ {
include /path/to/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
Double-check the /path/to/fastcgi_params
, and make sure that it is present and readable by the nginx user.
replace
include fastcgi_params;
with
include fastcgi.conf;
and remove fastcgi_param SCRIPT_FILENAME ... in nginx.conf
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
<pre><?php var_export($_SERVER)?></pre>
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).