Linux – 500 Internal Server Error with nginx local host

linuxlocalhostnginxPHP

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"

Best Answer

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;