0

I have a mod_cgi code "site.py" which is used to serve a website. I'm running several instances of this website on a single apache server, each instance has its own config file.

Currently my apache configuration (httpd.conf) contains

ScriptAlias /siteA /path/to/mainA.py # loads configA.txt then runs site.py
ScriptAlias /siteB /path/to/mainB.py # loads configB.txt then runs site.py
ScriptAlias /siteC /path/to/mainC.py # loads configC.txt then runs site.py
. . .

Is there a better way to have multiple sites like this? It would be good to have a way to avoid having a httpd.conf entry for each site and/or not needing a mainX.py for each site.

The goal is to have to create / change fewer files if I want to add another site or when I need to make changes.

mgb
  • 56
  • 5
  • I would like to recommend you to read a bit more of the documentation of the software you are using and update your question. "read a bit more" because this is not the way you are supposed to do this, and "update your question" because you should describe what you would like to achieve, and not how you (failed) to achieve it. – asdmin Jan 04 '20 at 01:09
  • Thx for the info. The goal of asking this question is to find out "the way you are supposed to do this". Reading documentation and comments on CGI, ScriptAlias and hosting multiple websites didn't point to any obvious solutions. If you could suggest what to look for would be appreciated. BTW I'm the main developer of site.py. – mgb Jan 04 '20 at 02:22

2 Answers2

0

The easiest is way is to modify your script to load a different configuration file for each virtual host.

Since you are using mod_cgi your script receives all the information relative to the HTTP request in environment variables. There are some standard CGI variables available in all CGI environments and Apache add some variables of its own.

All you have to do is to read the SERVER_NAME variable and choose your config accordingly with something like:

config_file = 'config-' + os.environ['SERVER_NAME'] + '.txt'
if not os.path.exists(config_file):
    config_file = 'config-default.txt'

On Apache's side you can use a default <VirtualHost> or a <VirtualHost> with all the ServerAlias'es you need.

Edit: After rereading your question it seems your sites are subdirectories of the same URL space. So you'll need to check SCRIPT_NAME instead of SERVER_NAME.

Piotr P. Karwasz
  • 5,748
  • 2
  • 11
  • 21
  • Thx for the suggestion. I like the idea of having site.py use os.environ['REQUEST_URI'] to determine which config file to load. In that case all requests (to /siteA /siteB /siteC . . .) would have to go to the same /path/to/site.py. I'm not sure how to do this, I'll do some digging. – mgb Jan 04 '20 at 02:01
  • I think it got it: ScriptAliasMatch ^/site(.*) "/path/to/site.py" seems to work. – mgb Jan 04 '20 at 03:12
  • Just noticed for some server configurations SCRIPT_NAME doesn't work so I have to use REQUEST_URI. – mgb Jan 23 '20 at 21:32
0

Based on feedback (thanks Piotr) and further research this is the best I came up with. I'm still hoping to find out "the way you are supposed to do this" and would appreciate any further input.

Problem: Too many httpd.conf entries

Solution: A single entry

ScriptAliasMatch ^/site(.*) "/path/to/site.py"

Maybe publish two versions of the code sitesingle.py and sitemulti.py.

Problem: One version of siteX.py for each site.

Solution: have site.py (or sitemulti.py) look at the environment variable SCRIPT_NAME to determine with instance of the site to serve.

Problem: Too many configuration files.

Solution: Have a single configuration file containing subsections for each site as given in the following example:

[path]
    datapath = /path/to/generic/data
    pythonpath = /pythonpath/for/all/sites

[sitea]
    [[path]]
        datapath = /path/to/siteA/data

site.py will merge the site subsection into the general configuration space. (easiest with ConfigObj which supports subsections and merging).

Problem: Need to edit site.py to point it to the correct config file.

No Good Solution: Could set an environment variable in httpd.conf which is read by site.py (or sitesingle.py or sitemulti.py)

mgb
  • 56
  • 5