tags:

views:

96

answers:

4

I'm running the same php script on many domains on an apache2 server. Only some of the files change between domains, and the rest are always the same. Right now, every time I set up a new domain, I copy all the files, but I'd really like to have these common files in one place so any changes would affect all domains.

I've thought of using a bunch of symlinks to point at common files. Is this an ok approach, or are there some simple edits I can make to the php scripts or apache configuration files to make this more efficient?

Thanks!

+1  A: 

This can be a bit tricky, as the application almost needs to know you're doing this. IME, it works best when you can divide the app into common code and instance code in two separate directory trees. The common code also needs to not do anything silly like include a file that has to be in the instance tree.

A single point of entry to load the common code is also a big bonus because then you can chain a few very small files: the instance code includes one in it's own directory; that file includes a file outside the instance code; that file then either loads the entry point file for the common code, or loads another that does. Now this is only one way to do it, but it means you have just one file that needs to know where the common code is (so you can move it if you have to with minimal effort), and if you do it right, all the various instance code trees load it, albeit indirectly.

staticsan
+3  A: 

The way I do this kind of thing is to create a "common" directory, where I place all the file that can be shared between each site. Then I simply include them wherever they are needed.

This is pretty good because allows to add features across multiple sites.

0plus1
Isn't there a PHP security setting you have to change to allow including across domains?
Shadow
I'm pretty sure he's talking about a shared-hosting situation, where multiple sites or domains are hosted on the same server in different directories. In this case PHP doesn't know or care about domains, since he'd be passing a local file path to include(), as opposed to using virtual() or something.
ithcy
+2  A: 

I'd suggest abstracting the common code into a set of 'library' scripts. Placing these in a common directory, and making that available by modifying PHP's include_path variable. This means you most likely won't have to modify your current scripts, while still removing the need to have more than one copy.

This path could (and probably should) be outside of your public directories. This enhances the security of your websites by not making them directly available to outside users.

Jordy Boom
+1  A: 

You could have a library directory that sits above all of your sites, and a config file that states which library files your sites should include by default. You can then have another config file within each site that overrides the global config. These config files can be used to generate include('../../lib/*.php') statements to build the basic function toolkit needed for each site.

some_high_level_directory/
-> lib/
   ->*.php (library files)
-> config.php (global library includes)
-> site_1/
   -> config.php (library includes that only relate to site_1)
   -> www/
-> site_2/
   -> config.php (library includes that only relate to site_2)
   -> www/
-> etc, etc

Hopefully that makes sense... :)

MatW