[Drupal] How to setup a multisite Drupal installation in your localhost?

| | 1 min read

If we want to run more than one site using a single Drupal installation we can use the multisite feature of Drupal. This multisite feature will allow us to share the core, contributed modules and themes among several sites. Nevertheless each sites has its own database and configuration. Read on to know how to setup a multisite Drupal installation in your localhost.

Follow the steps below to setup a multisite Drupal installation in your localhost

  1. Firstly open
    /etc/hosts

    in a text editor and add the following line to it

    128.1.1.1 <site name>
    For eg:- 128.1.1.1 masco.local
    128.1.1.1 lmaccc.local
    128.1.1.1 msicallcenter.local
  2. Next go to
    /etc/apache2/sites-available
  3. From there copy default to eg:- masco.local
  4. <VirtualHost *:80 >
    ServerName <site name>
    DocumentRoot <path>
    <Directory <path/>>
    AllowOverride All
    </Directory>
    </VirtualHost>
  5. Create this file for each site.
  6. In the terminal type the command -
    a2ensite <site name>

    to enable the site

  7. Enable each site like this. Eg:- a2ensite masco.local
  8. After that you should restart Apache using a
    sudo /etc/init.d/apache2 restart 

Drupal Changes

Following are the changes to be made in your Drupal site

  1. Set the sites/default folder for the main site and then copy the 'default' folder for other sites and rename it with sitename.
  2. Create a separate database for each site.
  3. Export the shared tables if there are any and import these tables to a new database. Then delete these tables from all the main database.
  4. Then edit the settings.php to change
    $db_url = 'mysqli://username:password@localhost/databasename';
    
    Add $db_prefix = array(
    'default' => '',
    'users' => 'masco_shared.',
    'sessions' => 'masco_shared.',
    'role' => 'masco_shared.',
    'authmap' => 'masco_shared.',
    'sequences' => 'masco_shared.',
    'users_roles' => 'masco_shared.',
    'permission' => 'masco_shared.',
    );

    to specify the shared database and tables.

  5. Then access each site in the browser and change the required admin settings.

Hope that helps.