Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple sites from a single Python web framework [duplicate]

I know you can do redirection based on the domain or path to rewrite the URI to point at a site-specific location and I've also seen some brutish if and elif statements for every site as shown in the following code, which I would like to avoid.

if site == 'site1':
    ...
elif site == 'site2:
    ...

What are some good and clever ways of running multiple sites from a single, common Python web framework (i.e., Pylons, TurboGears, etc)?

like image 796
Sean Avatar asked Sep 17 '08 16:09

Sean


4 Answers

Django has this built in. See the sites framework.

As a general technique, include a 'host' column in your database schema attached to the data you want to be host-specific, then include the Host HTTP header in the query when you are retrieving data.

like image 68
Jim Avatar answered Nov 09 '22 11:11

Jim


Using Django on apache with mod_python, I host multiple (unrelated) django sites simply with the following apache config:

<VirtualHost 1.2.3.4>
        DocumentRoot /www/site1
        ServerName site1.com
        <Location />
                SetHandler python-program
                SetEnv DJANGO_SETTINGS_MODULE site1.settings
                PythonPath "['/www'] + sys.path"
                PythonDebug On
                PythonInterpreter site1
        </Location>
</VirtualHost>

<VirtualHost 1.2.3.4>
        DocumentRoot /www/site2
        ServerName site2.com
        <Location />
                SetHandler python-program
                SetEnv DJANGO_SETTINGS_MODULE site2.settings
                PythonPath "['/www'] + sys.path"
                PythonDebug On
                PythonInterpreter site2
        </Location>
</VirtualHost>

No need for multiple apache instances or proxy servers. Using a different PythonInterpreter directive for each site (the name you enter is arbitrary) keeps the namespaces separate.

like image 35
Gabriel Ross Avatar answered Nov 09 '22 11:11

Gabriel Ross


I use CherryPy as my web server (which comes bundled with Turbogears), and I simply run multiple instances of the CherryPy web server on different ports bound to localhost. Then I configure Apache with mod_proxy and mod_rewrite to transparently forward requests to the proper port based on the HTTP request.

like image 44
Eli Courtwright Avatar answered Nov 09 '22 09:11

Eli Courtwright


Using multiple server instances on local ports is a good idea, but you don't need a full featured web server to redirect HTTP requests.

I would use pound as a reverse proxy to do the job. It is small, fast, simple and does exactly what we need here.

WHAT POUND IS:

  1. a reverse-proxy: it passes requests from client browsers to one or more back-end servers.
  2. a load balancer: it will distribute the requests from the client browsers among several back-end servers, while keeping session information.
  3. an SSL wrapper: Pound will decrypt HTTPS requests from client browsers and pass them as plain HTTP to the back-end servers.
  4. an HTTP/HTTPS sanitizer: Pound will verify requests for correctness and accept only well-formed ones.
  5. a fail over-server: should a back-end server fail, Pound will take note of the fact and stop passing requests to it until it recovers.
  6. a request redirector: requests may be distributed among servers according to the requested URL.
like image 31
defnull Avatar answered Nov 09 '22 11:11

defnull