简体   繁体   English

一台服务器上的多个 Django 应用程序

[英]Multiple Django Apps on one Server

How is it possible to serve out multiple Django apps on multiple domains?如何在多个域上提供多个 Django 应用程序?

For example I have例如我有

djangoapp1.com and djangoapp2.com djangoapp1.com 和 djangoapp2.com

I then have two separate apps in two separate locations然后我在两个不同的位置有两个单独的应用程序

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

I am running Apache2 with mod_wsgi and I currently have the following in its httpd.conf我正在使用 mod_wsgi 运行 Apache2,我目前在其 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 默认安装页面,但现在我想提供我的第二个应用程序,有人能指出我正确的方法吗?

Django V: 1.4.1 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.可能可行的最简单的事情很可能是@Hedde 建议的方法 - 在虚拟主机内定义每个站点的 WSGI 配置。

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.第二种可能更灵活的方法是在它自己的包含应用程序服务器中运行每个 Django 应用程序,例如gunicorn (希望在virtualenv中隔离应用程序特定的依赖项)在不同的端口上,然后使用 Apache 甚至Nginx作为应用程序流量的代理。

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.您可以一次为一个应用程序重新配置可用的工作线程、升级应用程序版本、更改 settings.py 等,而不必重新启动单个单一进程。

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.此外,尽管当然有可能,但监控同一 Apache 进程中的虚拟主机比单独监控单个应用程序服务器实例更复杂。

YMMV青年会

You can use Apache's VirtualHosts您可以使用 Apache 的VirtualHosts

There's plenty examples for Django, eg here Django 有很多例子,例如这里

One of the ways to run multiple django apps on a single server is to run one app on one port each.在单个服务器上运行多个 django 应用程序的一种方法是在每个端口上运行一个应用程序。

How to run two apps on two different ports?如何在两个不同的端口上运行两个应用程序?

As recommended by Django, I am using wsgi to interface between apache and Django code.按照 Django 的建议,我使用 wsgi 来连接 apache 和 Django 代码。 Tricky thing here is that you cannot use wsgi in "embedded" mode.这里棘手的事情是您不能在“嵌入式​​”模式下使用 wsgi。 In embedded some os resources are shared and hence leads to race conditions.在嵌入式中,一些操作系统资源是共享的,因此会导致竞争条件。 Solution is to use wsgi in daemon mode.解决方案是在守护进程模式下使用 wsgi。 In daemon mode, as the name suggests, wsgi runs as separate processes and hence no shared resources.在守护进程模式下,顾名思义,wsgi 作为单独的进程运行,因此没有共享资源。 Your two django apps will be unaware of each other.您的两个 django 应用程序将不知道彼此。

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我在端口 8082 和 8083 上运行应用程序。注意带有WSGIDaemonProcessWSGIProcessGroup以及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 ?如何在端口 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:访问 wassup 和 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 Django 代码按照Django 网站上的推荐,我已经替换了

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在 apache2.conf 或 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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