迁移到 nginx/uWSGI/django 设置未找到

发布于 2024-12-18 07:22:06 字数 4172 浏览 2 评论 0原文

Apache / Mod_wsgi 堆栈一直存在问题,最终决定减少损失并在服务器端重新开始。

已经将 Nginx 设置为代理对 uwsgi 的请求。使用 uwsgi 协议相当简单。

然而 uwsgi 一直很顽固,尝试阅读文档/教程/在 IRC 中询问... 由于 Django 未找到设置,不断出现 wsgi 模块的相同错误, 然而,当我通过控制台运行相同的代码时,我没有收到任何错误。

下面粘贴了所有相关脚本 - 假设我遗漏了一些明显的东西,尽管我不知道是什么。

在 Ubuntu 11.10 上运行,使用 Upstart 脚本启动

Upstart 脚本

description "uWSGI starter"
start on (local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
exec /usr/local/sbin/uwsgi \
--uid www-data \
--socket 127.0.0.1:5050 \
--master \
--logto /var/log/uwsgi_main.log \
--logdate \
--optimize 2 \
--processes 4 \
--harakiri 120 \
--vhost \
--no-site

nginx 服务器配置

server {
    listen   80;
    server_name .DOMAIN.com;
    access_log /home/USER/virtualenv/logs/nginx/access.log;
    error_log /home/USER/virtualenv/logs/nginx/error.log;
    client_max_body_size 10m;
    keepalive_timeout 120;

    location /media/ {
        root /home/USER/virtualenv/PROJECT;
    }
    location /static/ {
        root /home/USER/virtualenv/PROJECT;
    }

    location / {
        uwsgi_pass uwsgi_main;
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /home/USER/virtualenv;
        uwsgi_param UWSGI_SCRIPT deploy.deploy;
        uwsgi_param UWSGI_CHDIR /home/USER/virtualenv/PROJECT;
        root /home/USER/virtualenv;
    }
}

deploy.py

import os, site, sys

prev_sys_path = list(sys.path)
site.addsitedir('/home/USER/virtualenv/lib/python2.7/site-packages')

sys.path.append('/home/USER/virtualenv/PROJECT')
sys.path.append('/home/USER/virtualenv/')

new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
    sys.path.remove(item)
sys.path[:0] = new_sys_path

os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECT.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

错误

Fri Nov 25 22:24:25 2011 - WSGI application 0 (mountpoint=DOMAIN.com|) ready on interpreter 0x679920 pid: 9552
Traceback (most recent call last):
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named settings
DOMAIN.com [pid: 9552|app: 0|req: 1/1] 184.96.159.248 () {48 vars in 1083 bytes} [Fri Nov 25 22:24:25 2011] GET / => generated 0 bytes in 284 ms$
Fri Nov 25 22:24:26 2011 - WSGI application 0 (mountpoint=DOMAIN.com|) ready on interpreter 0x679920 pid: 9553
Traceback (most recent call last):
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))

Been having ongoing issues with an Apache / Mod_wsgi stack and finally decided to cut my losses and start fresh on the server side of things

Have gotten Nginx setup to proxy requests to uwsgi. With the uwsgi protocol was fairly easy.

However uwsgi has been plain stubborn, tried reading docs / tutorials / asking in IRC...
Keep getting the same error of wsgi module for Django not finding settings,
yet when I run the same code through a console I don't get any errors.

Pasted all relevant scripts below - assuming that I'm missing something obvious though I have no idea what.

Running on Ubuntu 11.10 w/ Upstart script to boot

Upstart Script

description "uWSGI starter"
start on (local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
exec /usr/local/sbin/uwsgi \
--uid www-data \
--socket 127.0.0.1:5050 \
--master \
--logto /var/log/uwsgi_main.log \
--logdate \
--optimize 2 \
--processes 4 \
--harakiri 120 \
--vhost \
--no-site

nginx server config

server {
    listen   80;
    server_name .DOMAIN.com;
    access_log /home/USER/virtualenv/logs/nginx/access.log;
    error_log /home/USER/virtualenv/logs/nginx/error.log;
    client_max_body_size 10m;
    keepalive_timeout 120;

    location /media/ {
        root /home/USER/virtualenv/PROJECT;
    }
    location /static/ {
        root /home/USER/virtualenv/PROJECT;
    }

    location / {
        uwsgi_pass uwsgi_main;
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /home/USER/virtualenv;
        uwsgi_param UWSGI_SCRIPT deploy.deploy;
        uwsgi_param UWSGI_CHDIR /home/USER/virtualenv/PROJECT;
        root /home/USER/virtualenv;
    }
}

deploy.py

import os, site, sys

prev_sys_path = list(sys.path)
site.addsitedir('/home/USER/virtualenv/lib/python2.7/site-packages')

sys.path.append('/home/USER/virtualenv/PROJECT')
sys.path.append('/home/USER/virtualenv/')

new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
    sys.path.remove(item)
sys.path[:0] = new_sys_path

os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECT.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Error

Fri Nov 25 22:24:25 2011 - WSGI application 0 (mountpoint=DOMAIN.com|) ready on interpreter 0x679920 pid: 9552
Traceback (most recent call last):
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named settings
DOMAIN.com [pid: 9552|app: 0|req: 1/1] 184.96.159.248 () {48 vars in 1083 bytes} [Fri Nov 25 22:24:25 2011] GET / => generated 0 bytes in 284 ms$
Fri Nov 25 22:24:26 2011 - WSGI application 0 (mountpoint=DOMAIN.com|) ready on interpreter 0x679920 pid: 9553
Traceback (most recent call last):
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/USER/virtualenv/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))

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

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

发布评论

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

评论(2

回眸一笑 2024-12-25 07:22:06

首先,你不需要所有这些 sys.path 混乱,你的 nginx 变量已经为你设置了 virtualenv。

我建议您进入静态模式,因为动态虚拟主机模式非常复杂并且通常难以调试。只需更改您的命令行并从 nginx 中删除三个 uwsgi_param 条目:

/usr/local/sbin/uwsgi
--uid www-data
--socket 127.0.0.1:5050
--master
--logto /var/log/uwsgi_main.log
--logdate
--optimize 2
--processes 4
--harakiri 120
--virtualenv /home/USER/virtualenv/
--pythonpath /home/USER/virtualenv/
--chdir /home/USER/virtualenv/PROJECT
--env DJANGO_SETTINGS_MODULE=PROJECT.settings
--module "django.core.handlers.wsgi:WSGIHandler()"

应该就足够了(显然您可以扔掉 wsgi 脚本)

First of all you do not need all of those sys.path mess, your nginx vars already set the virtualenv for you.

I suggest you to go in static mode, as dynamic virtualhosting mode is very complex and often hard to debug. Simply change your command line and remove the three uwsgi_param entry from nginx:

/usr/local/sbin/uwsgi
--uid www-data
--socket 127.0.0.1:5050
--master
--logto /var/log/uwsgi_main.log
--logdate
--optimize 2
--processes 4
--harakiri 120
--virtualenv /home/USER/virtualenv/
--pythonpath /home/USER/virtualenv/
--chdir /home/USER/virtualenv/PROJECT
--env DJANGO_SETTINGS_MODULE=PROJECT.settings
--module "django.core.handlers.wsgi:WSGIHandler()"

should be enough (and obviously you can throw away the wsgi script)

梦在深巷 2024-12-25 07:22:06

我的猜测是你在 python virtualenv 中运行 django 应用程序。如果是这样,您还需要在 upstart 脚本中激活 virtualenv:
执行 /path/to/virtualenv/activate && /usr/local/sbin/uwsgi

My guess is you run the django app in python virtualenv. If so, you need to activate virtualenv in upstart script as well:
exect /path/to/virtualenv/activate && /usr/local/sbin/uwsgi

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