博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux - CentOS 7.2系统 Nginx-1.10.2部署python的web环境 框架flask
阅读量:2159 次
发布时间:2019-05-01

本文共 14970 字,大约阅读时间需要 49 分钟。

文首前言

其实也是刚开始学python,但还没到搭web环境这一步,强行试了一下部署python的web环境,结果从上周二搞到现在!帖子、视频,看了不计其数,当然,也踩了很多坑!一开始甚至都没搞懂Django和flask是什么关系,那个痛苦、纠结,真的不堪回首,有几次,看着马上就要成功了,结果,就差一步,愣是搞了好几个小时,各种排错,填坑,然而,并无卵用,MD,真的,好多次想放弃,好多次!但想想,放弃好丢人,就坚持了一下,走走停停,磕磕绊绊的,终于,给搞出来了!今天,就只简单说说搭建步骤,知识点就不扫了(因为这类的帖子,真的好多,网上一搜就是)。

软件版本

LinuxCentOS 7.2Nginx-1.10.2Python-3.5.2

具体步骤

1、准备CentOS 7.2系统环境,并创建基础目录mkdir -p /mine/softwarecd /mine/software
2、进入到软件存放目录,下载Python-3.5.2.tgz文件,解压、配置、编译、安装wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgztar -zxvf Python-3.5.2.tgzcd Python-3.5.2./configuremakemake install
python的配置、编译、安装可能会好一会。执行完【 make install】,最后结果大概是:
if test "xupgrade" != "xno"  ; then \	case upgrade in \		upgrade) ensurepip="--upgrade" ;; \		install|*) ensurepip="" ;; \	esac; \	 ./python -E -m ensurepip \		$ensurepip --root=/ ; \fiIgnoring ensurepip failure: pip 8.1.1 requires SSL/TLS
这里有提示,需要SSL/TLS这两个协议,所以,得安装一下yum install openssl-devel -yyum install zlib-devel -y
若你忘了这一步,在安装完Nginx,再执行以上操作的时候,可能出现
[root@iZm5e8dv3a3mevwweefuo2Z /]# yum install openssl-devel -yLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfilePackage 1:openssl-devel-1.0.2k-19.el7.x86_64 already installed and latest versionNothing to do[root@iZm5e8dv3a3mevwweefuo2Z /]# yum install zlib-devel -yLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfilePackage zlib-devel-1.2.7-18.el7.x86_64 already installed and latest versionNothing to do[root@iZm5e8dv3a3mevwweefuo2Z /]#
出现以上,表示已安装,就不用管了!
测试python3 如下成功:
[root@iZm5e8dv3a3mevwweefuo2Z nginx-1.10.2]# python3Python 3.5.2 (default, Nov 19 2019, 11:09:08)[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linuxType "help", "copyright", "credits" or "license" for more information.>>>
3、下载、安装Nginx-1.10.2到指定目录安装依赖(任何路径都可以):yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel目录:/mine/software/cd /mine/software/获取资源wget http://nginx.org/download/nginx-1.10.2.tar.gz解压tar -zxvf nginx-1.10.2.tar.gz创建安装目录:mkdir /mine/serve/nginx进入压缩包目录cd nginx-1.10.2配置部署Nginx./configure --prefix=/mine/serve/nginx编译、安装make && make install
这里,只要你提前安装了依赖,通常不会有异常、报错出现!
4、创建站点项目目录、并进入mkdir -p /mine/www/mysite进入cd /mine/www/
5、使用python3 创建虚拟目录位置:/mine/www创建虚拟目录:python3 -m venv venv注意:这里一定是python3,而不是python
python3 创建虚拟目录,并查看,如下成功:
[root@iZm5e8dv3a3mevwweefuo2Z www]# python3 -m venv venv[root@iZm5e8dv3a3mevwweefuo2Z www]# lsmysite  venv
6、激活虚拟环境进入venv目录看一下cd /mine/www/venv/退出venv目录cd ..激活source venv/bin/activate
激活虚拟目录,并查看,如下成功:
[root@iZm5e8dv3a3mevwweefuo2Z www]# source venv/bin/activate(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]#
出现“(venv) ”表示进入虚拟环境。
7、使用pip安装flask框架在哪个目录都可以执行此操作pip3 install flask注意:这里可能有坑,提示-bash: pip3: command not found这时,你可以试一下pip install flask
出现如下,表示成功:
(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]# pip install flaskCollecting flask  Downloading http://mirrors.aliyun.com/pypi/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)    100% |████████████████████████████████| 102kB 1.9MB/sCollecting Jinja2>=2.10.1 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/65/e0/eb35e762802015cab1ccee04e8a277b03f1d8e53da3ec3106882ec42558b/Jinja2-2.10.3-py2.py3-none-any.whl (125kB)    100% |████████████████████████████████| 133kB 6.0MB/sCollecting click>=5.1 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)    100% |████████████████████████████████| 81kB 57.4MB/sCollecting Werkzeug>=0.15 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/ce/42/3aeda98f96e85fd26180534d36570e4d18108d62ae36f87694b476b83d6f/Werkzeug-0.16.0-py2.py3-none-any.whl (327kB)    100% |████████████████████████████████| 327kB 66.5MB/sCollecting itsdangerous>=0.24 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whlRequirement already satisfied: MarkupSafe>=0.23 in /usr/lib/python2.7/site-packages (from Jinja2>=2.10.1->flask)Installing collected packages: Jinja2, click, Werkzeug, itsdangerous, flask  Found existing installation: Jinja2 2.9.6    Uninstalling Jinja2-2.9.6:      Successfully uninstalled Jinja2-2.9.6Successfully installed Jinja2-2.10.3 Werkzeug-0.16.0 click-7.0 flask-1.1.1 itsdangerous-1.1.0You are using pip version 9.0.1, however version 19.3.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.
8、对Nginx服务的配置文件,进行配置注意:配置Nginx的时候,新开一个xshell窗口,因为【7】处在虚拟环境进入配置文件cd /mine/serve/nginx/conf备份配置文件cp nginx.conf nginx.conf.bak创建配置文件目录mkdir site-enable修改配置文件vi nginx.conf(按insert键,看到左下角出现“--insert”字样,进入编辑模式,右键,粘贴以下内容,然后,摁Esc键,切换到英文模式,最后输入“:wq”,即保存)
配置文件修改后,如下
user  root;worker_processes  auto;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    gzip            on;    sendfile        on;    keepalive_timeout  65;    include sites-enabled/*.conf;}
进入新建目录cd sites-enabled编辑、创建配置文件vim mysite.conf
server {        listen          80;        server_name     36.139.25.678;        charset         utf-8;        access_log      /mine/log/nginx/mysite.access.log main;        error_log       /mine/log/nginx/mysite.error.log  warn;        location / {                root    /mine/www/mysite;                index   index.html index.htm;                include         uwsgi_params;                uwsgi_pass      127.0.0.1:8080;                uwsgi_param     UWSGI_PYHOME    /mine/www/venv;                uwsgi_param     UWSGI_CHDIR     /mine/www/mysite;                uwsgi_param     PYTHONPATH      /mine/www/mysite;                uwsgi_param     UWSGI_MODULE    run;                uwsgi_param     UWSGI_CALLABLE  app;        }}
创建日志目录,否则,测试Nginx的时候,会有报错mkdir -p /mine/log/nginx
9、回到虚拟环境窗口,准备安装uwsgi容器如果窗口关闭了,可重开一下,激活虚拟环境,即可进入虚拟环境
进入虚拟环境
[root@iZm5e8dv3a3mevwweefuo2Z ~]# cd /mine/www/[root@iZm5e8dv3a3mevwweefuo2Z www]# lsmysite  venv[root@iZm5e8dv3a3mevwweefuo2Z www]# source venv/bin/activate(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]#
使用pip3安装uwsgi容器pip3 install uwsgi如果出现以下情况,可换pip试试(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]# pip3 install uwsgi-bash: pip3: command not found换pip试试pip install uwsgi如果有执行,但执行完显示“compilation terminated”,表示编译终止。然后,还提示你升级pip,不建议升级,即使升级,再安装uwsgi报错更严重!
那怎么办呢?重新配置、编译、安装python3.5 !是的,你没看错,重新来配置、编译、安装python3.5! (这是捷径!不信,你可以试试其他方案!!)a、进入你下载、解压的目录,配置、编译、安装cd /mine/software/Python-3.5.2./configuremakemake install
最终结果如是下面内容,表示好的兆头
Ignoring indexes: https://pypi.python.org/simpleCollecting setuptoolsCollecting pipInstalling collected packages: setuptools, pipSuccessfully installed pip-8.1.1 setuptools-20.10.1
b、删除原来的虚拟目录,重新生成、激活进入/mine/www目录cd /mine/www删除虚拟目录rm -rf venv重新创建虚拟目录python3 -m venv venv激活source venv/bin/activatec、重新安装flaskpip3 install flask出现如下,表示成功
(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]# pip3 install flaskCollecting flask  Downloading http://mirrors.aliyun.com/pypi/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)    100% |████████████████████████████████| 102kB 2.1MB/sCollecting itsdangerous>=0.24 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whlCollecting Werkzeug>=0.15 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/ce/42/3aeda98f96e85fd26180534d36570e4d18108d62ae36f87694b476b83d6f/Werkzeug-0.16.0-py2.py3-none-any.whl (327kB)    100% |████████████████████████████████| 327kB 6.9MB/sCollecting click>=5.1 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)    100% |████████████████████████████████| 81kB 75.9MB/sCollecting Jinja2>=2.10.1 (from flask)  Downloading http://mirrors.aliyun.com/pypi/packages/65/e0/eb35e762802015cab1ccee04e8a277b03f1d8e53da3ec3106882ec42558b/Jinja2-2.10.3-py2.py3-none-any.whl (125kB)    100% |████████████████████████████████| 133kB 52.1MB/sCollecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)  Downloading http://mirrors.aliyun.com/pypi/packages/6e/57/d40124076756c19ff2269678de7ae25a14ebbb3f6314eb5ce9477f191350/MarkupSafe-1.1.1-cp35-cp35m-manylinux1_x86_64.whlInstalling collected packages: itsdangerous, Werkzeug, click, MarkupSafe, Jinja2, flaskSuccessfully installed Jinja2-2.10.3 MarkupSafe-1.1.1 Werkzeug-0.16.0 click-7.0 flask-1.1.1 itsdangerous-1.1.0You are using pip version 8.1.1, however version 19.3.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.
上面的结果,跟上面的【7】的结果好像有不同,仔细观察!
d、再次尝试安装uwsgi,结果如下,表示成功
(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]# pip3 install uwsgiCollecting uwsgi  Downloading http://mirrors.aliyun.com/pypi/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz (801kB)    100% |████████████████████████████████| 808kB 87.1MB/sInstalling collected packages: uwsgi  Running setup.py install for uwsgi ... doneSuccessfully installed uwsgi-2.0.18You are using pip version 8.1.1, however version 19.3.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.
10、给uwsgi创建配置文件切换到非虚拟目录,新打开一个窗口即可。进入到mysite目录cd /mine/serve/mysite创建配置文件uwsgi.inivim uwsgi.ini
内容如下:
[uwsgi]enable-threads=truethunder-lock=truevhost=truesocket=127.0.0.1:8080master=trueworkers=4max-requests=128
11、配置测试、启动Nginx服务进入启动程序目录cd /mine/serve/nginx/sbin测试Nginx./nginx -t -c /mine/serve/nginx/conf/nginx.conf
[root@iZm5e8dv3a3mevwweefuo2Z sbin]# ./nginx -t -c /mine/serve/nginx/conf/nginx.confnginx: [emerg] unknown log format "main" in /mine/serve/nginx/conf/sites-enabled/mysite.conf:5nginx: configuration file /mine/serve/nginx/conf/nginx.conf test failed
有报错!查看,报错行access_log /mine/log/nginx/mysite.access.log main;没有创建日志目录,无法写入,故报错,解决,创建目录cd /mine/mkdir -p log/nginx继续测试Nginx服务cd /mine/serve/nginx/sbin./nginx -t -c /mine/serve/nginx/conf/nginx.conf
结果:
[root@iZm5e8dv3a3mevwweefuo2Z sbin]# ./nginx -t -c /mine/serve/nginx/conf/nginx.confnginx: the configuration file /mine/serve/nginx/conf/nginx.conf syntax is oknginx: configuration file /mine/serve/nginx/conf/nginx.conf test is successful
启动Nginx进入/mine/serve/nginx/sbin/./nginx -c /mine/serve/nginx/conf/nginx.conf
[root@iZm5e8dv3a3mevwweefuo2Z sbin]# ./nginx -c /mine/serve/nginx/conf/nginx.conf[root@iZm5e8dv3a3mevwweefuo2Z sbin]#
没有结果,就是最好的结果!表示启动成功!!!
12、切换到另一个虚拟环境的窗口,重启uwsgi进入到虚拟环境 cd /mine/wwwwuwsgi --ini mysite/uwsgi.ini
如果服务窗口停止连接,可重新进入
[root@iZm5e8dv3a3mevwweefuo2Z ~]# cd /mine/www/[root@iZm5e8dv3a3mevwweefuo2Z www]# lsmysite  venv[root@iZm5e8dv3a3mevwweefuo2Z www]# source venv/bin/activate(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]#
执行完“uwsgi --ini mysite/uwsgi.ini”, 如果看到以下,不要慌张,这不是报错!!!不是!!!!!
(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]# uwsgi --ini mysite/uwsgi.ini[uWSGI] getting INI configuration from mysite/uwsgi.ini*** Starting uWSGI 2.0.18 (64bit) on [Tue Nov 19 14:28:30 2019] ***compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 19 November 2019 05:47:02os: Linux-3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017nodename: iZm5e8dv3a3mevwweefuo2Zmachine: x86_64clock source: unixpcre jit disableddetected number of CPU cores: 1current working directory: /mine/wwwdetected binary path: /mine/www/venv/bin/uwsgiuWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***your processes number limit is 3896your memory page size is 4096 bytesdetected max file descriptor number: 65535VirtualHosting mode enabled.lock engine: pthread robust mutexesthunder lock: enableduwsgi socket 0 bound to TCP address 127.0.0.1:8080 fd 3uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***Python version: 3.5.2 (default, Nov 19 2019, 13:31:56)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]Python main interpreter initialized at 0x1176090uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***python threads support enabledyour server socket listen backlog is limited to 100 connectionsyour mercy for graceful operations on workers is 60 secondsmapped 364600 bytes (356 KB) for 4 cores*** Operational MODE: preforking ****** no app loaded. going in full dynamic mode ***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 master process (pid: 17378)spawned uWSGI worker 1 (pid: 17379, cores: 1)spawned uWSGI worker 2 (pid: 17380, cores: 1)spawned uWSGI worker 3 (pid: 17381, cores: 1)spawned uWSGI worker 4 (pid: 17382, cores: 1)
uwsgi服务启动!
13、在mysite目录创建run.py文件cd /mine/www/mysitevim run.py
内容如下:
from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():    return "Hello Python! Happy LDR!  "if __name__ == "__main__":    app.run(host='0.0.0.0', port=8080)
14、在浏览器输入你的ip,查看

看到以上内容,表示成功!
15、这里只是ip,我还还可以通过配置域名,进行访问首先,你得有域名。我这里是在万网购买的。有了域名(你还得备案,备案得好久,阿里会给你寄一个蓝底白字的大纸,让你站跟前拍照,并上传,备案,然后,提示你几个工作日完成。这里只说你已经备案完成的情况)登录阿里云控制台解析,登录控制台,进入【域名】-》进入【域名列表】-》在域名列表,找到你要用的域名-》【操作】列列点击【解析】-》若没有记录-》【添加记录】-》会出现一个弹出层,你只需在【主机记录】后填 www , 然后,在【记录值】后写上你的公网IP,如23.156.22.789 ,确定,提交即可!然后,修改/nginx/sites-enabled/mysite.conf在server_name行,添加你的域名,即可
server {        listen          80;        server_name     www.idonknow.net;        charset         utf-8;        access_log      /mine/log/nginx/mysite.access.log;        error_log       /mine/log/nginx/mysite.error.log  warn;        location / {                root    /mine/www/mysite;                index   index.html index.htm;                include         uwsgi_params;                uwsgi_pass      127.0.0.1:8080;                uwsgi_param     UWSGI_PYHOME    /mine/www/venv;                uwsgi_param     UWSGI_CHDIR     /mine/www/mysite;                uwsgi_param     PYTHONPATH      /mine/www/mysite;                uwsgi_param     UWSGI_MODULE    run;                uwsgi_param     UWSGI_CALLABLE  app;        }}
更改完配置之后,一定记得重启Nginx!但是,这里得注意,首先得停止uwsgi服务(ctrl+c退出),然后,停止Nginx服务(./nginx -s quit)!最后,重启Nginx服务,重新启动 uwsgi 服务[Nginx重启]cd /mine/serve/nginx/sbin./nginx -c /mine/serve/nginx/conf/nginx.conf[uwsgi重启]前提:在虚拟环境uwsgi --ini mysite/uwsgi.ini看见*** uWSGI is running in multiple interpreter mode ***spawned uWSGI master process (pid: 17454)spawned uWSGI worker 1 (pid: 17455, cores: 1)spawned uWSGI worker 2 (pid: 17456, cores: 1)spawned uWSGI worker 3 (pid: 17457, cores: 1)spawned uWSGI worker 4 (pid: 17458, cores: 1)
最后,通过浏览器访问域名,如果,出现你打印的内容,表示成功!!!

结束!!!!

写在最后

通过总结这篇帖子,我细细回想了一下,整个过程,从了解、学习新的知识点,到踩坑、填坑,这中间钻了不少牛角尖,老是在那死磕,最后,也没找到好的途径,浪费了时间,消磨了斗志,其实,这种方式是错误的!碰到一个多小时还搞不定的问题,就该思考,是不是方式错了,是不是哪个知识点不了解,及时退出来,从全局的角度,思考一下,汇总知识点,重新出发,或许,才会更快的解决问题!

转载地址:http://rrrwb.baihongyu.com/

你可能感兴趣的文章
Android Studio color和font设置
查看>>
Python 格式化打印json数据(展开状态)
查看>>
Centos7 安装curl(openssl)和libxml2
查看>>
Centos7 离线安装RabbitMQ,并配置集群
查看>>
Centos7 or Other Linux RPM包查询下载
查看>>
运行springboot项目出现:Type javax.xml.bind.JAXBContext not present
查看>>
Java中多线程向mysql插入同一条数据冲突问题
查看>>
Idea Maven项目使用jar包,添加到本地库使用
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>
contOS6 部署 lnmp、FTP、composer、ThinkPHP5、docker详细步骤
查看>>
TP5.1模板布局中遇到的坑,配置完不生效解决办法
查看>>
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>