File Not Found when running PHP with Nginx

后端 未结 15 1937
后悔当初
后悔当初 2020-12-04 13:59

Recently I installed the latest version of Nginx and looks like I\'m having hard time running PHP with it.

Here is the configuration file I\'m using for the domain:<

相关标签:
15条回答
  • 2020-12-04 14:36

    The error message “Primary script unknown or in your case is file not found.” is almost always related to a wrongly set in line SCRIPT_FILENAME in the Nginx fastcgi_param directive (Quote from https://serverfault.com/a/517327/560171).

    In my case, I use Nginx 1.17.10 and my configuration is:

        location ~ \.php$ {
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
          include fastcgi_params;
          fastcgi_read_timeout 600;
        }
    

    You just change $document_root to $realpath_root, so whatever your root location, like /var/www/html/project/, you don't need write fastcgi_param SCRIPT_FILENAME /var/www/html/project$fastcgi_script_name; each time your root is changes. This configuration is more flexible. May this helps.

    =================================================================================

    For more information, if you got unix:/run/php/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream, just change in /etc/nginx/nginx.conf, from user nginx; to user www-data;.

    Because the default user and group of PHP-FPM process is www-data as can be seen in /etc/php/7.2/fpm/pool.d/www.conf file (Qoute from https://www.linuxbabe.com/ubuntu/install-nginx-latest-version-ubuntu-18-04):

    user = www-data
    group = www-data
    

    May this information gives a big help

    0 讨论(0)
  • 2020-12-04 14:38

    Try another *fastcgi_param* something like

    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    
    0 讨论(0)
  • 2020-12-04 14:39

    I had the "file not found" problem, so I moved the "root" definition up into the "server" bracket to provide a default value for all the locations. You can always override this by giving any location it's own root.

    server {
        root /usr/share/nginx/www;
        location / {
                #root /usr/share/nginx/www;
        }
    
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }
    }
    

    Alternatively, I could have defined root in both my locations.

    0 讨论(0)
  • 2020-12-04 14:43

    I had this error as well. In my case it was because there was another virtual host that was pointing to the same root directory.

    0 讨论(0)
  • 2020-12-04 14:44

    After upgrading to PHP72, we had an issue where the php-fpm.d/www.conf lost the settings for user/group which was causing this error. Be sure to double check those if your setup involves php-fpm.

    0 讨论(0)
  • 2020-12-04 14:47

    my case: I used relative dir

           location ~* \.(css|js)\.php$ {
    
                root ../dolibarr/htdocs;
    
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                fastcgi_index index.php;
    
                 include /etc/nginx/fastcgi_params;
           }
    
    

    this line did not work too : fastcgi_param SCRIPT_FILENAME /vagrant/www/p1/../p2/htdocs/core/js/lib_head.js.php;

    So I discovered fastcgi_param does not support relative path.

    This works

    fastcgi_param SCRIPT_FILENAME /vagrant/www/p2/htdocs/core/js/lib_head.js.php;
    
    0 讨论(0)
提交回复
热议问题