views:

260

answers:

4

Hi, We are developing an application that would be offered as a hosted solution. I am struck with understanding how can i use multiple sites with same code without duplicating base code.

eg: website 1: www.example.com and website 2: www.sample.com will run on same code, but would have different configuration settings, and different theme... like we run our own domain names in wordpress.

i wish to know how can i do this.

also in case of database.. would it be better i create individual databases for each website or use the same database with website ID as a column in each table.

pls help me.

[clarification] its not domain alias. like... it would be a service. where different clients will be offered the same application on their own domain name with different theme. something like what blogger does.. with own domain names but same blog application

[technology] specifically am looking at how to use the host name to determine which configuration to use we are using PHP and MySQL

A: 

It's very simple actually.

Your code base can and should be single. You upload it to hosting1 and hosting2. When maintaining, as soon as you updated the code, upload it to both hosting spaces.

I suppose it's how this site works as well. Look at the footer of the page. You have there stackoverflow, meta, superuser and serverfault. If you look what the comment on bottom-right says right now, it would be "svn revision: 5404". The same thing. One code base published to four sites. The databases of course are different and contain completely different content.

So the variable parts are:

  1. Configuration

  2. Settings

  3. Data

Those ones you need to copy.

You need to code it this way that configuration and settings are not hardcoded but either stores in some database table or at least in some external configuration files. These do not make part of code (or implementation if you wish) so they do not need to be in code. If you have them in, take them out.

Developer Art
i need to do it with single hosting. I will be hosting around 1000 websites. so its impractical for me to update all the hosting accounts
Masade
Explain please what is the idea behind having multiple sites? Is it just domain alias or several sites of a family?
Developer Art
its not domain alias.like... it would be a service. where different clients will be offered the same application on their own domain name with different theme.something like what blogger does.. with own domain names but same blog application.
Masade
I have sort of been wondering the same thing. I suppose it would have to employ `$_SERVER['HTTP_HOST']`
Carson Myers
can you help me understanding what it stands for and how to use it?
Masade
+1  A: 

I suppose you could have two different domains pointing at the same server, and set different configurations based on the host name. I wouldn't recommend this though. You may encounter issues with stability and performance, especially if you have a large user base or a large number of domains pointing to the code base.

Generally, if you are running multiple sites, you should have a different server for each.

Edit:

After reading comments on the other answer, I understand your use-case a little better.

If you have a large number of sites that you want to use this set up for, if you design the architecture correctly, and implement a load-balancing scheme of some sort to handle large user loads, you could make this work.

To recap:

  • get multiple servers, and balance the load across them.
  • design the application to handle a large user load.
  • use the host name to determine which configuration to use.
Kyle Trauberman
that would increase my server costs.. how do they usualy do it? blogger is an example that comes to my mind? how do they usually do it?
Masade
see my edit. What language are you using to develop this application? We might be able to help you out with implementation if we know what you are using to develop the application.
Kyle Trauberman
yes we are in plan to do that.. load balancing etc... am unable to get the code on how to do the third part"use the host name to determine which configuration to use"
Masade
What language are you using to write your application?
Kyle Trauberman
we are using PHP and MySQL. I am basically not an expert at PHP and just started learning it
Masade
+1  A: 

Reusing code is definitely desirable. It is hard to imagine from your posts how different the sites will be from each other. Are we talking about logos, and color schemes or different functionality?

I'll start with the different look & feel. I'll also assume that this is the only application on the server.

I recommend planning your directory structure carefully. Something like:

www-root/  
   / lib  
   / themes
        / domain1  
        / domain2  


index.php:

<?php
$host = $_SERVER['HTTP_HOST'];
$include_theme = "themes/" . $host . "/configuration.php";
//make sure the file exists
require_once($include_theme);

This is a simplistic approach but something to thing about.

David W
this was exactly what i was looking for. only theme and config will change. i ll be using the config data from database.
Masade
Thanks. Glad it helped. Post another comment here if you have a further question.
David W
A: 

We do this with about 15,000 unique domains right now. It's not difficult, but it does take some design considerations to implement properly.

You can do it off a single codebase & server, and essentially just build your code to depend entirely upon the incoming request's HTTP_HOST value. Then everything in your DB keys off of that value as a means of segregating data properly and answering the request with the appropriate data/html.

EyePulp