我在使用阿里雲centos7.4布置django網站的時候,因為自帶的是python2,而我需要的是python3。為了圖方便我安裝了anaconda來引入python3,但是造成了不少的環境混亂的問題,在啟動uwsgi的時候會報錯找不到python。
安裝uwsgi
# 在這里默認你的pip3已經添加到環境 pip install uwsgi
- 1
- 2
如果pip無法使用,可以在/etc/profile 文件最后添加Anaconda環境路徑
# Anaconda export PATH=$PATH:/root/anaconda3/bin
- 1
- 2
測試uwsgi是否安裝成功
隨便找個干凈的目錄下(我這里找的/home)新建一個py文件
cd /home vim test.py
- 1
- 2
在里面寫上
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3
- 1
- 2
- 3
- 4
然后在當前目錄下執行
uwsgi --http :8000 --wsgi-file test.py
- 1
這里的8000端口
是可以隨意寫的,也可以寫8001,8080都沒問題,但是要注意在阿里雲的安全組里面開放相應的端口號
,不然是無法訪問的~
在瀏覽器輸入# 你的ip:8000
如果能夠看到"Hello World"就成功啦,說明uwsgi沒問題
配置連接django的uwgi的.ini文件
進入你的django目錄
cd /path/to/your/django
- 1
創建uwsgi.ini文件,該文件是用來和django,nginx聯系的,創建該文件啟動就不需要再用命令行大量加后綴了
#聲明這是個uwsgi文件 [uwsgi] # 設置端口號socket,和nginx實現通訊,需要和nginx的配置相同,如果直接訪問需要設置http socket = 127.0.0.1:8000 # 你的django項目目錄 chdir = /home/mysite # 配置wsgi接口模塊文件路徑 wsgi-file= /home/mysite/.../wsgi.py # Django的uwsgi項目名 module = mysite.wsgi # 啟動管理主進程 master = true # 每個進程的線程數 threads = 2 # 啟動的進程數 processes = 4 # 配置存放主進程的進程號文件 pidfile = uwsgi.pid # 日志記錄 daemonize = uwsgi.log
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
當然這里面還有很多很多其他的配置,有需要的可以查看官方文檔
嘗試運行uwsgi.ini
uwsgi --ini uwsgi.ini
- 1
重點來了,運行報錯了!
查看uwsgi.log
錯誤日志
*** Starting uWSGI 2.0.18 (64bit) on [Mon Apr 6 13:32:46 2020] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 20 January 2020 05:57:29
os: Linux-3.10.0-1062.18.1.el7.x86_64 #1 SMP Tue Mar 17 23:49:17 UTC 2020
nodename: iz2iu53qtx793rz
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /data/ecust-job/Project
writing pidfile to /data/ecust-job/Project/project-master.pid
detected binary path: /root/anaconda3/bin/uwsgi
uWSGI running as root, you can use --uid/–gid/–chroot options
setgid() to 2000
setuid() to 1000
chdir() to /data/ecust-job/Project
your processes number limit is 65535
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 3
Python version: 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0]
Could not find platform independent libraries
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to [:<exec_prefix>]
Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings’
解決方案,添加虛擬環境
這個問題困擾了我好久,一直解決不了,查閱了很多資料都不成,一度懷疑是anaconda的環境在搞我心態
直到后來看了b站的教學視頻,才搞明白是要用虛擬環境啟動
,不能用本地環境…
pip install virtualenv cd / virtualenv Env cd /Env/bin
- 1
- 2
- 3
- 4
# 啟動虛擬環境 source activate # 關閉 deactivate
- 1
- 2
- 3
- 4
然后在uwsgi.ini下添加home
# 虛擬環境 home=/Env
- 1
- 2
運行成功
其他錯誤
比如:
bind(): Cannot assign requested address [core/socket.c line 769]
- 1
這是端口占用,將端口釋放即可
netstat -apn|grep 8000 kill -9 (對應的進程)
- 1
- 2
參考
[1]https://www.cnblogs.com/xinghuaikang/archive/2018/03/17/8576580.html
[2]https://www.jianshu.com/p/0e85cf58e677