Wednesday, February 11, 2009

Drupal: Build a Multiple site on a local Ubuntu Machine

When I first started to play around drupal, I was going nuts trying to understand how to get things to work. It took me a long time to get the multi-site one code base to work, just to find out using multiple sites would be better for me. Now that I have a clue of how to do a Multiple site with a shared database, I thought I would write a quick "how-to" so others could give it a go.

Now remember..these are just the basic settings to get you up and running.

Starting fresh on a Unbuntu 8.10 system

So here we go.....First thing you need is a LAMP server.
Go to synaptic, click on edit and click on "Mark Package by Task". Select "LAMP Server" click ok.
Once LAMP is selected, do a search for phpmyadmin (NOT USING "Mark Package by Task") Once those two things are selected, click apply.

During the install you will be asked for a couple of things.
You will be asked to create a password for mysql (make sure you remember it, it will be for the root user)
You will also be asked what web-server to configure. Choose "Apache2"

Now go over here http://www.webmin.com/download.html and download webmin. The download should be under this "Debian package suitable for Debian, Ubuntu or other derived Linux"

Once you have it downloaded, install it by clicking on it....nothing special..

Download drupal http://drupal.org/ and extract the contents. Place the directory you just extract into /var/www (you may want to rename the directory to something else, whatever you need. (My example will be site, site1, site2, etc.)( Name the directory the sites name)

NOTE: I extracted the contents to my desktop, and renamed it to site. Once it was renamed I placed it into /var/www/
I then went back to my desktop and renamed that same directory site1 and copied that into /var/www
I then went back to my desktop and renamed the directory to site2 and copied that into /var/www
So all I did was make multiple copies of the directory, but gave each one a different name.

So the directory structure in /var/www hold
site
site1
site2
on so on.

Hint: If you have problem placing the directory or editing files try this....
In a terminal
sudo Nautilus

Nautilus should open once you enter your password in the terminal from the above command. That Nautilus session is now in sudo, so you should be able to move things around and edit files without too many problems.

Now that that is done, a couple link you need to know about....

To access phpmyadmin http://localhost/phpmyadmin
phpmyadmin is a web interface to administer your databases

To access webmin https://localhost:10000/
Webmin is a web interface to help you administer you machine. We will be using it to configure Apache.

So let's start!

We will be using site.com, and site1.com, etc. as the url's. We will now need to edit our host file.

Edit the host file.
sudo gedit /etc/hosts

Add this line just below the 127.0.0.1 addresses in the host file

127.0.0.1 site.com
127.0.0.1 site1.com
127.0.0.1 site2.com
127.0.0.1 site3.com
and so on...

Save and exit

Now for drupal

You should have the drupal directory you extracted and renamed from the step above in /var/www/


Next "copy" the "default.settings.php" located in /var/www/site/sites/default to a temporary place. Once you have it copied it to a temporary place, rename it to "settings.php"

copy that settings.php into each sites sites/default

example:
/var/www/site/sites/default/settings.php
/var/www/site/sites1/default/settings.php
/var/www/site/sites2/default/settings.php
/var/www/site/sites3/default/settings.php


Now go into phpmyadmin http://localhost/phpmyadmin and create a new database called site, site1, site2, site3 and shared_tables

Log on to phpmyadmin

Where it says "Create new database", enter "site" and click "Create"

Once the database is created, click the home icon (the house icon on the top left)

Now create a second database called "site1" using the same process as above.

continue for each site you need

The last database should be shared_tables


Now lets configure Apache

Go into Webmin https://localhost:10000/ (you will need yo enter your user name and password, not root)

Click on "Servers"

Click "Apache Webserver"

Click on "Configure Apache Moudules"

In the right column click "rewrite", and "vhost_alias"

On the bottom of the page click "Enable Selected Modules"

Now on to Virtual Hots.

Create your virtual host as normal.
So go into Webmin-servers-Apache Webserver. Click on Create virtual host.
Click on "Specific Address" and enter 127.0.0.1
Port click on the third radio/radial button (whatever its called) enter 80 for the value
Document root: Browse to where the site folder is. /var/www/site
Server Name click the second radio/radial button and enter the sites name. site.com
Click "Create Now"

Once created and applied go into /etc/apache2/sites-available
You will see a files with the names for you virtual hosts.

Example: site.com.conf

Now if needed open the file and change the line

DocumentRoot /var/www/site
to
DocumentRoot "/var/www/site"

While your in there do this also:

Under DocumentRoot "/var/www/site"
add this
AllowOverride All

Example of what it should look like:

DocumentRoot "/var/www/site"
AllowOverride All
allow from all
Options +Indexes
< /Directory >
ServerName site.com
< /VirtualHost >

Seems google filters the first line
VirtualHost 127.0.0.1:80 with the <>

That should take care of Clean URLs

Save the file

Do the above for each site

Restart Apache.

Back to Drupal.

Now open your browser and enter http://site.com for the URL
You can now run the installer for your main site.
When at the screen asking for the database, enter "site"
Enter "root" for the user name, and the password you created during the install. The rest should be easy.

Once that is complete go to http://site1.com, run the installer for the second site.

Once that is complete you will have multiple sites running. Now we want to share a few tables between them.

Log onto phpmyadmin
Export these tables from your main site.

authmap
profile_fields
profile_values
role
sessions
users
vocabulary
vocabulary_node_types
term_data
term_hierarchy
term_node
term_relation
term_synonym

All of the tables may not be in the database. Export the ones that are.
Note: If you need to know how to export read below.

Once they have been exported, import them into the shared_tables database

Once the tables have been imported, you will need to drop the listed tables from the other databases. (site, site1, site2)
authmap
profile_fields
profile_values
role
sessions
users
vocabulary
vocabulary_node_types
term_data
term_hierarchy
term_node
term_relation
term_synonym

Once the tables have been dropped, add the following to each sites settings.php just below

$db_url = 'mysqli://root:password@localhost/site';
changing password for your password, and site for each database name.

$db_prefix = array(
'default' => '',
'authmap' => 'shared_tables.',
'profile_fields' => 'shared_tables.',
'profile_values' => 'shared_tables.',
'role' => 'shared_tables.',
'sessions' => 'shared_tables.',
'users' => 'shared_tables.',
'vocabulary' => 'shared_tables.',
'vocabulary_node_types' => 'shared_tables.',
'term_data' => 'shared_tables.',
'term_hierarchy' => 'shared_tables.',
'term_node' => 'shared_tables.',
'term_relation' => 'shared_tables.',
'term_synonym' => 'shared_tables.',
);

Save each file.
If your databases have different user names and passwords try replacing
$db_url = 'mysqli://root:password@localhost/site';
with something like this
$db_url['default'] = 'mysql://root:password@localhost/site';
$db_url['site1db'] = 'mysql://user:password@localhost/site1db';


If all went well, you will now be sharing user and Taxonomy information across sites.

Database info.
A couple things good to know.
Once your is site set up the way you want, you may want to export your databases. The reason for this is there are people who will tweak and brake things. So if you export your database you can simply just re-import it and get you site back up in moments.
To export look here http://drupal.org/node/81993

To import look here http://drupal.org/node/81995 (be for you try to import, read below)
Now when you try to importing the database you will run into a few errors. To overcome these you will need to edit /etc/php5/apache2/php.ini
Look for: "memory_limit = 16M"
Change the 16M limit to 64M or 128M
Also uncomment "upload_tmp_dir" and save. Re-start Apache (sudo apache2ctl restart) You should now be able to import with out any problems.

To give you the "simple" run down on how to import the database, do this.
logon to phpmyadmin. Click on the database name you want to import (left side) (or if you want to import to a new database, create the new database, the click the database name on the left.
Once you have clicked on the database name click "import" (top middle of screen)
Click browse and browse to where your backup file is (ends with .sql if you did the export for the how to above)
click "Go"...thats it, its imported.

This is my first rev for this. I will be improving it as I go along. One other thing I'd like to be able to do, but don't know how yet, is share primary and secondary links.