1.Virtualenv
安装virtualenv
pip install virtualenv
创建虚拟环境
virtualenv venv
进入虚拟环境
source venv/bin/activate
之后再虚拟环境中安装相应的依赖
2.uswgi
安装uwsgi
pip install uwsgi
创建一个配置文件,uwsgi.ini
[uwsgi]
#启动的python路径
wsgi-file=/path/app.py
socket = 127.0.0.1:5000
#项目所在目录
chdir = /path/dir
#启动的文件名称
wsgi-file = app.py
callable = app
#处理器个数
processes = 1
#线程个数
threads = 2
stats = 127.0.0.1:9191
#若每次请求需要花费超过该值的时间则放弃该请求处理相应的worker被收回
#harakiri = 1200
~
启动uwsgi
uwsgi uwsgi.ini
或者
uwsgi -ini uwsgi.ini
3.nginx
安装nginx
#centos
yum install nginx
#ubuntu
apt-get install nginx
修改nginx的配置文件
vi /etc/nginx/nginx.conf
增加/修改以下内容
server {
listen 80;
listen x.x.x.x; #ip地址
listen 443; #https端口
server_name www.server.com; #域名
#下面是https配置
ssl on;
ssl_certificate 1_server.com_bundle.crt;
ssl_certificate_key 2_server.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照>这个套件配置
ssl_prefer_server_ciphers on;
location / {
#uwsgi配置
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /peth/venv;
uwsgi_param UWSGI_CHDIR /path/dir
uwsgi_param UWSGI_SCRIPT manage:app;
#root /usr/share/nginx/html;
}
检查nginx配置文件是否有错误
nginx -t
重启nginx
service nginx restart
4.supervisor
安装supervisor
apt-get install supervisor
或者
pip install supervisor
创建配置文件
echo_supervisord_conf > supervisor.conf
修改配置文件
#去掉前面的分号
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
#xxx 改为自己的应用名称
#command为启动程序执行的命令
[program:xxx]
command=uwsgi -ini uwsgi.ini ; 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)
;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=true ; 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 (0 means none, 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=~/log/ai_server.log ; 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 (0 means none, 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)
如果希望结束进程的时候把子进程也杀掉,那么需要设置stopasgroup为true。
之后启动supervisor
supervisord -c supervisor.conf
启动之后如果修改配置文件,需要更新
supervisorctl update
对进程进行管理
#开启
supervisorctl start xxx
#结束
supervisorctl stop xxx
#重启
supervisorctl restart xxx
查看进程状态
supervisorctl status
至此,基于centos环境下的venv+nwsgi+supervisor+nginx部署flask服务已经完成,也可以通过同样的方式部署Djengo等其他服务。