Linux - CentOS 7.2系统 Nginx-1.10.2部署python的web环境 框架flask

文首前言

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

软件版本

Linux
CentOS 7.2
Nginx-1.10.2
Python-3.5.2

具体步骤

1、准备CentOS 7.2系统环境,并创建基础目录
mkdir -p /mine/software
cd /mine/software
2、进入到软件存放目录,下载Python-3.5.2.tgz文件,解压、配置、编译、安装
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
tar -zxvf Python-3.5.2.tgz
cd Python-3.5.2
./configure
make
make install
python的配置、编译、安装可能会好一会。执行完【 make install】,最后结果大概是:
if test "xupgrade" != "xno"  ; then \
	case upgrade in \
		upgrade) ensurepip="--upgrade" ;; \
		install|*) ensurepip="" ;; \
	esac; \
	 ./python -E -m ensurepip \
		$ensurepip --root=/ ; \
fi
Ignoring ensurepip failure: pip 8.1.1 requires SSL/TLS
这里有提示,需要SSL/TLS这两个协议,所以,得安装一下
yum install openssl-devel -y
yum install zlib-devel -y
若你忘了这一步,在安装完Nginx,再执行以上操作的时候,可能出现
[root@iZm5e8dv3a3mevwweefuo2Z /]# yum install openssl-devel -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package 1:openssl-devel-1.0.2k-19.el7.x86_64 already installed and latest version
Nothing to do
[root@iZm5e8dv3a3mevwweefuo2Z /]# yum install zlib-devel -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
Nothing to do
[root@iZm5e8dv3a3mevwweefuo2Z /]#
出现以上,表示已安装,就不用管了!
测试python3 如下成功:
[root@iZm5e8dv3a3mevwweefuo2Z nginx-1.10.2]# python3
Python 3.5.2 (default, Nov 19 2019, 11:09:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "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]# ls
mysite  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 flask
Collecting 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/s
Collecting 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/s
Collecting 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/s
Collecting 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/s
Collecting itsdangerous>=0.24 (from flask)
  Downloading http://mirrors.aliyun.com/pypi/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Requirement 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.6
Successfully installed Jinja2-2.10.3 Werkzeug-0.16.0 click-7.0 flask-1.1.1 itsdangerous-1.1.0
You 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]# ls
mysite  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
./configure
make
make install
最终结果如是下面内容,表示好的兆头
Ignoring indexes: https://pypi.python.org/simple
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully 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/activate

c、重新安装flask
pip3 install flask
出现如下,表示成功
(venv) [root@iZm5e8dv3a3mevwweefuo2Z www]# pip3 install flask
Collecting 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/s
Collecting itsdangerous>=0.24 (from flask)
  Downloading http://mirrors.aliyun.com/pypi/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting 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/s
Collecting 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/s
Collecting 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/s
Collecting 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.whl
Installing collected packages: itsdangerous, Werkzeug, click, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.3 MarkupSafe-1.1.1 Werkzeug-0.16.0 click-7.0 flask-1.1.1 itsdangerous-1.1.0
You 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 uwsgi
Collecting uwsgi
  Downloading http://mirrors.aliyun.com/pypi/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz (801kB)
    100% |████████████████████████████████| 808kB 87.1MB/s
Installing collected packages: uwsgi
  Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.18
You 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.ini
vim uwsgi.ini
内容如下:
[uwsgi]
enable-threads=true
thunder-lock=true
vhost=true
socket=127.0.0.1:8080
master=true
workers=4
max-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.conf
nginx: [emerg] unknown log format "main" in /mine/serve/nginx/conf/sites-enabled/mysite.conf:5
nginx: 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.conf
nginx: the configuration file /mine/serve/nginx/conf/nginx.conf syntax is ok
nginx: 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/wwww
uwsgi --ini mysite/uwsgi.ini
如果服务窗口停止连接,可重新进入
[root@iZm5e8dv3a3mevwweefuo2Z ~]# cd /mine/www/
[root@iZm5e8dv3a3mevwweefuo2Z www]# ls
mysite  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:02
os: Linux-3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017
nodename: iZm5e8dv3a3mevwweefuo2Z
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /mine/www
detected binary path: /mine/www/venv/bin/uwsgi
uWSGI 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 3896
your memory page size is 4096 bytes
detected max file descriptor number: 65535
VirtualHosting mode enabled.
lock engine: pthread robust mutexes
thunder lock: enabled
uwsgi socket 0 bound to TCP address 127.0.0.1:8080 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.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 0x1176090
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 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/mysite
vim run.py
内容如下:
from flask import Flask

app = 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)
最后,通过浏览器访问域名,如果,出现你打印的内容,表示成功!!!

结束!!!!

写在最后

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

猜你喜欢

转载自blog.csdn.net/LDR1109/article/details/103144147