【保姆级】Python项目部署到Linux生产环境(uwsgi+python+flask+nginx服务 ...

立山  金牌会员 | 2024-7-21 14:17:15 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 580|帖子 580|积分 1740

1.安装python

我这里是3.9.5版本
安装依靠:
  1. yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y
复制代码
根据本身的需要下载对应的python版本:
  1. cd /usr/local
  2. wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
复制代码
 解压 > 编译 > 安装
  1. tar -xvf Python-3.9.5.tgz
  2. cd /usr/local/Python-3.9.5
  3. ./configure
  4. make all
  5. make install
复制代码
Python解释器配置清华源:
  1. pip3.9 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
复制代码
2.假造环境配置

1)安装 virtualenv

  1. pip3.9 install virtualenv
复制代码
2)创建假造环境(一样寻常是一个项目一个假造环境)

  1. mkdir home/python_item/envs
  2. cd /home/python_item/envs/
  3. virtualenv /home/python_item/envs/tender --python=python3.9
复制代码
3)激活假造环境

  1. source  /home/python_item/envs/tender/bin/activate
复制代码
4)安装项目依靠

  1. cd /home/python_item/tender_project #进入项目目录
  2. pip3.9 install flask
  3. pip3.9 install lxml
  4. pip3.9 install pymysql
  5. pip3.9 install requests
复制代码
3、uwsgi安装

1)安装uwsgi

  1. 激活虚拟环境,在虚拟环境中安装
  2. source  /home/python_item/envs/tender/bin/activate
  3. pip install uwsgi
复制代码
2)基于uwsgi配置文件的方式运行flask项目

  1. [uwsgi]
  2.     socket = 127.0.0.1:8001  
  3.     chdir = /home/python_item/tender_project
  4.     wsgi-file = main.py
  5.     callable = app
  6.     processes = 1
  7.     virtualenv = /home/python_item/envs/tender/
复制代码
3)启动项目

  1. uwsgi --ini tender_test.ini
复制代码
报错:
  1. (tender) [root@node1 tender_project]# uwsgi --ini tender_test.ini
  2. [uWSGI] getting INI configuration from tender_test.ini
  3. *** Starting uWSGI 2.0.26 (64bit) on [Tue Jul 16 15:30:21 2024] ***
  4. compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-44) on 16 July 2024 07:24:56
  5. os: Linux-3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022
  6. nodename: node1
  7. machine: x86_64
  8. clock source: unix
  9. pcre jit disabled
  10. detected number of CPU cores: 8
  11. current working directory: /home/python_item/tender_project
  12. detected binary path: /home/python_item/envs/tender/bin/uwsgi
  13. uWSGI running as root, you can use --uid/--gid/--chroot options
  14. *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
  15. chdir() to /home/python_item/tender_project
  16. *** WARNING: you are running uWSGI without its master process manager ***
  17. your processes number limit is 7787
  18. your memory page size is 4096 bytes
  19. detected max file descriptor number: 1024
  20. lock engine: pthread robust mutexes
  21. thunder lock: disabled (you can enable it with --thunder-lock)
  22. uwsgi socket 0 bound to TCP address 127.0.0.1:8001 fd 3
  23. uWSGI running as root, you can use --uid/--gid/--chroot options
  24. *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
  25. Python version: 3.9.5 (default, Jul 16 2024, 15:00:56)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  26. PEP 405 virtualenv detected: /home/python_item/envs/tender/
  27. Set PythonHome to /home/python_item/envs/tender/
  28. *** Python threads support is disabled. You can enable it with --enable-threads ***
  29. Python main interpreter initialized at 0x27abcc0
  30. uWSGI running as root, you can use --uid/--gid/--chroot options
  31. *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
  32. your server socket listen backlog is limited to 100 connections
  33. your mercy for graceful operations on workers is 60 seconds
  34. mapped 72920 bytes (71 KB) for 1 cores
  35. *** Operational MODE: single process ***
  36. Traceback (most recent call last):
  37.   File "main.py", line 7, in <module>
  38.     from common import WebInfoFactory
  39.   File "/home/python_item/tender_project/./common.py", line 2, in <module>
  40.     import requests
  41.   File "/home/python_item/envs/tender/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>
  42.     import urllib3
  43.   File "/home/python_item/envs/tender/lib/python3.9/site-packages/urllib3/__init__.py", line 42, in <module>
  44.     raise ImportError(
  45. ImportError: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips  26 Jan 2017'. See: https://github.com/urllib3/urllib3/issues/2168
  46. unable to load app 0 (mountpoint='') (callable not found or import error)
  47. *** no app loaded. going in full dynamic mode ***
  48. uWSGI running as root, you can use --uid/--gid/--chroot options
  49. *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
  50. *** uWSGI is running in multiple interpreter mode ***
  51. spawned uWSGI worker 1 (and the only) (pid: 1600, cores: 1)
复制代码
解决:
urllib3版本不兼容,卸载重装
  1. pip3.9 uninstall urllib3
  2. pip3.9 install urllib3==1.22
复制代码
重启即可
  1. uwsgi --ini tender_test.ini
复制代码
nginx

安装:Linux安装Nginx
配置:
  1.     upstream flask {
  2.         server 127.0.0.1:8001 weight=1;
  3. }
  4.    
  5.     server {
  6.         listen       80;
  7.         server_name  localhost;
  8.         #charset koi8-r;
  9.         #access_log  logs/host.access.log  main;
  10.         location / {
  11.             root   html;
  12.             index  index.html index.htm;
  13.         }
  14.         location /tenderTest {   
  15.             uwsgi_pass   flask;
  16.             include  /usr/local/nginx/conf/uwsgi_params;
  17. #            index  index.html index.htm;
  18.         }
  19.         location /test/ {
  20.             uwsgi_pass   127.0.0.1:8002;
  21.             include  /usr/local/nginx/conf/uwsgi_params;
  22. #            index  index.html index.htm;
  23.         }
  24.         #error_page  404              /404.html;
  25.         # redirect server error pages to the static page /50x.html
  26.         #
  27.         error_page   500 502 503 504  /50x.html;
  28.         location = /50x.html {
  29.             root   html;
  30.         }
  31.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  32.         #
  33.         #location ~ \.php$ {
  34.         #    proxy_pass   http://127.0.0.1;
  35.         #}
  36.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  37.         #
  38.         #location ~ \.php$ {
  39.         #    root           html;
  40.         #    fastcgi_pass   127.0.0.1:9000;
  41.         #    fastcgi_index  index.php;
  42.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  43.         #    include        fastcgi_params;
  44.         #}
  45.         # deny access to .htaccess files, if Apache's document root
  46.         # concurs with nginx's one
  47.         #
  48.         #location ~ /\.ht {
  49.         #    deny  all;
  50.         #}
  51.     }
复制代码
4.访问成功 


5.编写脚本快速重启/制止

reboot.sh
  1. #! /usr/bin/env bash
  2. echo -e "======================wsgi process======================"
  3. ps -ef|grep tender_test.ini |grep -v grep
  4. sleep 0.5
  5. echo -e "======================going to close ======================"
  6. ps -ef | grep tender_test.ini | grep -v grep | awk '{print $2}' | xargs kill -9
  7. sleep 0.5
  8. echo -e "======================check if the kill action is correct ======================"
  9. /home/python_item/envs/tender/bin/uwsgi --ini tender_test.ini & >/dev/null
  10. echo -e "======================started... ======================"
  11. sleep 1
  12. ps -ef | grep tender_test.ini |grep -v grep
复制代码
 stop.sh
  1. echo -e "======================wsgi process======================"
  2. ps -ef|grep tender_test.ini |grep -v grep
  3. sleep 0.5
  4. echo -e "======================going to close ======================"
  5. ps -ef  |grep tender_test.ini | grep -v grep | awk '{print $2}' | xargs kill -9
  6. sleep 0.5
复制代码
在项目目录下实验重启或制止,脚本放在下面,根据本身的现实环境举行修改即可
更新权限
  1. chmod 775 reboot.sh
  2. chmod 775 stop.sh
复制代码
启动!
  1. ./reboot.sh
  2. ./stop.sh
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立山

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表