【问题标题】:Linode Django uwsgi NginxLinode Django uwsgi Nginx
【发布时间】:2017-04-12 20:34:59
【问题描述】:

以下设置只让我看到默认的 Nginx html 页面。我怎样才能到达 Django?

我一直在关注 Linode 关于如何设置的文档(以及许多其他教程),但他们不使用 systemd,所以情况有些不同。

https://www.linode.com/docs/websites/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04

我将 Linode 与 Fedora24 一起使用。我已经安装了我的virutalenv /home/ofey/djangoenv 并激活它, Django 是使用 pip 安装的 /home/ofey/qqiProject 进入我已经安装了 uwsgi 的 virtualenv。 第一, /etc/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStart=/home/ofey/djangoenv/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

这会执行,

/etc/uwsgi/sites/qqiProject.ini

[uwsgi]
project = qqiProject
base = /home/ofey

chdir = %(base)/%(project)
home = %(base)/djangoenv
module = %(project).wsgi:application

master = true
processes = 2

socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true

还有,

/etc/nginx/sites-available/qqiProject

server {
    listen 80;
    server_name qqiresources.com www.qqiresources.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/django/qqiProject;
    }

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/home/django/qqiProject/qqiProject.sock;
    }
}

/etc/nginx/nginx.conf 文件没有改变。

用户是ofey,我用过,

$ sudo systemctl daemon-reload
$ sudo systemctl restart nginx
$ sudo systemctl start uwsgi.service

用 Django 开始,

$ python manage.py runserver

在 Django 的 settings.py 中我关闭了调试并添加了一个主机

DEBUG = False

ALLOWED_HOSTS = ['qqiresources.com']

我还创建了一个符号链接,

sudo ln -s /etc/nginx/sites-available/qqiProject /etc/nginx/sites-enabled

任何帮助将不胜感激,

谢谢

【问题讨论】:

  • 在 qqiProject.ini 你有 '/home/ofey',但在 /etc/nginx/sites-available/qqiProject 它是 'home/django/'。也许这是你的错?
  • 感谢您发现该错误。我更正了,但不幸的是仍然没有运气。
  • 更改配置后是否重新启动所有内容?
  • 我通过首先获得一个基本的 hello world python 脚本与 uwsgi 一起工作来简化问题。然后我成功添加了 Django,一旦我弄清楚如何在 Django 中服务器静态文件和 CDN,我将添加 nginx。所以不幸的是,我还没有为这一切做好准备。但我会在我到达那里时发布。谢谢stackoverflow.com/questions/40063646/…

标签: django nginx uwsgi linode


【解决方案1】:

以下内容对我有用,但是我不确定这是否是正确或最有效的方法,尤其是在涉及套接字的情况下。

systemd 运行 uwsgi.service 并且可以启动,

$ sudo systemctl start uwsgi.service

有时需要重新加载systemd,

$ sudo systemctl daemon-reload

/etc/sysemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStart=/home/ofey/djangoenv/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

这会调用我的 django 虚拟环境目录中的二进制文件,

ExecStart=/home/ofey/djangoenv/bin/uwsgi

还带我们到 /etc/uwsgi/sites,其中 uwsgi 配置文件称为 djangoForum.ini

/etc/uwsgi/sites/djangoForum.ini

[uwsgi]
project = djangoForum
base = /home/ofey

chdir = %(base)/%(project)
home = %(base)/djangoenv
module = crudProject.wsgi:application

master = true
processes = 2

socket = 127.0.0.1:3031
chmod-socket = 664
vacuum = true

Django 在 /home/ofey/djangoForum,我的 django 项目在 /home/ofey/djangoForum/crudProject

/etc/nginx/nginx.conf

events {
     worker_connections 1024;
}

http{

 upstream django {
    # connect to this socket
    # server unix:///tmp/uwsgi.sock;    # for a file socket
    server 127.0.0.1:3031;      # for a web port socket
    }

server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name example.com;   # substitute your machine's IP address or FQDN
    charset     utf-8;

    #Max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
                alias /home/ofey/djangoForum/fileuploader/uploaded_files;      # your Django project's media files
    }

        location /static {
                alias /home/ofey/djangoForum/noAppBoundStaticDirectory;     # your Django project's static files
        }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        # uwsgi_pass 127.0.0.1:3031;
        include     /etc/nginx/uwsgi_params; # or the uwsgi_params you installed manually
        }
  }
}

nginx可以开启,

$ sudo systemctl start nginx.service

“start”可以替换为“restart”或“stop”。

uwsgi 和 nginx 的这些配置文件对我有用。

有用的链接:

https://gist.github.com/evildmp/3094281

https://www.linode.com/docs/websites/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04

【讨论】:

    猜你喜欢
    • 2014-05-31
    • 2011-10-10
    • 2011-11-25
    • 2015-11-08
    • 2014-08-09
    • 2019-03-03
    • 2014-12-17
    • 2017-08-07
    • 2016-09-21
    相关资源
    最近更新 更多