Error 500 - Failed to connect to the database: exception occurred in driver could not find driver

I had the same error. I was able to diagnose by:

  1. running sudo -u www-data php -i and noting that it was running php version 8.3
  2. I created a text file with <?php phpinfo(); ?> and saved it as “test.php” in /var/www/html
  3. sudo mv index.php index.php.bak … this changes the default php script for nextcloud to have a different name.
  4. sudo cp test.php index.php … this makes the test script created in #2 the main script for the nextcloud instance.
  5. Opened the nextcloud instance in a web browser and noticed that it was reporting php 8.2

So I had a mismatch … the php was using 8.3 but apache2 was using 8.2.

To remedy this, I issued the following commands:

  1. sudo apt install libapache2-mod-php8.3 - this ensures the php module you need is installed
  2. sudo a2enmod php8.3 - this enables apache2 to use php8.3
  3. sudo a2dismod php8.2 - this disables php8.2
  4. sudo systemctl restart apache2 - this restarts apache
    (commands from How to install or upgrade to PHP 8.3 on Ubuntu and Debian • PHP.Watch)

After that was done, I restored the original index.php file in /var/www/html using sudo mv index.php.bak index.php. Then nextcloud started right up.