通过uwsgi+nginx启动flask的python web程序
通過uwsgi+nginx啟動flask的python web程序
一般我們啟動python web程序的時候都是通過python直接啟動主文件,測試的時候是可以的,當訪問量大的時候就會出問題
python manage.py
通過wsgi web服務器網關接口規范啟動是一種比較好的方式:
web服務器 nginx + uwsgi + flask
原理就是nginx通過代理訪問通過uwsgi啟動監聽在本機的flask程序
1.安裝uwsgi模塊
# pip install uwsgi
2.通過uwsgi啟動flask項目
# 通過http方式啟動(推薦)
# uwsgi --http 127.0.0.1:9999 -w user:app
# 通過socket方式啟動
uwsgi -s 127.0.0.1:9999 -w user:app
# 主入口需要__init__.py文件
user/__init__.py
訪問方式是:
http://192.168.3.91:8080 --> uwsgi:9999
?
centos6.8_x64安裝python3.7.2和wusgiwget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xztar -xf Python-3.7.2.tar.xz cd Python-3.7.2 mkdir /usr/local/python372 ./configure --prefix=/usr/local/python372 make make install# 報錯from _ctypes import Union, Structure, Array ModuleNotFoundError: No module named '_ctypes'# 解決辦法:yum install libffi-devel libffi# 再次編譯安裝即可 ./configure --with-ssl --prefix=/usr/local/python372 make make install echo $?# 刪除原來的軟鏈,重新建立軟連接 # rm -f /usr/local/python3 cd /usr/local ln -s python372 python3[root@srv3:/usr/local/python3]# python3 -V Python 3.7.2# 安裝ipython
# pip3 install ipython
# ln -s /usr/local/python372/bin/ipython3 /usr/sbin/ipython3
[root@srv3:/usr/local/uwsgi]# tar zxf uwsgi-latest.tar.gz [root@srv3:/usr/local/uwsgi]# cd uwsgi- uwsgi-2.0.18/ uwsgi-latest.tar.gz [root@srv3:/usr/local/uwsgi]# cd uwsgi-2.0.18/ [root@srv3:/usr/local/uwsgi/uwsgi-2.0.18]# python3 uwsgiconfig.py --build################# uWSGI configuration #################kernel = Linux execinfo = False ifaddrs = True locking = pthread_mutex event = epoll timer = timerfd filemonitor = inotify pcre = True routing = True capabilities = False yaml = embedded json = False ssl = True xml = libxml2 debug = False plugin_dir = . zlib = True ucontext = True malloc = libc############## end of uWSGI configuration ############# total build time: 3 seconds *** uWSGI is ready, launch it with ./uwsgi ***# 繼續install # python3 setup.py install[root@srv3:~]# whereis uwsgi uwsgi: /usr/bin/uwsgi /etc/uwsgi /usr/local/bin/uwsgi /usr/local/uwsgi /opt/uwsgi-2.0.13.1/bin/uwsgi [root@srv3:~]# uwsgi --version 2.0.18# 編寫測試文件 [root@srv3:/usr/local/uwsgi/uwsgi-2.0.18]# cat testuwsgi.py def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"]# 啟動 [root@srv3:/usr/local/uwsgi/uwsgi-2.0.18]# ./uwsgi --http :9000 --wsgi-file testuwsgi.py *** Starting uWSGI 2.0.18 (64bit) on [Wed Jun 19 02:39:19 2019] *** compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-23) on 19 June 2019 07:37:03 os: Linux-2.6.32-642.6.2.el6.x86_64 #1 SMP Wed Oct 26 06:52:09 UTC 2016 nodename: srv3.keepvid.com machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 24 current working directory: /usr/local/uwsgi/uwsgi-2.0.18 detected binary path: /usr/local/uwsgi/uwsgi-2.0.18/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 128183 your memory page size is 4096 bytes detected max file descriptor number: 65536 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI http bound on :9000 fd 4 spawned uWSGI http 1 (pid: 927) uwsgi socket 0 bound to TCP address 127.0.0.1:25232 (port auto-assigned) fd 3 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** Python version: 3.7.2 (default, Jun 19 2019, 02:23:57) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x1e8dcd0 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72920 bytes (71 KB) for 1 cores *** Operational MODE: single process *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1e8dcd0 pid: 926 (default app) uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 926, cores: 1) [pid: 926|app: 0|req: 1/1] 127.0.0.1 () {28 vars in 298 bytes} [Wed Jun 19 02:39:58 2019] GET / => generated 11 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1 switches on core 0)# 驗證 [root@srv3:~]# curl http://127.0.0.1:9000/ Hello World
?
./configure --with-ssl --prefix=/usr/local/python372
編譯的時候一定要添加 --with-ssl 否則pip3 install -r 安裝模塊時會報錯:
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail
轉載于:https://www.cnblogs.com/reblue520/p/7884943.html
總結
以上是生活随笔為你收集整理的通过uwsgi+nginx启动flask的python web程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hibernate事务增删改查(第一部分
- 下一篇: 保护个人信息需出“重典”