简体   繁体   中英

How to get uwsgi working with nginx and Django 1.7?

This is what I tried:

I have a django.conf inside /etc/nginx/sites-available/

upstream django {
    server unix:///tmp/uwsgi.sock;
}

server {
    listen              80;
    server_name         weasyprint.django.dev;
    charset             utf-8;
    error_log           /var/log/nginx/django-weasyprint.log;

    location / {
        uwsgi_pass      django;
        include         /etc/nginx/uwsgi_params;
    }
}

And then I do a sudo ln -s /etc/nginx/sites-available/django.conf inside sites-enabled

And then I restarted nginx.

I created a django folder called weasyprint_site inside /var/virtual/WebApps/virtualenvs/WeasyPrintProject

I used virtualenv on the same folder, so now my structure is like this:

WeasyPrintProject
     |---------bin
     |---------include
     |---------lib
     |---------local
     |---------share
     |---------weasyprint_site
                      |------------db.sqlite3
                      |------------manage.py
                      |------------test.py
                      |------------uwsgi.ini
                      |------------weasyprint_site

and then I also put in a uwsgi.ini as you can see above.

Its contents are:

[uwsgi]

socket=/tmp/uwsgi.sock
chmod-socket=666
uid = www-data
gid = www-data

chdir=/var/virtual/WebApps/virtualenvs/WeasyPrintProject
module=weasy_print.wsgi:application
master=true
pidfile=/var/virtual/WebApps/virtualenvs/WeasyPrintProject/weasy_print.pid
vacuum=true

When I am at /var/virtual/WebApps/virtualenvs/WeasyPrintProject/weasyprint_site ,

I run

uwsgi --ini uwsgi.ini

I get the following:

[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Thu Feb 19 11:59:12 2015] ***
compiled with version: 4.8.2 on 16 February 2015 05:39:16
os: Linux-3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014
nodename: vagrant-ubuntu-trusty-64
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /var/virtual/WebApps/virtualenvs/WeasyPrintProject/weasyprint_site
writing pidfile to /var/virtual/WebApps/virtualenvs/WeasyPrintProject/weasy_print.pid
detected binary path: /var/virtual/WebApps/virtualenvs/WeasyPrintProject/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /var/virtual/WebApps/virtualenvs/WeasyPrintProject
your processes number limit is 15934
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)
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1915160
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
ImportError: No module named weasy_print.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 ***
spawned uWSGI master process (pid: 1913)
spawned uWSGI worker 1 (pid: 1914, cores: 1)

I have 2 issues:

  1. I cannot get back to bash unless I do a Ctrl+C
  2. there is an import error saying no module named weasy_print.wsgi

I followed the instructions in here

My virtualenv for this particular project is to use python 2.7.4 and I am attempting to run django 1.7. The environment is Ubuntu 14.04

How do I get the django app to work.

Firstly, you're chdiring into WeasyPrintProject, but your project is in WeasyPrintProject/weasyprint_site. You should chdir into that directory inside your uwsgi.ini file.

Secondly, your project name is weasyprint_site, not weasy_print, so you should call module weasyprint_site.wsgi:application instead.

And last problem is: you should specify path to your virtualenv inside uwsgi.ini, so uwsgi process can now where are additional packages required by your application.

To return to your console without interrupting your uwsgi server, you must put it in background, by forking it, daemonizing it or simply launching from init script. I personally recomment using emperor/vassals system built in into wsgi.

Also having your project inside virtualenv directory is not recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM