想利用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