Saturday, April 12, 2008

Creating multiple drupal sites on the local machine

This is supplementary information on the article listed at: http://drupal.org/node/120647

So there were a few things that I had trouble with:


  1. Copy the database to a new database.
    This step kept failing saying that I was failing a constraint test (it said I had a duplicate key). I don't really really see why it failing considering that my users table only had 2 users. The first user was actually a blank row, that had only the default values. This seemed to have been the initialize drupal installation.

    So to solve this, I went into the database and literally just deleted that row. And when I now tried to copy the database to a new database, it worked.

  2. Modifying the httpd-vhosts.conf and hosts file
    It says to comment out existing lines, but I was using the Apache 2.2.6 which had them uncommented anyways. This file is not used anyways unless you tell it to be used in the httpd.conf file which is configured in an early step.

    For each new site, you need to add this:

    <VirtualHost *:80>
    DocumentRoot /www/drupal/
    ServerName databasename
    </VirtualHost>


    What this actually does it set the location of the files to be accessed when someone tries to access the server name. This actually is in relationship with the hosts file in the /windows/system32/drivers/etc directory.

    I had this annoying problem with vista where I couldn't edit the file because I didn't have permissions. So I just copied and pasted the file on my desktop. Opened it, edited it and the pasting it back in the same directory effectively overwriting it


    If you look at this file you should see some lines like this:


    127.0.0.1 localhost
    ::1 localhost
    127.0.0.1 how_far_are_you
    127.0.0.1 drupal_test_database


    This maps IP address to host names (Servers). The number 127.0.0.1 means localhost, and then we say that this IP address can map to three servers: localhost, how_far_are_you, and drupal_test_database.

    Where are these locations/Servers? Well they are actually on your machine!!! These are specified in your httpd-vhosts.conf file above. Each server must be listed with:


    <VirtualHost *:80>
    DocumentRoot /www/drupal/
    ServerName databasename
    </VirtualHost>


    So for example:


    <VirtualHost *:80>
    DocumentRoot ""D:\web\webserver\htdocs\drupal-6.2"
    ServerName how_far_are_you
    </VirtualHost>


    This says that this server "how_far_are_you" is located at the file location specified under document root. For another drupal site, we do exactly the same thing, expect the servername is just different (it is the database name)

    So the interesting thing is that they both point to the same document root. In order to differentiate the two different sites, we use the database names.

    Actually now that I think about it. I think it just matches the name of the site folder (ie .drupal/sites/site_name). Because the settings.php file handles the database name connection. Haven't confirmed this yet though.


    One thing I want to mention is that because turn this virtual host thingy on, typing in http://localhost no longer works. You actually have to add the line:


    <VirtualHost *:80>
    DocumentRoot ""D:\web\webserver\htdocs"
    ServerName localhost
    </VirtualHost>


    For it to work. I think this was taking care of when the virtual host feature wasn't turned on.



This is a pretty interesting thing, and it also allows to just declare like domain names to point to different document roots on the local computer. This is essentially how virtual servers work (where one machine hosts multiple sites!)

One interesting thing I tried to do was skip the part about editing the host file, and see what happened if I just typed in http://name_of_servername. It didn't worked and it searched the web actually. I suspect that part is necessary, because typing in http://... causes it to first search your local machine for valid servername. So the host file is necessary

No comments: