Django、Virtualenv、nginx + uwsgi导入模块wsgi错误

发布于 2025-01-02 18:12:04 字数 4457 浏览 1 评论 0原文

我尝试使用 nginx、virtualenv 和 uwsgi 在临时服务器上设置我的 django 项目,但我不断收到导入模块 wsgi 错误。

如果有一个社区我可以在这里找到答案...提前谢谢大家。

这是我的配置文件:

我的 django 项目上的 uwsgi.py:Nginx

import os
import sys 
import site

site.addsitedir(os.path.join(os.environ['WORKON_HOME'],'project/lib/python2.6/site-packages'))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../../'))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.configs.staging.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

配置

# nginx configuration for project.maumercado.com

server {
    server_name project.maumercado.com;
    access_log /home/ubuntu/logs/project/nginx/access.log;
    error_log /home/ubuntu/logs/project/nginx/error.log;

    location / {
            uwsgi_pass unix:/tmp/uwsgi.sock;
            include /etc/nginx/uwsgi_params;
    }

    location /static {
            root /home/ubuntu/django-projects/project/project/media;
    }
    location /media {
            root /home/ubuntu/django-projects/project/project/media;
    }
}

,并且我的 uwsgi.conf

# file: /etc/init/uwsgi.conf 
description "uWSGI starter"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

# home - is the path to our virtualenv directory
# pythonpath - the path to our django application
# module - the wsgi handler python script

exec /home/ubuntu/ve/project/bin/uwsgi \
--uid www-data \
--pythonpath /home/ubuntu/django-projects/project/project/configs/staging/ \
--socket /tmp/uwsgi.sock \
--chmod-socket \
--module wsgi \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /home/ubuntu/logs/project/uwsgi.log

Nginx 日志除了 access.log 中的 500 之外没有说明任何内容,所以这里是 uwsgi.log:

Mon Feb  6 13:58:23 2012 - *** Starting uWSGI 1.0.2.1 (32bit) on [Mon Feb  6 13:58:23 2012] ***
Mon Feb  6 13:58:23 2012 - compiled with version: 4.4.5 on 06 February 2012 12:32:36
Mon Feb  6 13:58:23 2012 - current working directory: /
Mon Feb  6 13:58:23 2012 - detected binary path: /home/ubuntu/ve/project/bin/uwsgi
Mon Feb  6 13:58:23 2012 - setuid() to 1000
Mon Feb  6 13:58:23 2012 - your memory page size is 4096 bytes
Mon Feb  6 13:58:23 2012 - chmod() socket to 666 for lazy and brave users
Mon Feb  6 13:58:23 2012 - uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Mon Feb  6 13:58:23 2012 - Python version: 2.6.6 (r266:84292, Sep 15 2010, 16:02:57)  [GCC 4.4.5]
Mon Feb  6 13:58:23 2012 - Set PythonHome to /home/ubuntu/ve/project
Mon Feb  6 13:58:23 2012 - Python main interpreter initialized at 0x9a9d740
Mon Feb  6 13:58:23 2012 - your server socket listen backlog is limited to 100 connections
Mon Feb  6 13:58:23 2012 - *** Operational MODE: preforking ***
Mon Feb  6 13:58:23 2012 - added /home/ubuntu/django-projects/project/ to pythonpath.
ImportError: No module named wsgi
Mon Feb  6 13:58:23 2012 - unable to load app 0 (mountpoint='') (callable not found or import error)
Mon Feb  6 13:58:23 2012 - *** no app loaded. going in full dynamic mode ***
Mon Feb  6 13:58:23 2012 - *** uWSGI is running in multiple interpreter mode ***
Mon Feb  6 13:58:23 2012 - spawned uWSGI master process (pid: 551)
Mon Feb  6 13:58:23 2012 - spawned uWSGI worker 1 (pid: 588, cores: 1)
Mon Feb  6 13:58:23 2012 - spawned uWSGI worker 2 (pid: 589, cores: 1)

我不知道是否我设置项目的方式与它有任何关系,但无论如何,这是我用来重定向 django 实用程序的管理文件:

manage.sh

#!/bin/bash

python ./project/configs/${DEPLOYMENT_TARGET:="common"}/manage.py $*

,以防万一,这就是我设置的方式Django 项目:

project
|-manage.sh -> this fellow is redirected to settings.py (production, common or staging)
|-requirements.txt
|-README
|-dashbard.py
|-project.sqlite
|- project/
    |- apps
        |- accounts
        |-other internal apps
    |- configs 
        |- common -> for local development
            |-settings.py
            |-manage.py
            |-urls
        |-staging
            |-manage.py
            |-settings.py
            |-wsgi.py
            |-logging.conf
        |-production
            |-manage.py
            |-settings.py
            |-wsgi.py
            |-logging.conf
    |-media
    |-templates

Im trying to setup my django project on a staging server with nginx, virtualenv, and uwsgi, but I keep getting an import module wsgi error.

If theres a community I can find an answer is here... Thank you all in advance.

This are my configuration files:

uwsgi.py on my django project:

import os
import sys 
import site

site.addsitedir(os.path.join(os.environ['WORKON_HOME'],'project/lib/python2.6/site-packages'))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../../'))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.configs.staging.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Nginx Configuration

# nginx configuration for project.maumercado.com

server {
    server_name project.maumercado.com;
    access_log /home/ubuntu/logs/project/nginx/access.log;
    error_log /home/ubuntu/logs/project/nginx/error.log;

    location / {
            uwsgi_pass unix:/tmp/uwsgi.sock;
            include /etc/nginx/uwsgi_params;
    }

    location /static {
            root /home/ubuntu/django-projects/project/project/media;
    }
    location /media {
            root /home/ubuntu/django-projects/project/project/media;
    }
}

and, my uwsgi.conf

# file: /etc/init/uwsgi.conf 
description "uWSGI starter"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

# home - is the path to our virtualenv directory
# pythonpath - the path to our django application
# module - the wsgi handler python script

exec /home/ubuntu/ve/project/bin/uwsgi \
--uid www-data \
--pythonpath /home/ubuntu/django-projects/project/project/configs/staging/ \
--socket /tmp/uwsgi.sock \
--chmod-socket \
--module wsgi \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /home/ubuntu/logs/project/uwsgi.log

Nginx logs does not state anything besides a 500 in access.log, so heres the uwsgi.log:

Mon Feb  6 13:58:23 2012 - *** Starting uWSGI 1.0.2.1 (32bit) on [Mon Feb  6 13:58:23 2012] ***
Mon Feb  6 13:58:23 2012 - compiled with version: 4.4.5 on 06 February 2012 12:32:36
Mon Feb  6 13:58:23 2012 - current working directory: /
Mon Feb  6 13:58:23 2012 - detected binary path: /home/ubuntu/ve/project/bin/uwsgi
Mon Feb  6 13:58:23 2012 - setuid() to 1000
Mon Feb  6 13:58:23 2012 - your memory page size is 4096 bytes
Mon Feb  6 13:58:23 2012 - chmod() socket to 666 for lazy and brave users
Mon Feb  6 13:58:23 2012 - uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Mon Feb  6 13:58:23 2012 - Python version: 2.6.6 (r266:84292, Sep 15 2010, 16:02:57)  [GCC 4.4.5]
Mon Feb  6 13:58:23 2012 - Set PythonHome to /home/ubuntu/ve/project
Mon Feb  6 13:58:23 2012 - Python main interpreter initialized at 0x9a9d740
Mon Feb  6 13:58:23 2012 - your server socket listen backlog is limited to 100 connections
Mon Feb  6 13:58:23 2012 - *** Operational MODE: preforking ***
Mon Feb  6 13:58:23 2012 - added /home/ubuntu/django-projects/project/ to pythonpath.
ImportError: No module named wsgi
Mon Feb  6 13:58:23 2012 - unable to load app 0 (mountpoint='') (callable not found or import error)
Mon Feb  6 13:58:23 2012 - *** no app loaded. going in full dynamic mode ***
Mon Feb  6 13:58:23 2012 - *** uWSGI is running in multiple interpreter mode ***
Mon Feb  6 13:58:23 2012 - spawned uWSGI master process (pid: 551)
Mon Feb  6 13:58:23 2012 - spawned uWSGI worker 1 (pid: 588, cores: 1)
Mon Feb  6 13:58:23 2012 - spawned uWSGI worker 2 (pid: 589, cores: 1)

I don't know if the way I set up my project has anything to do with it, but anyways heres the manage file that I use to redirect django utilities:

manage.sh

#!/bin/bash

python ./project/configs/${DEPLOYMENT_TARGET:="common"}/manage.py $*

and just in case this is how I have set up a django project:

project
|-manage.sh -> this fellow is redirected to settings.py (production, common or staging)
|-requirements.txt
|-README
|-dashbard.py
|-project.sqlite
|- project/
    |- apps
        |- accounts
        |-other internal apps
    |- configs 
        |- common -> for local development
            |-settings.py
            |-manage.py
            |-urls
        |-staging
            |-manage.py
            |-settings.py
            |-wsgi.py
            |-logging.conf
        |-production
            |-manage.py
            |-settings.py
            |-wsgi.py
            |-logging.conf
    |-media
    |-templates

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2025-01-09 18:12:04

我将 wsgi.py 更新为如下所示:

import os
import sys
import site

site.addsitedir(os.path.join('/home/ubuntu/ve','project/lib/python2.6/site-packages'))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../../'))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.configs.staging.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

我的 uwsgi.conf 文件现在如下所示:

# file: /etc/init/uwsgi.conf
description "uWSGI starter"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

# home - is the path to our virtualenv directory
# pythonpath - the path to our django application
# module - the wsgi handler python script

exec /home/ubuntu/ve/project/bin/uwsgi \
--uid ubuntu \
--pythonpath /home/ubuntu/django-projects/project/project/configs/staging \
-H /home/ubuntu/ve/project \
--socket /tmp/uwsgi.sock \
--chmod-socket 644 \
--module wsgi \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /home/ubuntu/logs/project/uwsgi.log

我的 nginx 站点可用文件如下所示:

# file: /etc/nginx/sites-available/yourdomain.com
# nginx configuration for project.maumercado.com

server {
        listen 80;
        charset utf-8;
        server_name project.maumercado.com;
        access_log /home/ubuntu/logs/project/nginx/access.log;
        error_log /home/ubuntu/logs/project/nginx/error.log;

        location ^~ /cache/ {
                root /home/ubuntu/django-projects/project/project/media;
                expires max;
        }

        location / {
                uwsgi_pass unix:/tmp/uwsgi.sock;
                include /etc/nginx/uwsgi_params;
        }
}

现在它工作完美,由于使用了奇怪的字符,我在样式方面遇到了一些问题就像 css 文件中的 ñ 一样。

现在我想知道当我需要在同一台服务器上使用uwsgi运行更多项目时该怎么办?

I updated wsgi.py to look like this:

import os
import sys
import site

site.addsitedir(os.path.join('/home/ubuntu/ve','project/lib/python2.6/site-packages'))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../../'))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.configs.staging.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

My uwsgi.conf file now looks like this:

# file: /etc/init/uwsgi.conf
description "uWSGI starter"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

# home - is the path to our virtualenv directory
# pythonpath - the path to our django application
# module - the wsgi handler python script

exec /home/ubuntu/ve/project/bin/uwsgi \
--uid ubuntu \
--pythonpath /home/ubuntu/django-projects/project/project/configs/staging \
-H /home/ubuntu/ve/project \
--socket /tmp/uwsgi.sock \
--chmod-socket 644 \
--module wsgi \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /home/ubuntu/logs/project/uwsgi.log

And my nginx site-available file looks like this:

# file: /etc/nginx/sites-available/yourdomain.com
# nginx configuration for project.maumercado.com

server {
        listen 80;
        charset utf-8;
        server_name project.maumercado.com;
        access_log /home/ubuntu/logs/project/nginx/access.log;
        error_log /home/ubuntu/logs/project/nginx/error.log;

        location ^~ /cache/ {
                root /home/ubuntu/django-projects/project/project/media;
                expires max;
        }

        location / {
                uwsgi_pass unix:/tmp/uwsgi.sock;
                include /etc/nginx/uwsgi_params;
        }
}

And its working perfect now, I had some problems with the styles because of strange characters being used like ñ in the css files.

Now I would like to know what should I do when I need to run more projects in the same server with uwsgi?

高冷爸爸 2025-01-09 18:12:04

确保将包含 wsgi.py 文件的目录添加到 pythonpath(您可以指定无限系列的 pythonpath 指令)

be sure to add the directory containing the wsgi.py file to the pythonpath (you can specify an unlimited series of pythonpath directives)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文