Setting up settings.php for database connectionfor Drupal 8 , 9 , 10 , and 11

Last updated :  

Introduction

Building on your previous experience with downloading and configuring Drupal manually, you are now ready to dive deeper into the configuration aspects of your Drupal setup. A crucial step in this process is setting up the settings.php file to establish a connection between your Drupal site and your database. Ensuring this is configured correctly is imperative for your site to function as expected, manage data effectively, and provide a smooth user experience.

Why is settings.php Important?

The settings.php file is a key configuration file in Drupal. It contains settings that determine database configurations, directory paths, and other environment-specific settings. This allows Drupal to interact with your database efficiently, making it essential for storing and retrieving content and configurations.

Prerequisites

To proceed with setting up settings.php, ensure you have:

  • Completed the process of downloading and setting up Drupal via tarball or Composer.
  • A database created for Drupal, with a username and password for access.
  • The necessary write permissions for the sites/default directory and the settings.php file.

Locating and Preparing settings.php

First, locate your settings.php file:

  1. Navigate to the Drupal installation directory sites/default from your terminal or file manager.
  2. If you haven't already, copy default.settings.php to settings.php:
  3. cp sites/default/default.settings.php sites/default/settings.php
  4. Ensure the file is writable during the setup:
  5. chmod 664 sites/default/settings.php

Configuring Database Connection in settings.php

To set up the database connection:

  1. Open settings.php in your code editor.
  2. Locate the database settings section, usually identified by a line like:
  3. $databases = [];
  4. Replace this with your database connection array:
  5. 
    $databases['default']['default'] = [
      'database' => 'my_drupal_database',
      'username' => 'my_database_user',
      'password' => 'my_database_password',
      'host' => 'localhost',
      'port' => '3306',
      'driver' => 'mysql',
      'prefix' => '',
    ];
    
  6. Adapt the database credentials based on your specific server setup.

Additional Configuration Settings

Trusted Host Settings

Add trusted host patterns to prevent HTTP Host header attacks:


$settings['trusted_host_patterns'] = [
  '^mydrupalsite\.com$',
  '^www\.mydrupalsite\.com$'
];

File System Permissions

Set up your file system paths:


$settings['file_public_path'] = 'sites/default/files';
$settings['file_private_path'] = '/private/path'; // If using private files

Security Recommendations

It’s crucial to keep your settings.php file secure:

  • After editing, change the permissions to read-only:
  • chmod 644 sites/default/settings.php
  • Never store sensitive data, like database passwords, in source control.

Testing Your Configuration

To verify your setup:

  1. Clear your Drupal site cache, ensuring any changes are refreshed:
  2. drush cr
  3. Visit your site to make sure it loads correctly with no connection errors. Any displayed errors will indicate issues with your database credentials or connection settings.

Conclusion

You've configured your settings.php file correctly, ensuring your Drupal site interacts seamlessly with your database. This knowledge is vital for managing your site’s performance and security.

Next Lesson Preview: Completing the Web-Based Installation Process

In the next lesson, you will learn how to complete the Drupal installation through the web-based installation process. This includes selecting and configuring your site's language, adding administrative users, and setting up essential configurations. Stay tuned for a comprehensive guide to launching your Drupal site!