How I manage multiple Drupal websites using the same codebase

In my last email, I mentioned that I maintain several Drupal websites for a client using the same codebase, but how do I do that?

The sites use Drupal's multisite functionality to have a separate directory for each site, each with its own settings file and files, and potentially modules and themes. Whilst there are some downsides to this approach, and we did evaluate alternatives, this approach allows us to keep one hosting account and save the client money compared to hosting each site separately.

Each site has a separate database and configuration files, so out of the box, I can customise what functionality is needed on each site by turning modules on and off. Whilst this is fine for larger pieces of functionality, for smaller pieces I like to use feature flags.

I use feature flags on single-site projects to separate deploying code from releasing a change, but I can also use them here to toggle something per-site. This could be using a module like Feature toggle or another way like a checkbox on a settings form. Anything that I can use to say "Do this if that is enabled".

Settings such as setting an endpoint URL or storing some API credentials would be set in an admin form and stored as configuration per site.

I've tried various iterations of this - initially duplicating the custom code and having several near-identical versions of the same modules (this wasn't good for maintenance). We also used environment variables. However, this didn't scale as I added more sites and needed to create a new set of environment variables every time.

This approach has worked well for the last few years on their original websites and should continue to work well as I upgrade and migrate them to their next versions.