Two Django projects running simultaneously and mod

2020-02-04 20:26发布

I'm trying to run two Django projects simultaneously. I happened to be using mod_wsgi, and found the site is acting weird. Perhaps there would be a workaround, but I'd like to know what I'm missing and how to solve the problem.

In the apache configuration

# Setup the Python environment
# As root owns basically everything on a Amazon AMI and root
# cannot be used. Create a folder under /var/run/wsgi
# with the owner as ec2-user and group ec2-user.
WSGISocketPrefix /var/run/wsgi
# Call your daemon process a name
WSGIDaemonProcess pydaemon processes=1 threads=5
# Call your daemon process group a name
WSGIProcessGroup pydaemon
# Point to where the handler file is. This will be different
# If you are using some other framework.
WSGIScriptAlias /test /var/www/html/test/wsgi.py
WSGIScriptAlias /proto /var/www/html/proto/wsgi.py

After Apache restarts, if I connect to '/proto', then the proto site is shown. However, then I connect to '/test', without restarting Apache, the proto site is still shown, and I cannot access to the test site.

Now I restart Apache, this time I go to '/test' first. The test site comes up! However, if I go to '/proto' it still shows the test site, not the proto site.

What could make this happen? I added SESSION_COOKIE_PATH differently for each application just in case, but the problem still exists.


[UPDATED]

I also tried as the following, to give different WSGI application group names, but without luck.

Alias /cuedit /var/local/test/wsgi.py
<Location /test>
SetHandler wsgi-script
Options +ExecCGI
WSGIApplicationGroup test
</Location>
Alias /proto /var/local/proto/wsgi.py
<Location /proto>
SetHandler wsgi-script
Options +ExecCGI
WSGIApplicationGroup proto
</Location>

[UPDATED]

I changed from the daemon mode to the embedded mode. I guess the problem was two instances shared the same mod_wsgi daemon process so their namespace collide.

I would expect they should be handled correctly, but in the daemon mode I couldn't get it right.

4条回答
一纸荒年 Trace。
2楼-- · 2020-02-04 20:41

I also have 2 Django projects however each one is running on a different port (httpd config), it looks something like this:

<VirtualHost *:80>
    ServerAdmin xx
    ServerName xx
    ServerAlias xx
    ErrorLog /path/to/first/project/logs/error.log
    CustomLog /path/to/first/project/logs/access.log combined

    Alias /static/ /path/to/first/project/sitestatic

    WSGIDaemonProcess app processes=1 threads=15 display-name=%{GROUP}
    WSGIProcessGroup app

    WSGIScriptAlias / /path/to/first/project/django.wsgi

    <Directory /path/to/first/project/apache>
       Order deny,allow
       Allow from all
     </Directory>
</VirtualHost>

<VirtualHost *:8080>
    ServerAdmin xx
    ServerName xx
    ServerAlias xx
    ErrorLog /path/to/second/project/logs/error.log
    CustomLog /path/to/second/project/logs/access.log combined

    WSGIDaemonProcess app1 processes=1 threads=15 display-name=%{GROUP}
    WSGIProcessGroup app1

    WSGIScriptAlias / /path/to/second/project/apache/django.wsgi

     <Directory /path/to/second/project/apache>
         Order deny,allow
         Allow from all
     </Directory>
</VirtualHost>
查看更多
戒情不戒烟
3楼-- · 2020-02-04 20:49

Can't comment on the answer given by Graham, so adding one of my own.

The problem for me really was the Python Interpreter, but I also had to add the python-path for each interpreter. Here follows an example configuration:

WSGIDaemonProcess py_app1 processes=1 threads=5 python-path=/path/to/app1
WSGIScriptAlias /app1 /path/to/app1/wsgi.py
<Directory /path/to/app1>
    <Files wsgi.py>
        Order deny,allow
        Allow from all
    </Files>
</Directory>
<Location /app1>
    WSGIProcessGroup py_app1
    WSGIApplicationGroup %{GLOBAL}
</Location>

WSGIDaemonProcess py_app2 processes=1 threads=5 python-path=/path/to/app2
WSGIScriptAlias /app2 /path/to/app2/wsgi.py
<Directory /path/to/app2>
    <Files wsgi.py>
        Order deny,allow
        Allow from all
    </Files>
</Directory>
<Location /app2>
    WSGIProcessGroup py_app2
    WSGIApplicationGroup %{GLOBAL}
</Location>
查看更多
Luminary・发光体
4楼-- · 2020-02-04 20:57

Use this as a workaround:

WSGIDaemonProcess pydaemon-1 processes=1 threads=5
WSGIDaemonProcess pydaemon-2 processes=1 threads=5

WSGIScriptAlias /test /var/www/html/test/wsgi.py

<Location /test>
WSGIProcessGroup pydaemon-1
WSGIApplicationGroup %{GLOBAL}
</Location>

WSGIScriptAlias /proto /var/www/html/proto/wsgi.py

<Location /proto>
WSGIProcessGroup pydaemon-2
WSGIApplicationGroup %{GLOBAL}
</Location>

This will force each application into separate daemon process group and no way they should be able to interfere with each other.

If that still doesn't work, you have problems with your WSGI script file somehow.

查看更多
来,给爷笑一个
5楼-- · 2020-02-04 20:58

The problem might be related to Apache sharing the Python sub interpreter between WSGI applications. Try adding this to the Apache configuration to avoid sharing:

WSGIApplicationGroup %{GLOBAL}

Check this blog post for in-depth explanation and additional tips (check the comments too).

查看更多
登录 后发表回答