500 Internal Server Error with nginx local host

18,501

I guess it does have nothing to do with the worker settings you mentioned.

Also having two locations location ~ \.php$ { does not make sense. I read from your comments that you've alradey removed the first one:

    location ~ \.php$ {
    proxy_pass   http://127.0.0.1;
}

Good.

Now find out why PHP-fpm is not working:

  • Is PHP-fpm installed and running? Check with service php7.2-fpm status

  • Where has the socket file gone? Check settings in your socket config file here /etc/php/7.2/fpm/pool.d/

  • Check if PHP-fpm uses the correct user with permissions for the application folders.

Other nginx config issues:

  • Add try_files:

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
  • remove the root html; line from your location ~ \.php$ {section.

  • Don't put these two lines in a location section. Just add them without location / { } around:

    root   /usr/share/nginx/html;
    index index.php index.html index.htm;
    
Share:
18,501

Related videos on Youtube

Dan Bray
Author by

Dan Bray

Updated on September 18, 2022

Comments

  • Dan Bray
    Dan Bray over 1 year

    I am using Linux Mint 17. I am trying to setup PHP 7.1 or PHP 7.2 with nginx but whenever I access a php script I get 500 Internal Server Error. Whenever I restart nginx or PHP, I receive no errors.

    Here are the contents of /etc/nginx/conf.d/default.conf:

    server {
        listen       80;
        listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index index.php index.html index.htm;
        }
    
        #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/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$ {
            root           html;
            fastcgi_pass unix:/var/run/php7.2-fpm.sock;
            #fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }
    

    Any help would be greatly appreciated, as I urgently need to get this working.

    Edit

    I fixed the 500 Error by adding worker_rlimit_nofile 100048 and changing worker_connections to 100048. Unfortunately, now it says "An error occurred" and the logs show:

    2018/02/11 21:53:23 [crit] 13219#13219: *1012 open() "/usr/share/nginx/html/50x.html" failed (24: Too many open files), client: 127.0.0.1, server: localhost, request: "GET /info.php HTTP/1.0", upstream: "http://127.0.0.1:80/info.php", host: "127.0.0.1"
    2018/02/11 21:53:48 [crit] 13263#13263: *56464 connect() to 127.0.0.1:80 failed (99: Cannot assign requested address) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /info.php HTTP/1.0", upstream: "http://127.0.0.1:80/info.php", host: "127.0.0.1"
    
    • Jonah Benton
      Jonah Benton about 6 years
      nginx is listening on port 80, but is also being configured to use port 80 as an upstream (under the "proxy php scripts to apache listening on 127.0.0.1:80"). try getting rid of that location block.
    • Michael Hampton
      Michael Hampton about 6 years
      It looks like nginx is proxying back to itself, not to Apache.
    • Michael Hampton
      Michael Hampton about 6 years
      Then you should change or remove that section.
    • Dan Bray
      Dan Bray about 6 years
      @JonahB I commented that code out and it solved the error. Unfortunately, I now get a different error, *1 connect() to unix:/var/run/php7.2-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7.2-fpm.sock:", host: "localhost". The file php7.2-fpm.sock appears to have disappeared.
  • Dan Bray
    Dan Bray about 6 years
    I got it working. I had to repeatedly check out the log files to fix the problem. There was a problem with fastcgi_param. The default line, '#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;` is completely wrong. There was also a permissions problem with the sock file that I fixed with sudo chmod 777 /run/php/php7.2-fpm.sock.
  • Bob
    Bob about 6 years
    Good to hear you got it working! :-) Yes, that fastcgi_param line should probably be fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;. I did not mention that as it was commended out #already in your config. Your sudo chmod 777 is only a temporary solution and might break on further config changes or restarts. I suggest setting the correct permissions permanently in the according pool config file here /etc/php/7.2/fpm/pool.d/.