简体   繁体   中英

Multiple Django Apps on one Server

How is it possible to serve out multiple Django apps on multiple domains?

For example I have

djangoapp1.com and djangoapp2.com

I then have two separate apps in two separate locations

/srv/www/djangoapp1 /srv/www/djangoapp2

I am running Apache2 with mod_wsgi and I currently have the following in its httpd.conf

WSGIScriptAlias / /srv/www/app1/app1/wsgi.py
WSGIPythonPath /srv/www/app1

<Directory /srv/www/app1/system>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

I also then obviously have the virtual host and I get the django default install page, but now I want to serve up my second app, can anybody point me in the right way to do this?

Django V: 1.4.1

There are many approaches you can take here, and no simple answer - it depends on your requirements and constraints.

The Simplest Thing That Could Possibly Work is very likely the approach suggested by @Hedde - to define the WSGI configuration per-site inside a virtualhost.

A second and possibly more flexible approach would be to run each Django application inside it's own containing application server eg gunicorn (hopefully in a virtualenv to isolate application specific dependencies) on different ports and then use Apache or even Nginx as a proxy for application traffic.

This involves a more complicated server environment to manage, but gives you the advantage of being able to manage your applications individually.

You can reconfigure your available workers, upgrade application versions, make changes to settings.py etc for one application at a time rather than having to restart a single monolithic process.

In addition, although it is, of course possible, monitoring virtualhosts within the same Apache process is more complex than monitoring individual application server instances separately.

YMMV

You can use Apache's VirtualHosts

There's plenty examples for Django, eg here

One of the ways to run multiple django apps on a single server is to run one app on one port each.

How to run two apps on two different ports?

As recommended by Django, I am using wsgi to interface between apache and Django code. Tricky thing here is that you cannot use wsgi in "embedded" mode. In embedded some os resources are shared and hence leads to race conditions. Solution is to use wsgi in daemon mode. In daemon mode, as the name suggests, wsgi runs as separate processes and hence no shared resources. Your two django apps will be unaware of each other.

This is how my configuration looks. I am running apps on port 8082 and 8083. Notice the lines with WSGIDaemonProcess and WSGIProcessGroup and process-group=pas

Listen 8082
<VirtualHost *:8082>
  WSGIDaemonProcess djangoapp1 processes=2 threads=15 display-name=%{GROUP}
  WSGIProcessGroup djangoapp1
  WSGIScriptAlias /apis /home/apis/djangoapp1/xyz/config.wsgi  process-group=djangoapp1      
  WSGIApplicationGroup %{GLOBAL}
  <Directory /home/apis/djangoapp1>
    Options +ExecCGI
    <Files config.wsgi>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>


Listen 8083
<VirtualHost *:8083>

  WSGIDaemonProcess djangoapp2 processes=2 threads=15 display-name=%{GROUP}
  WSGIProcessGroup djangoapp2
  WSGIScriptAlias /apis /home/apis/discovery_api/nykaa/config.wsgi process-group=djangoapp2
  WSGIApplicationGroup %{GLOBAL}
  <Directory /home/apis/djangoapp2>
    Options +ExecCGI
    <Files config.wsgi>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

How to serve both the apps on port 80 ?

<VirtualHost *:80>

  ProxyPreserveHost On
  ProxyRequests Off
  ServerName example.com
  ServerAlias localhost

   ProxyPassMatch "^/(apis/v1/hello$.*)" "http://127.0.0.1:8082/$1"
   ProxyPassMatch "^/(apis/v1/hi$.*)" "http://127.0.0.1:8082/$1"

   ProxyPassMatch "^/(apis/v1/wassup$.*)" "http://127.0.0.1:8083/$1"
   ProxyPassMatch "^/(apis/v1/howdy$.*)" "http://127.0.0.1:8083/$1"
</VirtualHost>

Two ways to access wassup and hello API:

http://example.com:8083/apis/v2/wassup?q=howsitgoing
http://example.com/apis/v2/wassup?q=howsitgoing
http://example.com:8082/apis/v2/hello?q=how are you
http://example.com/apis/v2/hello?q=howareyou

Django Code As recommended on Django website I have replaced

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")

to

os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"

Please see if this is helpful

in apache2.conf or htppd.conf

# Virtual hosts setup
NameVirtualHost *
<VirtualHost *>
    ServerName example1.com
    ............    
    WSGIScriptAlias / /..../path/to/wsgi1.py
</VirtualHost>

<VirtualHost *>
    ServerName example2.com
    ............    
    WSGIScriptAlias / /..../path/to/wsgi2.py
</VirtualHost>
in wsgi1.py

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings1' # or projectnaame.settings1
in wsgi2.py

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings2' # or projectname.settings2
In settings1.py & settings2.py you can make necessary databases and other configurations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM