4. Virtual Hosts

The built-in server supports the notion of Virtual Host, that is, the ability to serve different host names on the same server. For instance, the same instance of the built-in server can serve requests to www.foo.com and www.bar.com, provided these domain names are resolved to the IP address of the server. Each of these hosts will have a specific document root, configuration file, storage for session data and users, etc

To add a host, you must first make sure that the host name is resolved to the IP address of the server. The mapping can be done in the system hosts file (if you don't know what it is and how to customize it, type "hosts file" in your favourite search engine)

To understand what you have to do next, lets' start by the end : the directory structure adapted for this new host (called "foo" in the example)

  Karrigell.py
  server_config.py
  default_host_conf.py
  hosts.py
+ karrigell
  + core
  + package
  + ...
+ data
  + www
      + sessions
      conf.py
  + foo
      + sessions
      conf.py
+ www
    index.pih
    ...
+ foo
    default.html
    myscript.py
    ...
+ common
  + admin
  + demo
  + doc
  + ...

The steps you have to follow are :

  • create a new root directory for this host : (server dir)/foo
  • create a new subdirectory in the Data Directory (server dir)/data/foo
  • copy the script default_host_conf.py from the Server Directory to this new subdirectory, and rename it to conf.py
  • edit this script conf.py to update the values of root_dir and data_dir :
    root_dir = os.path.join(server_dir,"foo")
    data_dir = os.path.join(server_dir, "data","foo")
    
  • edit the plain text file hosts in the Server Configuration Directory and add a new line with the host name and the path to the configuration path :
    foo /usr/Karrigell-3.1.1/data/foo/conf.py
    

For the server administrator, a script is provided in the Administration menu to create and remove virtual hosts online

In fact this is the simplest possible configuration : you can put the Root Directory and Data Directory for a new host anywhere in your file system. In this case, you will have to customize the configuration scripts as mentioned in the previous section