为什么 django 项目部署失败? - django + nginx + uwsgi

为什么 django 项目部署失败? - django + nginx + uwsgi

我正在尝试通过 Nginx 和 uWSGI 部署一个 Django 项目。如果我手动运行该项目,python manage.py runserver :8000它可以正常工作,所以我猜问题不在于项目本身。

我在专用的虚拟环境中安装了 Django 和依赖项,并将项目放入其中。我还在虚拟环境之外安装了 nginx 和 uwsgi,并开始创建配置文件:

/etc/nginx/sites-available/myproject(在启用站点的符号链接):

upstream uwsgi_myproject {
    server 127.0.0.1:5678;
}

server {
    listen 80;
    server_name my.url.net;
    set $home /path/to/myvirtualenv;
    access_log /path/to/myvirtualenv/log/access_uwsgi.log;
    error_log /path/to/myvirtualenv/log/error_uwsgi.log;

    client_max_body_size 10m;
    keepalive_timeout 120;

    location / {
        uwsgi_pass uwsgi_myproject;
        include uwsgi_params;
        gzip on;
        uwsgi_param UWSGI_CHDIR $home/path/to/myproject;
        uwsgi_param UWSGI_SCRIPT uwsgi;
        uwsgi_param UWSGI_PYHOME $home;
        root $home;
    }
    location /static/  {
        alias /path/to/myvirtualenv/path/to/myproject/static/;
        expires max;
        autoindex off;
    }
    location /media_adm/  {
        alias /path/to/myvirtualenv/lib/python2.7/site-packages/grappelli/static/;
        autoindex off;
    }
}

/etc/init/uwsgi_myproject.conf

description "uWSGI starter for myproject"

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

respawn

exec /path/to/myvirtualenv/bin/uwsgi \
--uid venvowner \
--home /path/to/myvirtualenv \
--pythonpath /path/to/myvirtualenv/path/to/myproject/ \
--socket 127.0.0.1:5678 \
--chmod-socket \
--module wsgi \
-b 8192 \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /path/to/myvirtualenv/log/uwsgi.log

/path/to/myvirtualenv/path/to/myproject/wsgi.py

import os, sys, site

site.addsitedir('/path/to/myvirtualenv/lib/python2.7/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

然后我重启机器并尝试连接到 my.url.net,但是没有响应(当我在导入 myproject 之前安装了 nginx 时,它响应“欢迎使用 Nginx!”)。我注意到lsof | grep LISTEN上游套接字尚未创建。日志没有输出任何相关信息,因此我尝试使用相同的参数手动执行 uwsgiuwsgi_myproject.conf并得到以下输出:

current working directory: /etc/init
detected binary path: /path/to/myvirtualenv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 6674
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
The -s/--socket option is missing and stdin is not a socket.

编辑:所以我尝试的最后一件事是重新排序参数。现在 --socket 参数已成功处理,但我仍然在输出行中收到此消息:

*** no app loaded. going in full dynamic mode ***

我一直在阅读的一些文件:

关于我遗漏或忽略了什么,您有什么想法吗?

继续:我再次查看了文档并尝试了不同的方法:使用 .ini 文件并手动运行它以查看会发生什么:

虽然有点丑陋,但是可以这么说:

[uwsgi]
socket = 127.0.0.1:5678
uid = venvowner
chdir = /path/to/myvirtualenv
home = /path/to/myvirtualenv
virtualenv = /path/to/myvirtualenv
pythonpath = /path/to/myvirtualenv/path/to/myproject
module = wsgi
master = true
optimize = 2
processes = 2
logto = /path/to/myvirtualenv/log/uwsgi.log

我获得了一些有用的日志:

*** Python threads support is disabled. You can enable it with --enable-threads ***
*** Operational MODE: preforking ***
ImportError: No module named wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***

工人已经生成,但堆栈显然失败了。我会改天再来处理这个问题。

答案1

最后,我让它工作了。感谢@DhirajThakur 的评论,它帮助我找到正确的方向。

/etc/init/uwsgi_myproject.conf

description "uWSGI starter for myproject"

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

respawn

exec /usr/local/bin/uwsgi /etc/init/myproject.ini

/etc/init/myproject.ini

[uwsgi]
master = true
socket = 127.0.0.1:5678
uid = venvowner
chdir = /path/to/myvirtualenv
home = /path/to/myvirtualenv
virtualenv = /path/to/myvirtualenv
pythonpath = /path/to/myvirtualenv/path/to/myproject
wsgi-file = /path/to/myvirtualenv/path/to/myproject/wsgi.py
optimize = 2
processes = 2
logto = /path/to/myvirtualenv/log/uwsgi.log

不知为何,我感觉我解决问题的方式有点太难了。

相关内容