本文介绍了uWSGI将无法在混合Python环境中运行,以便与nginx一起正确运行并运行Django应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:在Windows 10主机上的Vagrant/Virtualbox中运行的Ubuntu 16.04(系统Python为2.7.12)

Environment: Ubuntu 16.04 (with system Python at 2.7.12) running in Vagrant/Virtualbox on Windows 10 host

Python设置:通过未激活virtualenv的python -V验证了系统python.还安装了Python 3.5,并且已经完成pipenv --three来为此项目创建virtualenv.在激活的virtualenv(pipenv shell激活)中执行python -V显示Python 3.5.2.

Python Setup: System python verified by doing python -V with no virtualenv's activated. Python 3.5 is also installed, and I've done pipenv --three to create the virtualenv for this project. Doing python -V within the activated virtualenv (pipenv shell to activate) shows Python 3.5.2.

其他背景:我正在开发Wagtail 2应用. Wagtail 2需要Django 2,而它当然需要Python3.我在这台机器上还有其他Django应用程序,这些应用程序是Django 1.11/Python 2.7开发的,由Apache提供.我们将继续使用Django 2/Python 3进行开发,并正在使用nginx/uWSGI来提供应用程序.

Additional Background: I'm developing a Wagtail 2 app. Wagtail 2 requires Django 2 which, of course, requires Python 3. I have other Django apps on this machine that were developed in Django 1.11/Python 2.7 and are served by Apache. We are moving to Django 2/Python 3 for development going forward and are moving to nginx/uWSGI for serving the apps.

我经历了许多教程/许多迭代.所有的Vagrant端口映射都可以通过nginx提供媒体/静态文件并在unix套接字上向Django应用传递请求的上游来很好地设置,但这会导致502 Gateway not found错误,因为uWSGI无法正确运行.因此,现在我只是从命令行运行以下命令,以尝试运行uWSGI:uwsgi --ini /etc/uwsgi/sites/my_site.com.ini.该文件包含:

I have gone through many tutorials/many iterations. All Vagrant port mapping is set up fine with nginx serving media/static files and passing requests upstream to the Django app on a unix socket, but this is giving a 502 Gateway not found error because uWSGI will not run correctly. Therefore, right now I'm simply running the following from the command line to try to get uWSGI to run: uwsgi --ini /etc/uwsgi/sites/my_site.com.ini. This file contains:

[uwsgi]

uid = www-data
gid = www-data

plugin = python35

# Django-related settings
# the base directory (full path)
chdir=/var/sites/my_site
# Django's wsgi file
wsgi-file = my_site.wsgi
# the virtualenv (full path)
virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID

# process-related settings
# master
master = True
# maximum number of worker processes
processes = 10

# the socket (use the full path to be safe)
socket = /tmp/my_site.sock

# clear environment on exit
vacuum = True

我尝试通过以下方式安装uWSGI:

I've tried installing uWSGI in the following ways:

  • 系统范围内的pip install uwsgipip3 install uwsgi
  • 使用apt-get install uwsgi uwsgi-plugin-python3
  • system-wide with pip install uwsgi as well as pip3 install uwsgi
  • using apt-get install uwsgi uwsgi-plugin-python3

我已经通过卸载以前的uwsgi安装来确保一次仅进行一次安装.后一种安装方法将uwsgi-core放置在usr/bin中,还将uusrgi,uwsgi_python3和uwsgi_python35的快捷方式放置在usr/bin中.

I've ensured that only one install is in place at a time by uninstalling any previous uwsgi installs. The latter install method places uwsgi-core in usr/bin and also places in usr/bin shortcuts to uwsgi, uwsgi_python3, and uwsgi_python35.

在.ini文件中,我也尝试过plugin = python3.我也从命令行尝试过:

In the .ini file I've also tried plugin = python3. I've also tried from the command line:

  • uwsgi_python3 --ini /etc/uwsgi/sites/my_site.com.ini
  • uwsgi_python35 --ini /etc/uwsgi/sites/my_site.com.ini
  • uwsgi_python3 --ini /etc/uwsgi/sites/my_site.com.ini
  • uwsgi_python35 --ini /etc/uwsgi/sites/my_site.com.ini

我已经尝试在激活的虚拟环境中以及在禁用virtualenv的情况下执行uwsgi ... .ini命令.三个命令行uwsgi ... .ini执行(uwsgi ...uwsgi_python3 ...uwsgi_python35 ...)中的每一个都会导致执行.ini文件,但是每次遇到以下错误时(以下语句):

I've tried executing the uwsgi ... .ini commands from both within the activated virtual environment and with the virtualenv deactivated. Each of the three command line uwsgi ... .ini executions (uwsgi ..., uwsgi_python3 ... and uwsgi_python35 ...) DO cause the .ini file to be executed, but each time I'm getting the following error (the last two lines of the following statements):

[uwsgi] implicit plugin requested python35
[uWSGI] getting INI configuration from /etc/uwsgi/sites/my_site.com.ini
*** Starting uWSGI 2.0.12-debian (64bit) on [Wed Mar  7 03:54:44 2018] ***
compiled with version: 5.4.0 20160609 on 31 August 2017 21:02:04
os: Linux-4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018
nodename: vagrant-ubuntu-trusty-64
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /vagrant/my_site
detected binary path: /usr/bin/uwsgi-core
setgid() to 33
setuid() to 33
chdir() to /var/sites/my_site
your processes number limit is 7743
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/my_site.sock fd 3
Python version: 3.5.2 (default, Nov 23 2017, 16:37:01)  [GCC 5.4.0 20160609]
Set PythonHome to /root/.local/share/virtualenvs/my_site-gmmiTMID
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

如果我进入已激活的virtualenv内的Python命令行并执行import encodings,它将很好地导入(无消息-仅返回命令行).搜索此特定错误没有发现任何用处.知道为什么ImportError:没有出现名为"encodings"的模块吗?

If I go into the Python command line within the activated virtualenv and do import encodings, it imports fine (no message - just comes back to command line). A search for this particular error has turned up nothing of use. Any idea why the ImportError: No module named 'encodings' is coming up?

更新-问题仍然发生

我正在使用pipenv,它将单个virtualenvs存储在/home/username/.local/share/virtualenvs文件夹中.尽管我可以通过以无业游民的用户身份执行uWSGI配置文件来从命令行启动uWSGI(请参见下面的注释),但是我仍然无法使用uWSGI配置文件中的/home/vagrant/.local/share/virtualenvs/my_venv启动该服务.我尝试将无业游民的用户添加到www-data组,并将www-data航海家添加到无业游民组.我还在整个路径(包括单个venv)上都设置了对世界的读取和执行许可,但是uWSGI服务仍将无法启动.

I'm using pipenv, and it stores individual virtualenvs in the /home/username/.local/share/virtualenvs folder. Though I was able to start uWSGI from the command line by executing the uWSGI config file as the vagrant user (see comment below), I have still not been able start the service with /home/vagrant/.local/share/virtualenvs/my_venv in the uWSGI config file. I tried adding the vagrant user to the www-data group and adding the www-data user to the vagrant group. I also put both read and execute permission for the world on the whole path (including the individual venv), but the uWSGI service will still not start.

一种自私的解决方法

通过将venv复制到/opt/virtualenvs/my_venv,我终于获得了uWSGI服务的启动.这样便可以使用sudo service uwsgi start启动该服务.整个路径的所有权是root:root.

I did finally get the uWSGI service to start by copying the venv to /opt/virtualenvs/my_venv. I was then able to start the service with sudo service uwsgi start. Ownership of that whole path is root:root.

仍在寻找解决方案...

此解决方案不是最佳解决方案,因为我现在正在从virtualenv执行,由于默认位置不是pipenv的默认位置,因此必须在更新默认virtualenv时进行更新,因此我仍在寻找答案.也许这是Ubuntu权限错误,但我只是找不到问题.

This solution is not optimal since I am now executing from a virtualenv that will have to be updated when the default virtualenv is updated since this location is not the default for pipenv, so I'm still looking for answers. Perhaps it is a Ubuntu permissions error, but I just can't find the problem.

推荐答案

这可能是您的虚拟环境存在的问题.尝试以下

It might be the problem with your virtual environment. Try the following

rm -rf /root/.local/share/virtualenvs/my_site-gmmiTMID
virtualenv -p python3 /root/.local/share/virtualenvs/my_site-gmmiTMID
source /root/.local/share/virtualenvs/my_site-gmmiTMID/bin/activate
pip install -r requirements.txt

并在uwsgi conf中尝试更改

and in uwsgi conf try to change

virtualenv=/root/.local/share/virtualenvs/my_site-gmmiTMID

home=/root/.local/share/virtualenvs/my_site-gmmiTMID

参考: http://uwsgi-docs.readthedocs.io/en/latest/Options.html#virtualenv

这篇关于uWSGI将无法在混合Python环境中运行,以便与nginx一起正确运行并运行Django应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:20