Designer, Coder & More...

Setting up a Drupal Multi-site on Mac Local Host

Get Multi-site working on Mac 10.10

Drupal multi-sites lets you run multiple websites from a single Drupal codebase. Each site has its own database, but they can share the same modules and themes. When you need to apply updates, you only need to update one installation.

This feature is often misunderstood. At first glance, it seems like the perfect solution for a Drupal development house, as they can make all their client websites part of a single code base. That way, when it comes to updates, they only need to do them once.

In practise though this is dangerous, as if the code base has an issue, then all the different client websites will be affected as there is only a single point of failure.
Given how easy it is to perform updates with Drush, it makes more sense to have separate installations per a client.

When to use Multi-site

A multi-site is perfect for one site that has different sections with radically different requirements. For example, perhaps you have a website that offers an online magazine with articles, a community section with forums and an e-commerce shop.
The articles, community and e-commerce could all be separate sub-sites with their own databases. But by being a multi-site they can use a shared theme and user log-on.

Working locally on a Mac: benefits and issues

Developing on a local machine is faster than creating directly on the server.
Unfortunately, most of the online tutorials that explain how set up multi-sites locally are written for older versions of Mac OSX. Many tutorials also suggest using MAMP.

Personally, I've never been a fan of MAMP. Although it makes it easy get a development environment running on your Mac, it's redundant as most of the tools it provides are already built into Mac OSX. To be fair, activating these tools requires a little bit of work. But if you want to customise your MAMP environment, you'll have to do most of this work anyway, so it seems odd to skip the setup.

Coolest Guide on the Planet already details how to set up AMP on Mac 10.10 without MAMP. But for a Drupal multi-site there is an extra step.

How Multi-sites work

In a multi-site every site has its own domain. Each domain points to the root Drupal installation. Inside the Drupal installation is the sites folder. In this post, we'll call root installation drupal7 but you probably should give it a better name.
In the drupal7/sites folder, you will put a separate folder for each domain, which will have the same name as the domain.

For example, let's say we have the domains druapl1.com and druapl2.com. Both domains point to the root drupal folder. The folder sturcutre will be as follows:
drupal7/sites/druapl1.com
drupal7/sites/druapl2.com

The drupal1.com and drupal2.com folders will have their own "settings.php files" and "files" folder. The "settings.php" files will each make reference to their own separate databases.

To have modules and themes available to one site only, put it in the sites folder for that site. For example, if drupal1 is our e-commerce site, all the modules for shopping will go in drupal7/sites/drupal1.com/modules.

To have modules and themes available to all sites, put it in the sites folder. E.g. durpal7/sites/themes or durpal7/sites/modules.

Setting up on Mac

Before starting make sure you have set up AMP and set up virtual hosts.

1. Open up a terminal, type sudo nano /etc/apache2/http.conf to setup virtual hosts.

2. Enter the following:

DocumentRoot "/Users/MACUSERNAME/Sites"

<Directory "/Users/MACUSERNAME/Sites">
    AllowOverride All
</directory>

NameVirtualHost 127.0.0.1:80

<VirtualHost 127.0.0.1:80>
    DocumentRoot "/Users/MACUSERNAME/Sites/drupal7"
    ServerName drupal1.com
    <Directory "/Users/MACUSERNAME/Sites">
        Require all granted
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost 127.0.0.1:80>
    DocumentRoot "/Users/MACUSERNAME/Sites/drupal7"
    ServerName drupal2.com
    <Directory "/Users/MACUSERNAME/Sites">
        Require all granted
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

3. Now open a new terminal and type: sudo nano /etc/hosts. Add the following lines:

127.0.0.1     drupal1.com
127.0.0.1     drupal2.com

4. Open a new terminal and type sudo nano /etc/apache2/extra/httpd-vhosts.conf and add the following:

<VirtualHost *:80>  
    ServerName drupal1.com
    DocumentRoot "/Users/MACUSERNAME/Sites/druapl7"
    <Directory "/Users/MACUSERNAME/Sites/druapl7">
        DirectoryIndex index.php index.html
        AllowOverride all
        Options -MultiViews
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost> 

<VirtualHost *:80> 
    ServerName drupal2.com
    DocumentRoot "/Users/MACUSERNAME/Sites/druapl7"
    <Directory "/Users/MACUSERNAME/Sites/druapl7">
        DirectoryIndex index.php index.html
        AllowOverride all
        Options -MultiViews
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost> 

5. Finally, restart apache by going to a new terminal and typing sudo apachectl restart.

6. Now when you open a browser and go to http://drupal1.com and http://drupal2.com. The appropriate sites will display. Make sure you type in the http:// prefix otherwise you might get a browser search.

Accessing the live domain

When you put your website live, if you enter http://drupal1.com in the browser address bar, you will be taken to the localhost version of the site.

There are two ways around this:

1. Use the www. prefix in the address bar (e.g. http://www.drupal1.com).
This is quite handy, as you can omit the prefix when you need to go back to the local site.

2. Disable the local domain in terminal.
Go to the terminal and enter sudo nano /etc/hosts. You can then put a hash in front of the domain name to deactivate it. This will let you access the live site. When you want to access the local site, remove the hash.
All the other terminal settings can remain unchanged.

Q&A

For the Mac user name, do I need to include a tilda (~)?

No! It should be like this:
DocumentRoot "/Users/MACUSERNAME/Sites"

Do not do this:
DocumentRoot "/Users/~MACUSERNAME/Sites"

I've set it up, but it just says "It Works!"

This means you haven't set up virtual hosts properly.

style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-3945684648599994"
data-ad-slot="7784927124">