Internal Server Error after upgrading to Ubuntu 20.04 and php7.4-fpm (Nextcloud 19.0.1)

Nextcloud version: 19.0.1
Operating system and version: Ubuntu 20.04
Apache or nginx version: Nginx 1.18.0
PHP version: 7.4-FPM

The issue you are facing:

I was originally on Ubuntu 18.04 with PHP 7.2-FPM with no problem. I recently upgraded Nextcloud 19.0.1, and then also upgraded Ubuntu to 20.04 and PHP-FPM to 7.4. However, after that, I have not been able to access Nextcloud due to “Internal Server Error”.

I have tried to revert back to php7.2-fpm but the same errors exist.

Is this the first time you’ve seen this error? (Y/N): Y

Steps to replicate it:

  1. Upgraded Nexcloud to 19.0.1 successfully with web updater.
  2. Upgraded Ubuntu from 18.04 to 20.04
  3. Upgraded PHP-FPM to 7.4

The output of your Nextcloud log in Admin > Logging:

// Nothing relevant 

The output of your config.php file in /var/www/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => 'occxxxxxxxxx',
  'passwordsalt' => 'PASSWORD_SALT',
  'secret' => 'SECRET',
  'trusted_domains' => 
  array (
    0 => 'cloud.example.com',
  ),
  'datadirectory' => '/mnt/md10/nextcloud-data',
  'dbtype' => 'mysql',
  'version' => '19.0.1.1',
  'overwrite.cli.url' => 'https://cloud.example.com',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nextcloud',
  'dbpassword' => 'nextcloud_db_pw',
  'installed' => true,
  'maintenance' => false,
  'mysql.utf8mb4' => true,
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'ssl',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => 'no.reply',
  'mail_domain' => 'example.com',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtpauth' => 1,
  'mail_smtphost' => 'mx.example.com',
  'mail_smtpport' => '465',
  'mail_smtpname' => 'no.reply@example.com',
  'mail_smtppassword' => 'SMTP_PW',
  'updater.secret' => 'UPDATER_SECRET',
  'theme' => '',
  'loglevel' => 2,
);

The output of your Apache/nginx/system log in /var/log/____:

2020/08/18 13:06:28 [error] 28804#28804: *4558 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: SCRIPT_NAME in /var/www/nextcloud/lib/base.php on line 156" while reading response header from upstream, client: 192.168.1.1, server: cloud.example.com, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "cloud.example.com"
2020/08/18 13:06:28 [error] 28804#28804: *4558 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught TypeError: Return value of OC\AppFramework\Http\Request::getScriptName() must be of the type string, null returned in /var/www/nextcloud/lib/private/AppFramework/Http/Request.php:836
Stack trace:
#0 /var/www/nextcloud/lib/base.php(161): OC\AppFramework\Http\Request->getScriptName()
#1 /var/www/nextcloud/lib/base.php(581): OC::initPaths()
#2 /var/www/nextcloud/lib/base.php(1090): OC::init()
#3 /var/www/nextcloud/index.php(35): require_once('/var/www/nextcl...')
#4 {main}
  thrown in /var/www/nextcloud/lib/private/AppFramework/Http/Request.php on line 836" while reading upstream, client: 192.168.1.1, server: cloud.example.com, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "cloud.example.com"
2020/08/18 13:06:28 [error] 28804#28804: *4558 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: SCRIPT_NAME in /var/www/nextcloud/lib/base.php on line 156" while reading response header from upstream, client: 192.168.1.1, server: cloud.example.com, request: "GET /favicon.ico HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "cloud.example.com"
2020/08/18 13:06:28 [error] 28804#28804: *4558 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught TypeError: Return value of OC\AppFramework\Http\Request::getScriptName() must be of the type string, null returned in /var/www/nextcloud/lib/private/AppFramework/Http/Request.php:836
Stack trace:
#0 /var/www/nextcloud/lib/base.php(161): OC\AppFramework\Http\Request->getScriptName()
#1 /var/www/nextcloud/lib/base.php(581): OC::initPaths()
#2 /var/www/nextcloud/lib/base.php(1090): OC::init()
#3 /var/www/nextcloud/index.php(35): require_once('/var/www/nextcl...')
#4 {main}
  thrown in /var/www/nextcloud/lib/private/AppFramework/Http/Request.php on line 836" while reading upstream, client: 192.168.1.1, server: cloud.example.com, request: "GET /favicon.ico HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "cloud.example.com"

Other configs:

PHP-FPM Config

/etc/php/7.4/fpm/pool.d/www.conf:

[www]

user = www-data
group = www-data

listen = /run/php/php7.4-fpm.sock

listen.owner = www-data
listen.group = www-data

listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 5

pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

clear_env = no

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
nginx site config

/etc/nginx/conf.d/nextcloud.conf

server {
    listen 80;
    listen [::]:80;
    server_name cloud.example.com;
    # enforce https
    return 301 https://$server_name:443$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name cloud.example.com;

    # Use Mozilla's guidelines for SSL/TLS settings
    # https://mozilla.github.io/server-side-tls/ssl-config-generator/
    # NOTE: some settings below might be redundant
    ssl_certificate /etc/letsencrypt/live/cloud.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cloud.example.com/privkey.pem;

    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
    #
    # WARNING: Only add the preload option once you read about
    # the consequences in https://hstspreload.org/. This option
    # will add the domain to a hardcoded list that is shipped
    # in all major browsers and getting removed from this list
    # could take several months.
    add_header Referrer-Policy "no-referrer" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Download-Options "noopen" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Permitted-Cross-Domain-Policies "none" always;
    add_header X-Robots-Tag "none" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Remove X-Powered-By, which is an information leak
    fastcgi_hide_header X-Powered-By;

    # Path to the root of your installation
    root /var/www/nextcloud;

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

    # The following rule is only needed for the Social app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/webfinger /public.php?service=webfinger last;

    location = /.well-known/carddav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }

    # set max upload size
    client_max_body_size 1024M;
    fastcgi_buffers 64 4K;

    # Enable gzip but do not remove ETag headers
    gzip on;
    gzip_vary on;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
    gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    #pagespeed off;

    location / {
        rewrite ^ /index.php;
    }

    location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
        deny all;
    }
    location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }

    location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
        fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
        set $path_info $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param HTTPS on;
        # Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        # Enable pretty urls
        fastcgi_param front_controller_active true;
        fastcgi_pass php-fpm;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
        try_files $uri/ =404;
        index index.php;
    }

    # Adding the cache control header for js, css and map files
    # Make sure it is BELOW the PHP block
    location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463";
        # Add headers to serve security related headers (It is intended to
        # have those duplicated to the ones above)
        # Before enabling Strict-Transport-Security headers please read into
        # this topic first.
        #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
        #
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        add_header Referrer-Policy "no-referrer" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header X-Download-Options "noopen" always;
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-Permitted-Cross-Domain-Policies "none" always;
        add_header X-Robots-Tag "none" always;
        add_header X-XSS-Protection "1; mode=block" always;

        # Optional: Don't log access to assets
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
        try_files $uri /index.php$request_uri;
        # Optional: Don't log access to other assets
        access_log off;
    }
}
nginx php-fpm upstream config
## PHP handler
    upstream php-fpm {
        server unix:/run/php/php7.4-fpm.sock;
    }
    
    fastcgi_hide_header X-Powered-By;
nginx fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Managed to resolve it myself.

Solution:
In addition to the recommended nginx config, add the following line in the location block where fastcgi_param is configured.

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

So the block should look like:

    location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
        fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
        set $path_info $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param HTTPS on;
        # Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        # Enable pretty urls
        fastcgi_param front_controller_active true;
        fastcgi_pass php-fpm;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

Reload nginx and the error is gone.