Get Database error after Installing Roundcube on Ubuntu 24.04

If you're encountering a database error after installing Roundcube on Ubuntu 24.04, it typically indicates an issue with the connection between Roundcube and your database (usually MySQL/MariaDB). Below are the steps to troubleshoot and resolve the issue:

1. Verify Database Configuration

Roundcube requires a properly configured database connection. Check the Roundcube configuration file to ensure the database settings are correct. -> Open the Roundcube configuration file:
sudo nano /var/www/roundcube/config/config.inc.php
-> Look for the following lines and ensure they match your database settings:
<?php
$config['db_dsnw'] = 'mysql://roundcubeuser:password@localhost/roundcubedb';
?>
Replace: -> `roundcubeuser` with your database username. -> `password` with your database password. -> `roundcubedb` with your database name. Save the file and exit (`Ctrl + X`, then `Y` to confirm).

2. Check Database Credentials

Ensure the database user and database exist and have the correct permissions. -> Log in to MySQL/MariaDB:
sudo mysql -u root -p
-> Verify the database and user:
```sql
SHOW DATABASES;
SELECT User, Host FROM mysql.user;
-> If the database or user doesn't exist, create them:
```sql
CREATE DATABASE roundcubedb;
CREATE USER 'roundcubeuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON roundcubedb.* TO 'roundcubeuser'@'localhost';
FLUSH PRIVILEGES;

3. Test Database Connection

Manually test the database connection to ensure Roundcube can connect. -> Install the MySQL client (if not already installed):
```bash
sudo apt install mysql-client
```
-> Test the connection:
```bash
mysql -u roundcubeuser -p -h localhost roundcubedb
```
Enter the password when prompted. If you can connect, the issue is likely with Roundcube's configuration.

4. Check Database Schema

Roundcube requires a specific database schema. If the schema isn't initialized, you'll encounter errors. -> Import the Roundcube database schema:
```bash
sudo mysql -u root -p roundcubedb < /var/www/roundcube/SQL/mysql.initial.sql
```
-> If you're using a different database (e.g., PostgreSQL), use the corresponding schema file.

5. Check Logs for Errors

Inspect the Roundcube and database logs for more details about the error. -> Roundcube logs:
```bash
sudo nano /var/www/roundcube/logs/errors.log
```
-> MySQL/MariaDB logs:
```bash
sudo tail -f /var/log/mysql/error.log
```

6. Verify PHP Extensions

Roundcube requires specific PHP extensions for database connectivity. Ensure they are installed and enabled. -> Install required PHP extensions:
```bash
sudo apt install php-mysql php-json php-xml php-mbstring php-intl php-zip
```
-> Restart the web server:
```bash
sudo systemctl restart apache2
```

7. Check Web Server Permissions

Ensure the web server has the necessary permissions to access the Roundcube directory. -> Set correct permissions:
```bash
sudo chown -R www-data:www-data /var/www/roundcube
sudo chmod -R 755 /var/www/roundcube
```

8. Reinstall Roundcube (Optional)

If the issue persists, consider reinstalling Roundcube. -> Remove Roundcube:
```bash
sudo rm -rf /var/www/roundcube
```
-> Reinstall Roundcube:
```bash
sudo apt install roundcube roundcube-core roundcube-mysql
```

9. Common Errors and Fixes

-> Error: "Database connection failed": - Verify the database credentials in `config.inc.php`. - Ensure the database server is running:
```bash
sudo systemctl status mysql
```
-> Error: "Table not found": - Import the Roundcube database schema as described in Step 4. If you still encounter issues, provide the exact error message, and I can help you further!