Drupal Tutorials: directory structure and Its Use Cases

The sites directory in a Drupal installation plays a crucial role in managing site-specific configurations, files, modules, themes, and other resources. It is designed to support multisite setups, allowing multiple Drupal sites to share a single codebase while maintaining separate configurations and content.

Structure of the sites Directory

Here’s an overview of the typical structure within the sites directory:

drupal/
├── includes/
├── misc/
├── modules/
├── profiles/
├── scripts/
├── sites/
│   ├── all/
│   │   ├── libraries/
│   │   ├── modules/
│   │   └── themes/
│   ├── default/
│   │   ├── files/
│   │   ├── settings.php
│   │   └── default.settings.php
│   ├── example.com/
│   │   ├── files/
│   │   ├── settings.php
│   │   └── default.settings.php
│   └── example.sites.php
├── themes/
├── updates/
├── web.config
├── .htaccess
├── cron.php
├── index.php
├── install.php
├── LICENSE.txt
├── MAINTAINERS.txt
├── README.txt
└── xmlrpc.php

Key Directories and Their Uses

1. sites/all/

This directory contains resources that are shared across all Drupal sites in a multisite setup. It’s useful for storing libraries, modules, and themes that should be available to every site using the same Drupal codebase.

  • libraries/: Place shared third-party libraries here.
  • modules/: Place shared modules here.
  • themes/: Place shared themes here.

2. sites/default/

This directory is used for the default Drupal site configuration. If you’re running a single-site setup, this is where all your site-specific files and configurations will be located.

  • files/: Stores uploaded files such as images and documents. It must be writable by the web server.
  • settings.php: The main configuration file for the site, including database connection details and other site-specific settings.
  • default.settings.php: A template for the settings.php file. This file is copied and renamed to settings.php during installation.

3. sites/[sitename]/ (e.g., sites/example.com/)

In a multisite setup, each site has its own directory within the sites directory. The directory name typically matches the site’s domain or subdomain.

  • files/: Stores uploaded files for this specific site.
  • settings.php: Configuration file for this specific site.
  • default.settings.php: Template for creating the settings.php file.

4. example.sites.php

This file is an example configuration file for setting up multisite. It shows how to map hostnames to specific site directories within the sites directory. To enable multisite, you would copy this file to sites/sites.php and customize it.

Use Cases

Single-Site Setup

In a single-site setup, you primarily use the sites/default directory for all site-specific configurations and files. This simplifies management as everything related to your site is contained within this directory.

Multisite Setup

Drupal’s multisite feature allows you to run multiple websites from a single codebase. Each site can have its own configuration, modules, themes, and content. This setup is ideal for scenarios where you need to manage multiple websites that share common features but require separate content and configurations.

Example Multisite Use Case:

  • Corporate Website Network: A company with multiple regional or product-specific websites can manage all these sites from a single Drupal installation. Each site can have its own directory under sites, such as sites/region1.example.com, sites/region2.example.com, and sites/products.example.com.

Shared Resources

The sites/all directory is used to store shared resources like modules and themes that should be available to all sites in a multisite setup. This allows you to maintain and update common functionalities in one place without duplicating files across multiple directories.

Configuration Details

Multisite Configuration Example:

  1. Create Site Directories:Create a directory for each site in the sites directory:
mkdir -p sites/region1.example.com
mkdir -p sites/region2.example.com

Copy Default Settings:

Copy the default.settings.php file to each site directory and rename it to settings.php:

cp sites/default/default.settings.php sites/region1.example.com/settings.php
cp sites/default/default.settings.php sites/region2.example.com/settings.php

Configure Database Settings:

Edit the settings.php file for each site to configure the database connection and other site-specific settings.

Set Up sites.php:

Copy example.sites.php to sites/sites.php and configure it to map hostnames to site directories:

$sites = array(
  'region1.example.com' => 'region1.example.com',
  'region2.example.com' => 'region2.example.com',
);
  1. Configure Web Server:Ensure your web server is configured to serve the correct site based on the hostname. For Apache, this involves setting up virtual hosts.

Example Apache Virtual Host Configuration:

<VirtualHost *:80>
    ServerName region1.example.com
    DocumentRoot /path/to/drupal
    <Directory /path/to/drupal>
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName region2.example.com
    DocumentRoot /path/to/drupal
    <Directory /path/to/drupal>
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

By understanding and leveraging the sites directory, you can effectively manage single-site and multisite Drupal installations, allowing for greater flexibility and organization in your Drupal projects.

<VirtualHost *:80>
    ServerName region1.example.com
    DocumentRoot /var/www/html/drupal
    <Directory /var/www/html/drupal>
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName region2.example.com
    DocumentRoot /var/www/html/drupal
    <Directory /var/www/html/drupal>
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
</VirtualHost>