Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP-FPM sending empty response with Nginx on macOS

I installed nginx 1.10.3 and php 5.5.38 as a development server on macOS 10.12.4

When I try a test php file in my browser the body is empty but the response headers seem ok:

HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Wed, 29 Mar 2017 11:35:21 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.5.38

There are no errors in php-fpm.log or nginx/error.log

my nginx.conf has:

server {
    listen       80;
    server_name  wordpress.bob;
    root /Users/mark/Sites/wordpress;     

    include /usr/local/etc/nginx/global_restrictions.conf;
    include /usr/local/etc/nginx/wordpress.conf;

    location ~ \.php$ {
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass   unix:/usr/local/var/run/php-www.sock;
      fastcgi_index  index.php;
      include        fastcgi_params;
    }

}

wordpress.bob is a local hostname for testing pointing to 127.0.0.1 in etc/hosts

php-fpm.conf has:

listen = '/usr/local/var/run/php-www.sock'

Any ideas what I'm doing wrong?

like image 370
Mark Robinson Avatar asked Mar 09 '23 11:03

Mark Robinson


2 Answers

It's hard to help without the ability to read all the configuration files.

You just posted one, not the included ones nor php-fpm.conf. This is not a disapproval (a wall of configuration files is not quite appropriate in a question) but it's just to point out that the configuration file we "don't see" may differ depending on installation.

Anyway I see some differences from the configuration file I have on a server for a wordpress site.

Here are some hints considering that as you don't get any errors php-fpm is running and nginx can "communicate" to it via the socket (otherwise you would get a bad gateway error).


At the beginning...

server {
    listen       80;
    server_name  wordpress.bob;
    root /Users/mark/Sites/wordpress;     

    index index.php; # <-- ADD THIS

Make sure in the included wordpress.conf you have

location / {
        try_files $uri $uri/ /index.php?$args;
}

The last part...

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 256 16k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 512k;
    fastcgi_intercept_errors on;
    fastcgi_max_temp_file_size 0;
    fastcgi_connect_timeout 3s;
    fastcgi_send_timeout 5s;
    fastcgi_read_timeout 5s;
    include fastcgi.conf; # <--- fastcgi.conf, NOT fastcgi_params
    fastcgi_pass /usr/local/var/run/php-www.sock;
}

The difference between fastcgi.conf and fastcgi_params (on my installation) is just one line:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

If this line is missing php code is not able to read $_SERVER['SCRIPT_FILENAME'] and (I think) this may break wordpress code resulting in empty output.


Finally make sure php-fpm worker processes have privileges to access /usr/local/var/run/php-www.sock

Usually the socket has the same owner:group of the workers.

The workers user and group is set in php-fpm.conf:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = ......
group = ......
like image 71
Paolo Avatar answered Mar 15 '23 18:03

Paolo


To install NGINX with Homebrew :

$ brew install nginx

Run NGINX :

$ sudo nginx

Test the localhost nginx :

http://localhost:8080

NGINX configuration file should be in :

$ /usr/local/etc/nginx/nginx.conf

If you want to change the default port :

$ sudo nginx -s stop
$ vim /usr/local/etc/nginx/nginx.conf

Change the : listen 8080; To : listen 80;

To save and Conf and start NGINX run :

$ sudo nginx

Then, according to your problem, you might simply be pointing to a an empty PHP file. Try to print a phpinfo() then look for "DOCUMENT_ROOT" to see where it goes.

like image 45
Youssef El Gharbaoui Avatar answered Mar 15 '23 18:03

Youssef El Gharbaoui