两个Django项目同时运行,mod_wsgi代理werid

时间:2023-01-18 19:51:36

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.

我试图同时运行两个Django项目。我碰巧在使用mod_wsgi,发现这个网站很奇怪。也许会有一个变通的办法,但我想知道我遗漏了什么,以及如何解决这个问题。

In the apache configuration

在apache配置

# 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.

在Apache重新启动后,如果我连接到'/proto',则显示原始站点。然而,我连接到'/test',没有重新启动Apache, proto站点仍然显示,我无法访问测试站点。

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.

现在我重新启动Apache,这次我要先进行'/test'。测试站点出现了!但是,如果我去“/proto”,它仍然显示测试站点,而不是proto站点。

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

是什么让这一切发生呢?我为每个应用程序添加了不同的SESSION_COOKIE_PATH,以防万一,但是问题仍然存在。


[UPDATED]

(更新)

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

我还尝试了如下方法,给出不同的WSGI应用程序组名称,但是没有成功。

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.

我从守护模式更改为嵌入模式。我猜问题是两个实例共享同一个mod_wsgi守护进程,因此它们的名称空间发生冲突。

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

我希望它们应该被正确地处理,但是在守护模式中,我不能正确地处理它们。

4 个解决方案

#1


14  

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.

如果这仍然不起作用,那么您的WSGI脚本文件可能会出现问题。

#2


4  

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

我还有两个Django项目,但是每个项目都在不同的端口上运行(httpd配置),看起来是这样的:

<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


1  

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:

问题可能与Apache共享WSGI应用程序之间的Python子解释器有关。尝试将此添加到Apache配置中,以避免共享:

WSGIApplicationGroup %{GLOBAL}

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

查看这篇博客文章获得深入的解释和额外的提示(也可以查看评论)。

#4


0  

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

不能对Graham的回答做出评论,所以加上我自己的一个。

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:

对我来说,真正的问题是Python解释器,但我还必须为每个解释器添加Python -path。下面是一个示例配置:

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>

#1


14  

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.

如果这仍然不起作用,那么您的WSGI脚本文件可能会出现问题。

#2


4  

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

我还有两个Django项目,但是每个项目都在不同的端口上运行(httpd配置),看起来是这样的:

<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


1  

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:

问题可能与Apache共享WSGI应用程序之间的Python子解释器有关。尝试将此添加到Apache配置中,以避免共享:

WSGIApplicationGroup %{GLOBAL}

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

查看这篇博客文章获得深入的解释和额外的提示(也可以查看评论)。

#4


0  

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

不能对Graham的回答做出评论,所以加上我自己的一个。

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:

对我来说,真正的问题是Python解释器,但我还必须为每个解释器添加Python -path。下面是一个示例配置:

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>