Configuring multisite in a localhost environment

We are going to configure our Drupal multisite environment on our localhost development server, either on Windows or Linux. I'm going to walk through the steps of setting this up on Windows because there's an additional trick for Windows users to make sure they can get a Drupal multisite environment functioning easily in a Windows development environment. However, these instructions will also work on a Linux system or a MAMP system on Mac OS. We'll also briefly discuss how to best approach setting up multisite environments on production servers including Ubuntu Linux, and using a more common cPanel solution on shared servers. First let's start by configuring a multisite development environment on our local development environment.

You have already prepped your multisite environment in the previous chapter when we set up a Drupal site on our localhost using MoWeS Portable applications. When doing this we installed the Apache web server, PHP, and MySQL on our local development environment. We now have Apache running and we have access to phpMyAdmin to do our database work by going here: http://i27.0.0.i/ phpmyadmin/.

We also installed a Drupal website in our C:\www root web docs folder. Our site is simply called /drupal and is located here via the web browser: http://localhost/.

So, we have the base Drupal core site set up. Our core Drupal site is working smoothly, and in the previous chapter recall that we also configured Memcache API and Authcache to run caching mechanisms on our site. The next thing we're going to do is configure this core /drupal site to be the core codebase for two new Drupal sites that we're going to install and configure. To make things simple, I'm going to just duplicate this site and use the same content for the two new sites. I'll do this by exporting the data from the core /drupal site's database and importing that data into two new MySQL databases that we're going to create. Once we complete the multisite configuration, all three of our Drupal sites will be running from their own database, but they will be sharing the core codebase, core modules, and themes.

Creating the multisite folders

The first thing we need to do is create folders for each site we want to run. To do this, go to your main /drupal site folder and open the main /sites folder. For Drupal multisite, you need to add your site folders into the main core / sites folder. So, we're going to add two sites and create the following folders in our

/drupal/sites folder:

You should now have your folder directory looking like this:

/sites/site1 /site2

The next step is to copy your main / sites/default/settings.php file and paste a copy of it into each of your multisite folders. So you'll have:

/site1/settings.php /site2/settings.php

The reason you do this is that each multisite needs to have its own settings.php configuration file so that you can tell that site what database to work with and also what base_url to use. We'll change those settings in a moment.

You must also create / files folders inside each of your site's directories. So you'll have:

/site1/files /site2/files

Setting up databases for your multisite

Now we already have a core database for our /drupal folder. We're going to create two new databases (one for each of our new sites). To do this I'm going to use the same data that my core database contains. So the first thing I'm going to do is export a clean database dump of the /drupal database and save that locally on my desktop. Then I'll import that data into each new database that I configure. Here are the steps:

1. Fire up your phpMyAdmin at: http://localhost/phpmyadmin.

2. Locate your main site's database and open it.

3. Click on the Export tab and then select Save as file and click on the GO button.

4. This will save a .sql file to your desktop.

5. Now load your main phpMyAdmin screen and click on the Privileges link.

6. Click on Add a new User.

ffi Add a new User

7. Type in a new username in the text field box and set the host to localhost. Type and confirm a password into the Password field. This will be the username and password for your new site's database, so make a note of this because you will be re-entering this information into your site's settings.php file. You will need to know the database username, password, and database name. A hint for what to name your new site's databases: call them _sitei and _site2, using your main core's database name as the appended name. So, for example, my new site's databases are called drupal6_site1 and drupal6_site2.

8. Check the radio button next to Create database with same name and grant all privileges.

9. Click on the Check All link in the Global privileges section and then click on Go. This will create the new database.

Si* Add a new User

-Login Information-

Si* Add a new User

-Login Information-

User name:

1 Usetextfield:

M

|drupalE_site1

Host:

1 Local

B

J localhost

Password:

1 Usetexttield:

m

Re-type:

Generate Password:

[ Generate ] |

Copy ]

1 1

rDatabase for user-

O None

0 Create database with same name and grant all privileges Q Grant all privileges on wildcard name (username\_%)

rDatabase for user-

O None

0 Create database with same name and grant all privileges Q Grant all privileges on wildcard name (username\_%)

-Global privileges ^Check All; / UncheckAII)-

Atote: ftfySQLprivilege names sib expressed in English r D ata- r Stru ctu re-

0 CREATE 0 ALTER 0 INDEX 0 DROP

0 CREATE TEHPORARY TABLES 0 CREATE VIEW 0 SHOW VIEW 0 CREATE ROUTINE 0 ALTER ROUTINE 0 EXECUTE

-Administration-

0 CHANT 0 SUPER 0 PROCESS 0 RELOAD 0 SHUTDOWN 0 SHOW DATABASES 0 LOCK TABLES 0 REFERENCES 0 REPLICATION CLIENT 0 REPLICATION SLAVE 0 CREATE USER

rResource limits—

e options to 0 feero) /e

HAX QUERIES PER HOUR 0 HAX UPDATES PER HOUR Q

HAX CONNECTIONS PER HOUR |Q HAX USER_COUNECTIONS m

10. Repeat steps 5 to 9 to create a database for your second multisite site. Now you have both databases created. The username for each database is the same as the database name, and the password used for each is drupal. You will want to use a more complex password combination on a production site or server, but here we can keep things simple. Make a note of these details, as you'll need them when we edit the settings.php file for each new site.

11. Now you need to import the exported data (from your main core database) into these new databases.

12. Click on the database link in the left sidebar of phpMyAdmin and then click on the Import tab.

13. Browse for your saved .sql file and then make sure the format of the file to be imported is set to SQL.

14. Click on Go and this will import the database tables to your new database.

15. Repeat steps 11 to 14 for your second site's database.

16. You should now have a set of three databases with the same data in your phpMyAdmin. The main drupal6 database is your core site's database. The _site1 and _site2 databases are for each of your site's multisite installations. You should see something in phpMyAdmin that looks like the following screenshot:

Tweaking settings.php for each site

Now that you have duplicated your database and set up your databases for your new multisites, let's make tweaks to the settings.php file for each of these new sites.

Open up your /site1 folder and open the settings.php file to edit it in a text editor. You will need to change the permissions on the settings.php file before editing it because the file is normally set to read-only. To do that in a Windows environment, right-click on the file and select Properties from your menu options. Then on your properties General tab, uncheck Read-only in the Attributes section. You can also click on the Security tab in your Properties box and then check the permissions for your system Users. Make sure that system Users (or at least Administrators if you are currently logged into your computer as its main admin user) have write permissions to the file. You can also change these permissions using FTP or a Filemanager utility. With FTP on your remote host side of the FTP client, right-click on the settings.php file and you should be able to set the permissions for the file. In a Filemanager utility via cPanel, you should see a button (Change Permissions) to change permissions for the file.

We need to update the $db_url to reference our new _sitei database. You insert your database user, password, and then the database name in this line of code in the settings.php file. This site will have its own database, but will share core resources with the main Drupal site configuration, as those database tables have been copied over into the _sitei database. Change your db_url to:

$db_url = 'mysqli://drupal6_site1:[email protected]/drupal6_site1'; $db_prefix = '';

Do the same for the settings.php file of /site2. That site's $db_url should read:

$db_url = 'mysqli://drupal6_site2:[email protected]/drupal6_site2'; $db_prefix = '';

If you are installing multisites, but sharing the additional sites with the main core Drupal site database, you'll need to reference the same $db_url, but update your $db_prefix to note the additional site name. This way you can share the same database and make sure each site has its own database table prefix. That code would look like the following for sitel:

$db_url = 'mysqli://drupal6:[email protected]/drupal6'; $db_prefix = 'site1_';

This would be the site2 configuration:

$db_url = 'mysqli://drupal6:[email protected]/drupal6'; $db_prefix = 'site2_';

For now let's keep this configuration using different databases as per my instructions above.

Editing your Apache configuration

The next step in multisite configuration is to add virtual host entries to our Apache web server configuration. You need to tell Apache to look for these additional sites in our multisite configuration when a site visitor loads the site's web address in the web browser. To do this we use a virtual host entry in the Apache configuration file.

Locate your Apache httpd.conf file. It should be in the following folder location on your localhost server:

C:\apache2\conf\httpd.conf

Open this .conf file in your text editor. You will need to know what port your localhost is listening on. Usually it's port 8 0, but if you are not sure check your server configuration. The default settings in the httpd.conf file should be correct.

First look for the following code in your httpd.conf file and make sure it's uncommented. This code should be in Section 3: Virtual Hosts of your conf file. We want the following uncommented:

NameVirtualHost *:80

Below this you will see some VirtualHost examples. You can use the examples the configuration file provides, and just make sure they are uncommented and have the correct settings as per your configuration. This is what the code should look like:

<VirtualHost *:80>

DocumentRoot /www/drupal ServerName localhost </VirtualHost>

<VirtualHost 127.0.0.1:80> ServerName site1 DocumentRoot "C:/www/drupal" <Directory /> Options All AllowOverride All #Simply allow all directives to be overridden in .htaccess Order allow,deny Allow from localhost #Only allow access from localhost </Directory> </VirtualHost>

<VirtualHost 127.0.0.1:80> ServerName site2 DocumentRoot "C:/www/drupal" <Directory /> Options All AllowOverride All

#Simply allow all directives to be overridden in .htaccess Order allow,deny Allow from localhost #Only allow access from localhost </Directory> </VirtualHost>

Make these code additions and tweaks, then save your httpd.conf file and close it. Then stop your MoWeS Apache web server and restart it so that the above changes take effect.

Here's what the above code does. The first section of code tells Apache to use localhost as the server and to set the document root for that server to the Drupal core site you have configured. In this case, the document root is /www/drupal. This is reflected in this code excerpt:

<VirtualHost *:80>

DocumentRoot /www/drupal ServerName localhost </VirtualHost>

The next set of code is the VirtualHost directives to tell Apache to look for your multisites. First, we have a selection that names ServerName as the multisite entry (site1 or site2) and then instructs it to use the same document root as the main core site /www/drupal. This allows Apache to look for these multisites in your main core / sites folder and then to deliver them to the site visitor when they type in site1 or site2 in the web browser URL menu.

Once you restart the web server, you will need to make one final tweak on Windows computers to make sure this new multisite configuration works for you.

Tweaking the hosts driver file on Windows

Now, in order for this to work on Windows, we need to tweak the driver file for Host configurations in the system settings folder. Browse to the C:\WINDOWS\system32\ drivers\etc folder and look for the hosts file. If you do not see the file, then it's most likely hidden. To show the file on your Windows system, go to the Tools menu at the top of your folder window and select Folder Options. Then click on the View tab and select the Show hidden files and folders radio button that resides in the Hidden files and folders folder icon. Open that file in the text editor.

Look for the hostname code at the bottom of the file and make sure it lists localhost as well as your multisites. You should add the following if it's not there already:

127.0.0.1 localhost

127.0.0.1 site1

127.0.0.1 site2

Make sure this code is uncommented. Save this file.

Tweaking the Base URL

You should also make sure to tweak your Base URL setting in each of your site's settings.php files. Open each settings.php file and look for the Base URL section. You will need to uncomment the line of code that reads:

$base_url = 'http://localhost'; // NO trailing slash!

Make sure this has the URL address for your site. So the above is for the main localhost site and the following are for your multisites (in their respective settings.php files):

$base_url = 'http://site1'; // NO trailing slash! $base_url = 'http://site2'; // NO trailing slash!

Loading your multisites

That's it! You are ready to fire up all of your sites in your web browser. First, load the main localhost site, which is your main core Drupal site. Load that by going to:

Now load sitel, which will be: http://sitei/. Site2 will be: http://site2/.

Each of these sites is now behaving as their own Drupal site with their own databases. We will now work with each site to show you how you can install and enable modules independently on each site or share core and contributed modules from site to site. Congratulations on getting multisite configured and working!

Was this article helpful?

0 0

Post a comment