简体   繁体   中英

nginx and php72: scripts not running; maybe not found?

I am running a fresh install of FreeBSD (latest stable production version) with nginx and php72 installed from packages. My first difficulty is that most of the information on the web relates to building these applications from source and the packages put .conf files and other things in different directories. I think I figured that one out with the help of Nginx HTTP Server - Fourth Edition and some browsing. Nginx serves static files just fine. But PHP isn't working. Every attempt to run a short script containing php-info() results in:

An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later. If you are the system administrator of this resource then you should check the error log for details. Faithfully yours, nginx.

I found this Stack Exchange question from several years ago: File Not Found when running PHP with Nginx I tried several of the solutions given in the question, including the ones with 67 and 10 upvotes. You can see them in my nginx.conf file below. Here is my nginx.conf file:

  user  nginx;
    worker_processes auto;
    worker_priority 10;

    # This default error log path is compiled-in to make sure configuration parsing
    # errors are logged somewhere, especially during unattended boot when stderr
    # isn't normally logged anywhere. This path will be touched on every nginx
    # start regardless of error log location configured here. See
    # https://trac.nginx.org/nginx/ticket/147 for more info. 
    #
    error_log  /var/log/nginx/error.log;
    #

    pid        logs/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       mime.types;
        default_type  application/octet-stream;

        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';

        #access_log  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
            server_name  JRWFreeBSD;
            listen       80;
            root /usr/local/www/nginx/documents;
            index index.html index.htm index.php
            #charset koi8-r;

            #access_log  logs/host.access.log  main;


            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   /usr/local/www/nginx-dist;
            }

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            location ~* \.php$ {
            root /usr/local/www/nginx/documents;
                fastcgi_pass   127.0.0.1:9000;
            #   fastcgi_index  index.php;
                include        fastcgi_params;

            #   fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param SCRIPT_FILENAME /usr/local/www/nginx/documents$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;

            }
        }
    }
    enter code here

(This nginx.config file is based on the default that shipped with nginx. I have only included the server block I actually use, not the commented-out blocks.) All the files in the documents folder have user nginx / group nginx as the owner, with permissions 755. Nginx loads without reporting errors from any config file, including the one for php-fpm. Nothing showed up in the log files since I got nginx properly configured to serve static files. I have verified that the root path -- /usr/local/www/nginx/documents -- is correct. Nginx serves static files from there. The following script called phpinfo.php is in documents.

  <?php
    // Show all information, defaults to INFO_ALL
    phpinfo();
    ?>

php - I gave me results that include these lines:

 Environment
    OLDPWD => /usr/local/etc/nginx
    --snip--
    PWD => /usr/local/www/nginx/documents

    PHP Variables
    $_SERVER['OLDPWD'] => /usr/local/etc/nginx
    --snip--
    $_SERVER['PWD'] => /usr/local/www/nginx/documents

Command line php scripts work.

Everything looks right, but php scripts do not run when nginx serves them.

Solved it! sockstat told me that nobody was listening on 127.0.0.1 and while I was looking for a way to fix that I found out that while nginx was sending scripts to that ip address, php-fpm72 was listening on /var/run/php72-fpm.sock

I made these changes to nginx.conf:

 fastcgi_pass   unix:/var/run/php72-fpm.sock;
 #fastcgi_pass   127.0.0.1:9000;

PHP scripts now work. Installing nginx from the FreeBSD pkg system installed a default nginx.conf script that was not compatible with the default .conf script created when I installed php-fpm72 from a pkg. I still have one question, though: should I file a bug with the pkg maintainers?

Edited to add: Thank you Ms Hasse for getting me to re-examine the logs which started me on the right path. Also, Mr. Gelov, I replaced several of the lines I had with your better-focused version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM