flask项目开发到部署配置
启动
- python manager runserver --threaded
flask-sqlacodegen生成model数据层
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/model.py"
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/AppSet.py" --tables app_sets --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Campus.py" --tables campus --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Classes.py" --tables classes --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Feedback.py" --tables feedback --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/MemberRole.py" --tables member_role --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Member.py" --tables members --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/PaperQuestion.py" --tables paper_question --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Paper.py" --tables papers --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/QuestionLog.py" --tables question_log --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Question.py" --tables questions --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/RoleRule.py" --tables role_rule --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Roles.py" --tables roles --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Rules.py" --tables rules --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Tag.py" --tables tags --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Uesr.py" --tables users --flask
need
Flask==1.0.2
Flask-Cache==0.13.1
Flask-Cors==3.0.6
Flask-SQLAlchemy==2.3.2
PyJWT==1.4.2
PyMySQL==0.9.2
redis==3.0.1
requests==2.20.0
Werkzeug=0.14.1
线上部署
1.安装python3
sudo mkdir /usr/local/python3 # 创建安装目录
$ wget --no-check-certificate https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz # 下载 Python 源文件
# 注意:wget获取https的时候要加上:--no-check-certifica
$ tar -xzvf Python-3.6.2.tgz # 解压缩包
$ cd Python-3.6.2 # 进入解压目录
sudo ./configure --prefix=/usr/local/python3 # 指定创建的目录
sudo make
sudo make install # 编译安装
2.配置两个版本共存
创建 python3 的软链接:
$ sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3
3.安装pip
# 下载源代码
$ wget --no-check-certificate https://github.com/pypa/pip/archive/9.0.1.tar.gz
$ tar -zvxf 9.0.1.tar.gz # 解压文件
$ cd pip-9.0.1
$ python3 setup.py install # 使用 Python 3 安装
创建链接:
$ sudo ln -s /usr/local/python3/bin/pip /usr/bin/pip3
升级 pip
$ pip install --upgrade pip
3.安装gunicorn
创建虚拟环境
cd /home/www/blog
mkdir venv
python3 -m venv venv
激活虚拟环境:
source venv/bin/activate
然后根据requirements.txt文件安装依赖包:
pip3 install -r requirements.txt
安装gunicorn
pip3 install gunicorn
启动服务:
gunicorn -w 4 -b 127.0.0.1:8000 manager:app
4.nignx配置
server
{
listen 80;
listen 443 ssl;
server_name ttl.pxx.com;
index index.html index.htm default.php default.htm default.html;
root /www/wwwroot/ttl_pxx_com;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
error_page 404 /404.html;
error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-00.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/ttl.pxx.com.conf;
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
location ^~ /api {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log /www/wwwlogs/ttl.pxx.com.log;
error_log /www/wwwlogs/ttl.pxx.com.error.log;
}
4.安装supervisor进程守护
安装supervisor在/www/supervisord目录下
$ pip install supervisor
$ echo_supervisord_conf > supervisord.conf # 生成 supervisor 默认配置文件
$ vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 进程管理
线上文件如下
; 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=/www/supervisord/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=/www/supervisord/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=/www/supervisord/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=/tmp ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp ; ('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:///www/supervisord/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=/tmp ; 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)
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (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=/tmp ; 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)
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (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_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 ; # 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 = relative/directory/*.ini
[include]
files = /www/supervisord/conf/*.conf
在/www/supervisord/conf/下创建文件ttl.conf
[program:ttl]
command=/www/wwwroot/api_ttl_pxx_com/venv/bin/gunicorn -w4 -b 127.0.0.1:8000 manager:app ;supervisor启动命令
directory=/www/wwwroot/api_ttl_pxx_com ; 项目的文件夹路径
startsecs=0 ; 启动时间
stopwaitsecs=0 ; 终止等待时间
autostart=true ; 是否自动启动
autorestart=true ; 是否自动重启
stdout_logfile=/www/wwwlogs/ttl.log ; log 日志
stderr_logfile=/www/wwwlogs/ttl.err ; 错误日志
使用 supervsior 启动 gunicorn
$ sudo supervisord -c supervisord.conf
$ sudo supervisorctl start ttl
然后接着查看/etc/rc.local的权限
[root@openstack ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 1月 29 10:45 /etc/rc.local -> rc.d/rc.local
[root@openstack ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 1月 25 23:52 /etc/rc.d/rc.local
/etc/rc.d/rc.local没有执行权限,于是按说明的内容执行:
[root@openstack ~]# chmod +x /etc/rc.d/rc.local
[root@openstack ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 1月 25 23:52 /etc/rc.d/rc.local
在开机文件rc.loacl中添加开机启动
supervisord -c /www/supervisord/supervisord.conf
superviosrd命令
查看单个任务状态: supervisorctl status 服务名
# supervisorctl status sparkportal
sparkportal RUNNING pid 26073, uptime 1 day, 23:12:10
启动/停止/重启任务
启动任务
supervisorctl start 服务名
# supervisorctl stop sparkportal
sparkportal: stopped
#supervisorctl status sparkportal
sparkportal STOPPED Jan 05 01:59 PM
停止任务
supervisorctl stop 服务名
# supervisorctl start sparkportal
sparkportal: started
# supervisorctl status sparkportal
sparkportal RUNNING pid 32207, uptime 0:00:05
重启任务
supervisorctl restart 服务名
# supervisorctl restart sparkportal
sparkportal: stopped
sparkportal: started
# supervisorctl status sparkportal
查看gunicorn状态
pstree -ap |grep gunicorn
重启进程
kill -HUP 进程编号
kill -HUP 2072
网友评论