Alex Rivera | Logout

How do I run multiple websites on the same server with the same code base in PHP?

Asked 2010-09-27T16:12:22.487
10

I have created a custom, specialized CMS with a number of clients on it, each with their own domain name, website, admin area and database, but all residing on the same server.

Whenever I get a new client, I simply copy all of the code over, change 3 lines in a config file, and then all of their styles/data is taken out of the database or from uploads they post to the server from their own admin area.

Sounds great so far right?

Well when I recently decided to update all of the clients sites, this became a major pain. Obviously i had to change the code on each install. For a major update this is fine, but for frequent tweaks or the like, duplicating the effort of uploading becomes very annoying....I hope to someday have dozens or hundreds of clients, so the code will eventually have to be centralized so that changing it in one place updates it everywhere...how does one do that?

Edit
Report

2 Answers

17

A easy solution would be to put the source code in a subdirectory, except the files, which should be altered for each client (for example, the config file).

You can then put this source code directory somewhere out and just create symlinks to it.

For example, your directory structure might look like:

/var/www/src/index.php
/var/www/src/more_source.php
/var/www/clients/client_a/settings.php
/var/www/clients/client_a/src -> ../../src/
/var/www/clients/client_b/settings.php
/var/www/clients/client_b/src -> ../../src/

If you choose this structure, the only thing you would need to change, would be the include for settings.php (e.g. from require "settings.php" to require "../settings.php").

answered 2010-09-27T16:16:42.687
0

Firstly, you need to be careful of the pitfalls of sharing code between sites. Updating many sites at once multiplies the danger of your updates causing a problem -- if the shared code has a bug, it's better to find out on one site than on a hundred at once!

A deployment management solution may be better than directly sharing the code. (if you're using Drupal, for example, you could use Aegir to manage all your site deployments and updates)

That said, it can be helpful to share code in some cases. I did something like this at a previous job, where they wanted several sites using a single shared code base. What we did was to put the shared libraries into a folder on the server, and each site was then given a symbolic link to that folder, so it treated it as a local folder. Quite easy to do, and quite easy to manage.

answered 2010-09-27T16:34:49.090

Your Answer