flask+nginx+uwsgi+supervisor 完整的部署过程(ubuntu16.04)
关于这几个东西每个的作用我就不多加阐述了,因为最近要部署很多服务给别人提供,之前一直在服务器测试时使用的nohup的方式运行程序,nohup的方式太不正规,于是找到了flask+nginx+uwsgi+supervisor的部署方式,把自己的部署过程进行记录。
1.nginx的安装与配置
安装nginx
sudo apt-get install nginx
安装完以后先测试一下nginx有没有安装成功,进行以下操作启动nginx。
sudo service nginx start
启动完毕以后打开浏览器输入网址0.0.0.0或者127.0.0.1,若页面出现“Welcome to nginx!”的提示语则证明安装成功。接下来要进行nginx配置文件的修改。
首先进入/etc/nginx/sites-available目录下。
cd /etc/nginx/sites-available/
接着创建一个新的文件,这个文件是本次项目部署的nginx配置文件。(非root用户记得再vim前加sudo)
sudo vim myproject
server {
listen 8889; #监听的端口
server_name 0.0.0.0; #主机名或者是ip,若是云服务器部署需要为公网ip或域名
location / {
include uwsgi_params; # 导入uwsgi配置
uwsgi_pass 127.0.0.1:8866; # 转发端口,需要和uwsgi配置当中的监听端口一致(也可以使用sock文件的方式这里就不展示了)
}
}
创建并编辑好nginx的配置文件以后,下一步要创建连接到/etc/nginx/sites-enabled文件夹下
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
然后对之前的配置进行测试
sudo nginx -t
测试没问题,重启nginx
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'
这里要说明一点的是,为了每次的项目管理和部署方便,我们是按照nginx原有的文件的存放方式进行存储的,即在/etc/nginx/sites-available下创建配置文件然后软连接到/etc/nginx/sites-enabled下面,这样再nginx启动的时候就会加载我们的配置,有心的小伙伴会在/etc/nginx/nginx.conf配置文件中找到include /etc/nginx/sites-enabled/*;一句脚本,相信大家也明白了加载方式。
2.uwsgi的安装与配置
安装uwsgi(可以使用apt-get与pip两种安装方式)
sudo apt-get install uwsgi-plugin-python3 #根据自己的python版本决定安装版本
sudo apt-get install uwsgi
先写一个简单的falsk的脚本manage.py,如下
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
return 'Hello Flask!'
if __name__ == '__main__':
app.run()
接着配置uwsgi,创建对这次测试的uwsgi配置文件uwsgi.ini,如下
[uwsgi]
# 如果使用nginx就用socket,这里要和nginx配置文件中的uwsgi_pass参数保持一致
socket=127.0.0.1:8866
# 如果不使用socket,直接用uwsgi启动程序,就是用http
#http=0.0.0.0:8866
# 如果程序中有文件下载,图片、视频等就需要就上这个配置
sgi-disable-file-wrapper=true
# flask程序在你服务器上的目录
chdir=/media/xxx/flask_test
# flask项目的启动文件
wsgi-file=manage.py
# 启动文件中的flask app(即第一部分中的app)
callable=app
# 启动的进程数
processes=1
#启动的线程数
threads=1
buffer-size = 999999999
# 是否启动多线程
enable-threads=True
# 是否将uwsgi的运行线程设置为主线程,如果设置为主进程,在开启的其他的子进程会随着主进程关闭而关闭
master=True
# 设置用来记录uwsgi进程号的文件
pidfile=uwsgi.pid
# 设置uwsgi运行的日志文件(可以记录程序运行的) 这里要关掉
#daemonize=uwsgi.log
#在每个worker而不是master中加载应用
lazy-apps=true
#这两条很重要,我被这坑了好久
plugins-dir=/usr/lib/uwsgi/plugins/
plugin = python3
运行以下命令启动uwsgi。(如果不需要与nginx进行配合使用的话可以将socket注释,将http的注释打开,运行以下命令,也可以进行部署)
uwsgi wsgi.ini
若打开浏览器,进入0.0.0.0:8889(这里进入的网址是nginx配置文件中server_name与listen两个参数决定的,具体什么意思你应该懂得),我在做这一步的时候犯了个大错,我之前设置的listen是80,也就是说我打开0.0.0.0网址就得看到以下结果,但是无论我怎么修改看到的都是‘Welcome to nginx!’,快要崩溃的时候,我按住ctrl+f5竟然出来了想要的结果,想删自己两个耳光!(网页缓存问题导致的)
3.supervisor的安装与配置
安装supervisor
sudo apt-get install supervisor
生成默认配置文件
echo_supervisord_config > supervisord.conf
我比较喜欢对每一个不同的项目单独创建一个配置文件,这样比较方便管理项目于是我创建了一个文件夹,这个文件夹里保存所有需要使用supervisor项目的配置文件。创建本项目的配置文件demo.ini,如下
[program:flask]
command = uwsgi /media/xxx/flask_test/uwsgi.ini ; 启动命令,可以看出与手动在命令行启动的命令是一样的
autostart = true ; 在 supervisord 启动的时候也自动启动
stopsignal=QUIT
user=yuhao
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
startretries = 3 ; 启动失败自动重试次数,默认是 3
autorestart = true ; 程序异常退出后自动重启
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile = /media/xxx/flask_test/log/supervisor.log
stderr_logfile = /media/xxx/flask_test/log/supervisor_err.log
接下来修改刚才生成的配置文件supervisord.conf,只要是修改最后一行,把之前的注释打开,并更换修改files的值为supervisor配置文件存放路径
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
[unix_http_server]
file=/media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
[supervisord]
logfile=/media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
;umask=022 ; (process file creation umask;default 022)
;user=chrism ; (default is current user, required if root)
;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
;directory=/media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super ; ('AUTO' child log dir, default $TEMP)
;environment=KEY="value" ; (key value pairs to add to environment)
;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as http_username if set
;password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
directory=/media/yuhao/5b0f7996-5d0a-42db-8e1a-957f828bb1a6/flask_test/super ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /media/xxx/flask_test/super/*.ini
然后来到与supervisord.conf平级的目录下运行以下命令即可开启supervisor
sudo supervisord -c supervisord.conf
记录完毕,欢迎讨论!
上一篇: 使用Excel批量导入数据
下一篇: python3实现随机数
推荐阅读