Django1.11 + Python3.6 + Nginx 1.12 + uWSGI 2.0 развертывание с ошибкой ImportError: Нет модуля с именем XXX ... невозможно загрузить приложение 0 (точка монтирования = '') - PullRequest
0 голосов
/ 21 мая 2018

Моя среда развертывания - "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:

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" inмой код, он выдает ту же ошибку, например:

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.отображается журнал:

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 голосов
/ 21 мая 2018

Вы можете сначала использовать командную строку uwsgi, чтобы запустить приложение от имени пользователя root

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

, затем, если все в порядке, отладить режим файла конфигурации

...