uwsgi部署相关问题Fatal Python error: Py_Initialize: Unable to get the locale encoding 问题(13: Permission denied) while connecting to upstream

想利用uid参数启动uwsgi,报错
(flask) [root@localhost flask]# uwsgi uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.17.1 (64bit) on [Sun Oct 21 03:50:21 2018] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 20 October 2018 12:54:58
os: Linux-3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018
nodename: localhost.localdomain
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /var/flask
detected binary path: /root/.local/share/virtualenvs/flask-VGoRTHto/bin/uwsgi
setgid() to 995
setuid() to 997
chdir() to /var/flask/
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3802
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 /var/flask/flask.sock fd 3
Python version: 3.4.9 (default, Aug 14 2018, 21:28:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
!!! Python Home is not a directory: /root/.local/share/virtualenvs/flask-VGoRTHto/ !!!
Set PythonHome to /root/.local/share/virtualenvs/flask-VGoRTHto/
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x00007f03d1f7e840 (most recent call first):
已放弃
uwsgi配置文件
   [uwsgi] # Http access port. # If
this option comes into effect, we can visit our web site on http://[our IP]:[Port] # http=:5001 # Uwsgi's ip and port when it is loaded by Nginx socket=/var/flask/flask.sock chmod-socket=666 uid = nginx gid = nginx # Point to the main directory of the Web Site chdir=/var/flask/ # Setting up a virtual environment virtualenv=/root/.local/share/virtualenvs/flask-VGoRTHto/ # Python startup file wsgi-file=app.py # The application variable of Python Flask Core Oject callable=app # The maximum numbers of Processes processes=1 # The maximum numbers of Threads threads=2

猜想原因:

ini文件中切换用户nginx后,因为虚拟环境目录权限问题,python虚拟环境变成了系统默认环境,缺少很多库。

解决方法:

登录其他用户xxxxx后,pipenv新建python3环境,安装好库。

cd /var/flask/
pipenv --python 3.4 pipenv shell pipenv install uwsgi msyql-connector-python flask

修改uwsgi.ini文件:

uid = eledim
gid = eledim
virtualenv=/home/eledim/.local/share/virtualenvs/flask-VGoRTHto/ #新的虚拟环境
$ uwsgi uwsgi.ini

其他相关:

虚拟环境下用root启动uwsgi(不推荐的方法):

(flask) [root@localhost flask]#
uwsgi -s /var/flask/flask.sock -w app:app 
chmod 777 /var/flask/flask.sock

其他问题:

另外切换用户启动,可能导致没有旧sock文件的权限,导致socket绑定失败。应删除旧socket。

为了避免uwsgi启动权限问题,sock文件不应该在root目录,其他网站相关文件同理!

为了避免nginx中出现权限问题 (13: Permission denied) while connecting to upstream

nginx进程不推荐为root拥有,应该设置uwsgi.ini

chmod-socket=666

另外可能原因:

setsebool -P httpd_can_network_connect 1
setenforce 0

参考https://blog.csdn.net/kriszhang/article/details/68941793

原文地址:https://www.cnblogs.com/eledim/p/9824552.html