NginX + PHP-FPM displays blank php pages

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have a problem where my server running NginX with php-fpm loads blank PHP pages (strangely except for my phpinfo.php https://forum.tuteehub.com/tag/file">file, which loads normally). If I put an index.html page in the same directory and browse to it, it loads. The fact that phpinfo.php (which https://forum.tuteehub.com/tag/calls">calls the phpinfo(); function) loads, confirms that php-fpm works.

I am hoping somebody might have some advice. I apologise in advance for the bulk of info I am about to give, but I would rather give too much information than too little. Here are my configuration https://forum.tuteehub.com/tag/file">files.

/etc/nginx/conf.d/default.conf:

server {
    listen   80;
    server_name  45.55.182.120;

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

        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;

    location / {
       try_https://forum.tuteehub.com/tag/file">files $uri $uri/ =404;
    }

    https://forum.tuteehub.com/tag/error">error_page  404              /404.html;

    # redirect server https://forum.tuteehub.com/tag/error">error pages to the static page /50x.html
    #
    https://forum.tuteehub.com/tag/error">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$ {
    try_https://forum.tuteehub.com/tag/file">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;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    include fastcgi_params;

    }

server {
    listen   80;
    server_name  45.55.182.120;

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

        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;

    location / {
       try_https://forum.tuteehub.com/tag/file">files $uri $uri/ =404;
    }

    https://forum.tuteehub.com/tag/error">error_page  404              /404.html;

    # redirect server https://forum.tuteehub.com/tag/error">error pages to the static page /50x.html
    #
    https://forum.tuteehub.com/tag/error">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$ {
    try_https://forum.tuteehub.com/tag/file">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;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

 

According to your configuration, you have two server{...} blocks which are exactly the same. So before I start explain what's wrong with your configuration, you need to provide more details. See down below for some troubleshooting hints.

For now, I'll post mine here and highlight a few directives that do matter.

My /etx/nginx/conf.d/default.conf looks as follow

server {

  # Replace this port with the right one for your requirements
  listen 80;

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name mydomain.nl;

  root /var/www/mydomain.nl/public_html/;

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  index index.php index.html;

  location / {
    # This is cool because no php is touched for static content.
    try_files $uri $uri/ /index.php;
  }

  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    expires max;
  }

  location ~* \.php$ {
    try_files $uri =404

    fastcgi_intercept_errors on;

    fastcgi_index   index.php;
    fastcgi_pass    unix:/var/run/php5-fpm.sock;

    include fastcgi_params;

    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  }

  location ~ /\.(ht|ssh) {
    deny  all;
  }

  location /status {
    include fastcgi_params;
    fastcgi_pass    unix:/var/run/php5-fpm.sock;
  }

}

The following directives are important:

server_name mydmaiin.nl; <-- This is unique for every server block.

root /var/www/mydomain.nl/public_html/; <-- This is the root that holds your website / data.

The rest is trivial.

So Let's take the /etc/php-fpm.d/www.conf file and examine. You chose to use a file socket

listen = /var/run/php-fpm/php-fpm.sock <-- php-fpm will communicate with nginx through this file. So this is my www.conf file unless you missed something. I've filtered out all commented lines. So these are the lines that are uncommented.

[www]
listen = /var/run/php5-fpm.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
user = apache
group = apache
pm = dynamic
pm.max_children = 50
pm
                                                    
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.