Django1.11 + Python3.6 + Nginx 1.12 + uWSGI 2.0部署错误ImportError:没有名为XXX的模块...无法加载app 0(mountpoint ='')

时间:2018-05-21 08:57:16

标签: django nginx virtualenv python-3.6 uwsgi

我的部署环境是“Django 1.11.13 + Python3.6.5(使用virtualenv)+ uWSGI 2.0 + Nginx 1.12”。这是我的项目:

(ncms) [ncms@localhost ncms]$ pwd
/home/ncms/ncms
(ncms) [ncms@localhost ncms]$ ll
总用量 36
drwxrwxr-x 12 ncms ncms   157 5月  17 13:48 apps
-rwxrwxr-x  1 ncms ncms 16384 5月  14 18:49 celerybeat-schedule
drwxrwxr-x  2 ncms ncms    66 5月  17 10:40 db_tools
drwxrwxr-x  4 ncms ncms    41 5月  17 10:40 extra_apps
drwxrwxr-x  5 ncms ncms   233 5月  17 10:40 libs
drwxrwxr-x  2 ncms ncms   152 5月  17 10:40 logfiles
-rwxrwxr-x  1 ncms ncms   855 5月   6 22:23 manage.py
drwxrwxr-x  3 ncms ncms   201 5月  21 14:19 ncms
-rwxrwxr-x  1 ncms ncms   351 5月  15 18:25 ncms.conf
-rwxrwxr-x  1 ncms ncms  2766 5月  17 13:43 notes.md
-rwxrwxr-x  1 ncms ncms   518 5月  14 15:52 requirements.txt
drwxrwxr-x  3 ncms ncms    23 5月  18 16:09 static
drwxrwxr-x 10 ncms ncms   120 5月  18 16:05 static_files
drwxrwxr-x 11 ncms ncms  4096 5月  17 15:38 templates

我的virtualenv路径和名称:

(ncms) [ncms@localhost ncms]$ pwd
/home/ncms/.virtualenvs/ncms
(ncms) [ncms@localhost ncms]$ ll
总用量 8
drwxrwxr-x 3 ncms ncms 4096 5月  21 13:46 bin
drwxrwxr-x 2 ncms ncms   24 5月  15 11:49 include
drwxrwxr-x 3 ncms ncms   23 5月  15 11:49 lib
-rw-rw-r-- 1 ncms ncms   61 5月  15 11:50 pip-selfcheck.json

您必须知道的三个重要文件:

  

1./etc/uwsgi/ncms.ini

[uwsgi]
# Django diretory that contains manage.py
chdir = /home/ncms/ncms
module = ncms.wsgi:application
env = DJANGO_SETTINGS_MODULE=ncms.settings
# enable master process manager
master = true
# bind to UNIX socket
socket = /run/uwsgi/ncms.sock
# number of worker processes
processes = 4
# user identifier of uWSGI processes
uid = ncms
# group identifier of uWSGI processes
gid = ncms
#respawn processes after serving 5000 requests
max-requests = 5000
# clear environment on exit
vacuum = true
# the virtualenv you are using (full path)
home = /home/ncms/.virtualenvs/ncms
# set mode and own of created UNIX socket
chown-socket = ncms:nginx
chmod-socket = 660
# place timestamps into log
log-date = true
logto = /var/log/uwsgi.log
no-site = true
  

2./etc/systemd/system/uwsgi.service

[Unit]
Description=ncms uWSGI service

[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown ncms:nginx /run/uwsgi'
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=graphical.target
  

3./etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    keepalive_timeout   65;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
    listen       [::]:80 default_server;
        server_name  _;
        location = favicon.ico { access_log off; log_not_found off; }
        location /static {
        root         /home/ncms/ncms;
        }
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/run/uwsgi/ncms.sock;
        }
    }
}

配置好后:

sudo nginx –t
sudo usermod -a -G ncms nginx
chmod 710 /home/ncms
sudo systemctl daemon-reload
sudo systemctl restart nginx
sudo systemctl restart uwsgi

然后,当我查看/var/log/uwsgi.log时,我总是在操作模式:preforking 步骤中遇到此错误:

Mon May 21 16:38:35 2018 - SIGINT/SIGQUIT received...killing workers...
Mon May 21 16:38:35 2018 - received message 0 from emperor
Mon May 21 16:38:36 2018 - worker 1 buried after 1 seconds
Mon May 21 16:38:36 2018 - worker 2 buried after 1 seconds
Mon May 21 16:38:36 2018 - worker 3 buried after 1 seconds
Mon May 21 16:38:36 2018 - worker 4 buried after 1 seconds
Mon May 21 16:38:36 2018 - goodbye to uWSGI.
Mon May 21 16:38:36 2018 - VACUUM: unix socket /run/uwsgi/ncms.sock removed.
Mon May 21 16:38:38 2018 - *** Starting uWSGI 2.0.17 (64bit) on [Mon May 21 16:38:38 2018] ***
Mon May 21 16:38:38 2018 - compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-16) on 26 April 2018 05:37:29
Mon May 21 16:38:38 2018 - os: Linux-3.10.0-693.21.1.el7.x86_64 #1 SMP Wed Mar 7 19:03:37 UTC 2018
Mon May 21 16:38:38 2018 - nodename: localhost.localdomain
Mon May 21 16:38:38 2018 - machine: x86_64
Mon May 21 16:38:38 2018 - clock source: unix
Mon May 21 16:38:38 2018 - pcre jit disabled
Mon May 21 16:38:38 2018 - detected number of CPU cores: 4
Mon May 21 16:38:38 2018 - current working directory: /etc/uwsgi
Mon May 21 16:38:38 2018 - detected binary path: /usr/bin/uwsgi
Mon May 21 16:38:38 2018 - chdir() to /home/ncms/ncms
Mon May 21 16:38:38 2018 - your processes number limit is 7164
Mon May 21 16:38:38 2018 - your memory page size is 4096 bytes
Mon May 21 16:38:38 2018 - detected max file descriptor number: 1024
Mon May 21 16:38:38 2018 - lock engine: pthread robust mutexes
Mon May 21 16:38:38 2018 - thunder lock: disabled (you can enable it with --thunder-lock)
Mon May 21 16:38:38 2018 - uwsgi socket 0 bound to UNIX address /run/uwsgi/ncms.sock fd 3
Mon May 21 16:38:38 2018 - setgid() to 2014
Mon May 21 16:38:38 2018 - setuid() to 2030
Mon May 21 16:38:38 2018 - Python version: 2.7.5 (default, Aug  4 2017, 00:39:18)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
Mon May 21 16:38:38 2018 - Set PythonHome to /home/ncms/.virtualenvs/ncms
Mon May 21 16:38:38 2018 - *** Python threads support is disabled. You can enable it with --enable-threads ***
Mon May 21 16:38:38 2018 - Python main interpreter initialized at 0x1cea860
Mon May 21 16:38:38 2018 - your server socket listen backlog is limited to 100 connections
Mon May 21 16:38:38 2018 - your mercy for graceful operations on workers is 60 seconds
Mon May 21 16:38:38 2018 - mapped 364600 bytes (356 KB) for 4 cores
Mon May 21 16:38:38 2018 - *** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./ncms/__init__.py", line 1, in <module>
    from __future__ import absolute_import, unicode_literals
ImportError: No module named __future__
Mon May 21 16:38:38 2018 - unable to load app 0 (mountpoint='') (callable not found or import error)
Mon May 21 16:38:38 2018 - *** no app loaded. going in full dynamic mode ***
Mon May 21 16:38:38 2018 - *** uWSGI is running in multiple interpreter mode ***
Mon May 21 16:38:38 2018 - spawned uWSGI master process (pid: 3456)
Mon May 21 16:38:38 2018 - spawned uWSGI worker 1 (pid: 3458, cores: 1)
Mon May 21 16:38:38 2018 - spawned uWSGI worker 2 (pid: 3459, cores: 1)
Mon May 21 16:38:38 2018 - spawned uWSGI worker 3 (pid: 3460, cores: 1)
Mon May 21 16:38:38 2018 - spawned uWSGI worker 4 (pid: 3462, cores: 1)

当我从代码中的 future import absolute_import,unicode_literals“中删除”行时,它会出现同样的错误,例如:

Mon May 21 16:43:30 2018 - *** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./ncms/__init__.py", line 4, in <module>
    from .celery import app as celery_app
  File "./ncms/celery.py", line 6, in <module>
    import os
ImportError: No module named os
Mon May 21 16:43:30 2018 - unable to load app 0 (mountpoint='') (callable not found or import error)
Mon May 21 16:43:30 2018 - *** no app loaded. going in full dynamic mode ***

看起来无法导入任何东西......

当我访问我的网站时,显示了/var/log/uwsgi.log:

Mon May 21 16:55:28 2018 - --- no python application found, check your startup logs for errors ---
[pid: 3812|app: -1|req: -1/3] 192.168.10.1 () {46 vars in 854 bytes} [Mon May 21 16:55:28 2018] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)
Mon May 21 16:55:28 2018 - --- no python application found, check your startup logs for errors ---
[pid: 3812|app: -1|req: -1/4] 192.168.10.1 () {48 vars in 855 bytes} [Mon May 21 16:55:28 2018] GET /favicon.ico => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)

我已经谷歌了很多,并尝试了很多时间在这里和那里改变,只是没有得到正确的答案。

有人可以帮助我吗?请!

1 个答案:

答案 0 :(得分:1)

您可以先使用uwsgi命令行以root身份启动应用

uwsgi --socket 127.0.0.1:8080 --chdir /home/ncms/ncms/ --wsgi-file ncms/wsgi.py

然后如果可以,调试配置文件模式