python服务器环境(python3+nginx+flask+uwsgi+supervisor)

python web服务器环境搭建

服务器为cnetos7,以下环境以此为基准。

1、安装python3并与python2共存

1.1 python检测

centos7默认安装了 python2.7.5 因为一些命令要用它比如yum,它使用的是python2.7.5。

使用 python -V 命令查看一下是否安装Python。

[root@localhost bin]# python -V
Python 2.7.5

然后使用命令 which python 查看一下Python可执行文件的位置。

[root@localhost bin]# which python
/usr/bin/python
[root@localhost bin]# cd /usr/bin/
[root@localhost bin]# ll py*
lrwxrwxrwx. 1 root root    7 4月   8 2017 python -> python2
lrwxrwxrwx. 1 root root    9 4月   8 2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 6月  18 2014 python2.7

python 指向的是python2.7。因为我们要安装python3版本,所以python要指向python3才行。

先yum安装必要的包,用于下载编译python3。

[root@localhost bin]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

安装后防止错误,先做备份:

[root@localhost bin]# mv python python.bak
[root@localhost bin]# ll pytho*
lrwxrwxrwx. 1 root root    9 4月   8 2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 6月  18 2014 python2.7
lrwxrwxrwx. 1 root root    7 4月   8 2017 python.bak -> python2

1.2 安装python3

获取安装包。

[root@localhost ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz

解压。

[root@localhost ~]# tar -xvJf  Python-3.6.4.tar.xz

切进目录。

[root@localhost ~]# cd Python-3.6.4/

编译,安装。

[root@localhost Python-3.6.4]# ./configure prefix=/usr/local/python3
[root@localhost Python-3.6.4]# make && make install

安装完毕,/usr/local/目录下就会有python3了。

[root@localhost Python-3.6.4]# cd /usr/local/python3/
[root@localhost python3]# ll
总用量 4
drwxr-xr-x 2 root root 4096 3月  21 18:37 bin
drwxr-xr-x 3 root root   23 3月  21 18:37 include
drwxr-xr-x 4 root root   60 3月  21 18:37 lib
drwxr-xr-x 3 root root   16 3月  21 18:37 share

然后我们可以添加软链到执行目录下/usr/bin。

[root@localhost Python-3.6.4]# ln -s /usr/local/python3/bin/python3 /usr/bin/python

同时pip也建立一个软连接。

[root@localhost bin]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

然后软连接创建完成,查看当前python版本。

[root@localhost python3]# python -V
Python 3.6.4
[root@localhost python3]# python2 -V
Python 2.7.5
[root@localhost python3]# cd /usr/bin/
[root@localhost bin]# ll python*
lrwxrwxrwx  1 root root   30 3月  21 18:40 python -> /usr/local/python3/bin/python3
lrwxrwxrwx. 1 root root    9 4月   8 2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 6月  18 2014 python2.7

最后因为执行yum需要python2的版本,所以我们还要修改yum的配置。

[root@localhost bin]# vim /usr/bin/yum
把#!/usr/bin/python 修改为 #!/usr/bin/python2

同理。

[root@localhost bin]# vim /usr/libexec/urlgrabber-ext-down

这样python3版本就安装完成,同时python2也存在。

2、uwigi安装

这个很坑,老司机弯都转不过来。

坑如下:

说是在基于Debian的发行版上要安装依赖,依赖么,我懂的。

apt-get install build-essential python-dev

然后我去找centos版本的相关依赖。

然后下面这样,python-dev的包在centos的yum中不叫python-dev,而是python-devel.

[root@localhost local]# yum install python-devel

然后build-essential对应的工具说是下面这样。

[root@localhost local]# yum groupinstall "Development Tools" 

然后centos警告错误啊。

[root@localhost local]# yum groupinstall "Development Tools"
已加载插件:fastestmirror, langpacks
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.163.com
 * updates: mirrors.nju.edu.cn
警告:分组 development 不包含任何可安装软件包。
Maybe run: yum groups mark install (see man yum)
指定组中没有可安装或升级的软件包

大家都是如是说:警告:分组 development 不包含任何可安装软件包。

尼玛,然后我就把这么说的全部包都安装了,两列,必要项和可选项。

[root@localhost local]# yum install ElectricFence ant babel bzr chrpath cmake compat-gcc-44 compat-gcc-44-c++ cvs dejagnu expect gcc-gnat gcc-objc gcc-objc++ imake javapackages-tools ksc libstdc++-docs  mercurial mod_dav_svn nasm perltidy python-docs rpmdevtools rpmlint systemtap-sdt-devel systemtap-server

[root@localhost local]# yum install autoconf automake binutils bison flex gcc gcc-c++ gettext libtool make patch pkgconfig redhat-rpm-config rpm-build rpm-sign

安装后装uwsgi,用pip方式。

[root@localhost local]# pip install uwsgi

fuck,装完没可执行命令啊。

[root@localhost local]# uwsgi
bash: uwsgi: 未找到命令...

然后下面一顿倒腾,甚至py3我都重装了,车毁人亡。

最后不得已还是求助官档吧。

获取uwsgi

正确方法就是这句,使用网络安装器:

curl http://uwsgi.it/install | bash -s default /usr/local/uwsgi

卧槽了,吃枣药丸。继续前进!

3、nginx安装

以前直接用lnmp跑的站,然后有现成的,修修改改直接跑。

nignx配置文件:

server
{
    listen 80 default_server;
    server_name daocoder.ccom;
    location /
    {
        include        uwsgi_params;
        uwsgi_pass     127.0.0.1:8001;
    }
}

4、集成

4.1 nginx+uwsgi配置

之前nginx配置文件已有。

server
{
    listen 80 default_server;
    server_name daocoder.ccom;
    location /
    {
        include        uwsgi_params;
        uwsgi_pass     127.0.0.1:8001;
    }
}

下面uwsgi跑起来,编辑配置文件,那里创建都行,我是直接放在flask项目目录下创建uwsgi.ini内容如下:

[uwsgi]
# 启动程序时所使用的地址和端口,通常在本地运行flask项目,地址和端口是127.0.0.1:5000
# 不过在服务器上是通过uwsgi设置端口,通过uwsgi来启动项目,也就是说启动了uwsgi,也就启动了项目
socket = 127.0.0.1:8001
# 项目目录
# chdir = /home/wwwroot/flask/
# flask程序的启动文件,通常在本地是通过运行 python manage.py runserver 来启动项目的
wsgi-file = /home/wwwroot/flask/app.py
# 程序内启用的application变量名
callable = app
# 处理器个数
processes = 2
# 线程个数
threads = 2
# 获取uwsgi统计信息的服务地址
stats = 127.0.0.1:9191

4.2 flask项目

先下载flask。

[root@localhost flask]# pip install flask

我的路径是:

[root@localhost flask]# pwd
/home/wwwroot/flask

此文件加创建文件如下:

flask
    - templates/
        - index.html
    - app.py
    - uwsgi.ini

app.py文件内容:

#! /usr/bin/python
# -*- coding:utf-8 -*-

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


if __name__ == '__main__':
    app.run()

templates/index.html的内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flask</title>
</head>
<body>
    <div>hello daocoder</div>
</body>
</html>

uwsgi.ini的内容在前面已有。

完了后,nginx重新启动,uwsgi跑起来。

[root@localhost flask]# service nginx restart 
[root@localhost flask]# uwsgi uwsgi.ini 

最后浏览器访问你的服务器,bingo。

再一个问题在于uwsgi于前台进行,无法后台跑,有说emperor参数,我没尝试,下面会使用supervisor来改变这方式。

5 安装配置supervisor

5.1 安装supervisor

尝试直接安装,成功后可直接到配置下一节。

[root@localhost flask]# pip install supervisor

下面是我遇到的坑。

第一次失败。

[root@localhost flask]# pip install supervisor
Collecting supervisor
  Downloading supervisor-3.3.4.tar.gz (419kB)
    100% |████████████████████████████████| 419kB 5.1kB/s 
    Complete output from command python setup.py egg_info:
    Supervisor requires Python 2.4 or later but does not work on any version of Python 3.  You are using version 3.6.2 (default, Mar 22 2018, 07:59:08)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)].  Please install using a supported version.
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-1n5dvn1g/supervisor/

好像不支持python3,那就yum安装。

[root@localhost flask]# yum install supervisor
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.163.com
 * updates: mirrors.nju.edu.cn
没有可用软件包 supervisor。
错误:无须任何处理
[root@localhost flask]# yum info supervisor
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.163.com
 * updates: mirrors.nju.edu.cn
错误:没有匹配的软件包可以列出

无解,那就用python2指定pip安装。

[root@localhost bin]# python2 -m pip install supervisor
/usr/bin/python2: No module named pip

无力吐槽,原系统用的好像还是easy_install把管理工具这个,表示从来没用过。那就软连接先切回python2的版本再利用这个工具安装supervisor。

[root@localhost bin]# easy_install supervisor
Searching for supervisor
Reading https://pypi.python.org/simple/supervisor/
Best match: supervisor 3.3.4
Downloading https://pypi.python.org/packages/44/60/698e54b4a4a9b956b2d709b4b7b676119c833d811d53ee2500f1b5e96dc3/supervisor-3.3.4.tar.gz#md5=f1814d71d820ddfa8c86d46a72314cec
Processing supervisor-3.3.4.tar.gz
Writing /tmp/easy_install-aL9rb7/supervisor-3.3.4/setup.cfg
Running supervisor-3.3.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-aL9rb7/supervisor-3.3.4/egg-dist-tmp-_eRZYl
warning: no previously-included files matching '*' found under directory 'docs/.build'
Adding supervisor 3.3.4 to easy-install.pth file
Installing echo_supervisord_conf script to /usr/bin
Installing pidproxy script to /usr/bin
Installing supervisorctl script to /usr/bin
Installing supervisord script to /usr/bin

Installed /usr/lib/python2.7/site-packages/supervisor-3.3.4-py2.7.egg
Processing dependencies for supervisor
Searching for meld3>=0.6.5
Reading https://pypi.python.org/simple/meld3/
Best match: meld3 1.0.2
Downloading https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz#md5=3ccc78cd79cffd63a751ad7684c02c91
Processing meld3-1.0.2.tar.gz
Writing /tmp/easy_install-WgVnBv/meld3-1.0.2/setup.cfg
Running meld3-1.0.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-WgVnBv/meld3-1.0.2/egg-dist-tmp-fZ0vyo
zip_safe flag not set; analyzing archive contents...
Adding meld3 1.0.2 to easy-install.pth file

Installed /usr/lib/python2.7/site-packages/meld3-1.0.2-py2.7.egg
Finished processing dependencies for supervisor

安装完了,然后当然是切回来python3的版本了,下面是py2切回py3的操作。

[root@localhost bin]# ll pytho*
lrwxrwxrwx. 1 root root    7 4月   8 2017 python -> python2
lrwxrwxrwx  1 root root    9 3月  21 19:41 python2 -> python2.7
-rwxr-xr-x  1 root root 7136 8月   4 2017 python2.7
lrwxrwxrwx  1 root root   30 3月  22 08:04 python3.bak -> /usr/local/python3/bin/python3
[root@localhost bin]# mv python python2.bak
[root@localhost bin]# ll pytho*
lrwxrwxrwx  1 root root    9 3月  21 19:41 python2 -> python2.7
-rwxr-xr-x  1 root root 7136 8月   4 2017 python2.7
lrwxrwxrwx. 1 root root    7 4月   8 2017 python2.bak -> python2
lrwxrwxrwx  1 root root   30 3月  22 08:04 python3.bak -> /usr/local/python3/bin/python3
[root@localhost bin]# mv python3.bak python
[root@localhost bin]# ll pytho*
lrwxrwxrwx  1 root root   30 3月  22 08:04 python -> /usr/local/python3/bin/python3
lrwxrwxrwx  1 root root    9 3月  21 19:41 python2 -> python2.7
-rwxr-xr-x  1 root root 7136 8月   4 2017 python2.7
lrwxrwxrwx. 1 root root    7 4月   8 2017 python2.bak -> python2

这里发现确实pip安装后可能生成不了可执行命令,未找到相应答案去提了个问题pip安装模块后没有生成相应的可执行命令。当然这是臆断的,可能是我自己的服务器环境问题。以后估计还是要踩这个坑。

上面是自己煞笔了,pip安装后直接放在pythons/bin目录了,还要自己建立软连接到/usr/bin下面,这个确实蛋疼。

5.2 配置supervisor

因为上面的包是安装在py2里面的,所以不能直接跑。

编辑下/usr/bin/supervisord。

#!/usr/bin/python 改成 #!/usr/bin/python2

同理修改这几个命令文件:/usr/bin/echo_supervisord_conf、/usr/bin/supervisorctl。

官档 supervisor

都改完了,先看写帮助。

[root@localhost bin]# supervisord -h
supervisord -- run a set of applications as daemons.

Usage: /usr/bin/supervisord [options]

Options:
-c/--configuration FILENAME -- configuration file path (searches if not given)
-n/--nodaemon -- run in the foreground (same as 'nodaemon=true' in config file)
-h/--help -- print this usage message and exit
-v/--version -- print supervisord version number and exit
-u/--user USER -- run supervisord as this user (or numeric uid)
-m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
-d/--directory DIRECTORY -- directory to chdir to when daemonized
-l/--logfile FILENAME -- use FILENAME as logfile path
-y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile
-z/--logfile_backups NUM -- number of backups to keep when max bytes reached
-e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical)
-j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME
-i/--identifier STR -- identifier used for this instance of supervisord
-q/--childlogdir DIRECTORY -- the log directory for child process logs
-k/--nocleanup --  prevent the process from performing cleanup (removal of
                   old automatic child log files) at startup.
-a/--minfds NUM -- the minimum number of file descriptors for start success
-t/--strip_ansi -- strip ansi escape codes from process output
--minprocs NUM  -- the minimum number of processes available for start success
--profile_options OPTIONS -- run supervisord under profiler and output
                             results based on OPTIONS, which  is a comma-sep'd
                             list of 'cumulative', 'calls', and/or 'callers',
                             e.g. 'cumulative,callers')

然后这里说明下supervisor这个工具会在运行时,跑两个进程。一个是作为后台运行的服务supervisord,另一个是客户端supervisorctl用来交互supervisord

至此大概说完,下面开始配置supervisord。

1、创建默认的配置文件。

[root@localhost bin]# echo_supervisord_config > /etc/supervisord.conf

配置文件具体可参考CentOS7下Supervisor安装与配置

官档Supervisor配置

2、修改/etc/supervisord.conf文件

直接找到最下面的这个。

[include]                                   # 这里的‘;’注释去掉
;files = relative/directory/*.ini
files = /etc/supervisor/conf.d/*.conf       # 这里加入自定义的配置文件文件路径

3、创建自定义文件配置文件夹

[root@localhost etc]# mkdir -p supervisor/conf.d
[root@localhost etc]# cd supervisor/conf.d
[root@localhost conf.d]# vim uwsgi.cof
; ================================
;  uwsgi supervisor
; ================================

[program:uwsgi]
command=/usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini
directory=/home/wwwroot/flask
user=root
stdout_logfile=/var/log/flask/uwsgi_out.log
stderr_logfile=/var/log/flask/uwsgi_err.log
autostart=true
autorestart=true
startsecs=10
priority=997

结合自己的项目修修改改就好了。

都设置完了,下面跑起来吧。

[root@localhost supervisor]# killall supervisord
# -c指定配置文件路径,-n可前台运行,方便自己调试,具体自己去看官档
[root@localhost supervisor]# /usr/bin/supervisord -c /etc/supervisord.conf

然后看看跑起来没。

[root@localhost supervisor]# ps -aux | grep supervisord
root     16454  0.0  0.1 217868 10952 ?        Ss   13:12   0:00 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisord.conf
root     16633  0.0  0.0 112660   972 pts/1    S+   13:24   0:00 grep --color=auto supervisord

再看看uwsgi跑起来没。

[root@localhost supervisor]# ps -aux | grep uwsgi
root     16556  0.0  0.3 242740 22764 ?        S    13:15   0:00 /usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini
root     16558  0.0  0.3 316728 18860 ?        Sl   13:15   0:00 /usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini
root     16559  0.0  0.3 316728 18860 ?        Sl   13:15   0:00 /usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini
root     16647  0.0  0.0 112660   972 pts/1    S+   13:24   0:00 grep --color=auto uwsgi

然后客户端可以直接运行会出现命令行调试。

[root@localhost supervisor]# supervisorctl 
uwsgi                            RUNNING   pid 16556, uptime 0:26:03
supervisor> help

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

supervisor> 

有坑的话这个supervisor 启动报错 error: , Errno 2 No such file or directory: file: /usr/lib/python2.7/,其它就保证服务已经启动就好了。

最后可以用虚拟环境完美解决,理论上这样,未尝试,因为前面pip安装模块后无法生成可执行命令的坑。附上参考地址virtualenv - 廖雪峰的官方网站

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容