[该文章只涉及个人部署的简单流程,读者可通过其它途径了解详细部署流程]
依个人部署项目可预先安装好需要的环境,这里已提前安装好LNMP环境
1.安装Python环境
安装virtualenv环境
配置环境变量
配置虚拟环境保存的路径,执行sh文件生成 mkvirtualenv 等命令
source virtualenvwrapper.sh
2.配置Git
yum install git
将远程仓库拉去到本地
3.安装项目依赖包
pip install -i https://pypi.douban.com/simple/ -r requirements.txt
4.修改项目配置文件
config 数据库连接配置、默认路径等
数据库迁移,生成数据库迁移脚本,实现模型 <===> 数据库表 之间的映射
尝试启动服务
5.安装和使用uwsgi 对应的应用服务:gunicorn
是一个应用服务器,非静态文件的网络请求必须通过该应用完成,当然也可以充当静态文件服务器(不推荐使用,而推荐使用Nginx)
uwsgi是用Python编写,因此可通过 pip install uwsgi 安装,且必须安装在系统级别的Python的环境中
*****通过 uwsgi 部署项目时 请求流程
Client <===> Nginx <===> uwsgi <===> Django/Flask等服务
尝试启动:
uwsgi --http :8080 --module bbs.wsgi --virtualenv=/applications/python/env/bbs-env
编写配置文件
[uwsgi]
# 服务器上是通过uwsgi来启动项目,也就是说启动了uwsgi,也就启动了项目
socket=127.0.0.1:8001
# 项目目录
chdir=/applications/python/bbs
# Python 虚拟环境的路径
home=/applications/python/env/bbs-env
# flask程序的启动文件,通常在本地是通过运行 python manage.py runserver 来启动项目的
wsgi-file=/applications/python/bbs/server.py
# 程序内启用的application变量名
callable=app
http = :9001
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 设置socket权限
chmod-socket=666
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
6.安装和使用Nginx
作用:
动静分离 (静态资源:js/cs/图片等文件)
反向代理
负载均衡
安装以及配置好环境变量,并这是Nginx开机自启动
配置Nginx配置文件并且启动
7.supervisor配置
管理uwsgi,可在uwsgi发生意外时自动重启应用服务
安装,且必须安装在系统级别的Python的环境中 pip3 install git+https://github.com/Supervisor/supervisor
创建配置文件 bbssupervisor.conf
[program:bbs]
command = uwsgi --ini /applications/conf/uwsgi/flask.bbs.ini
directory = /applications/python/bbs
startsecs = 0
stopwaitsecs = 0
autostart = true
autorestart = true
stdout_logfile = /applications/python/bbs/bbssupervisor.log
stderr_logfile = /applications/python/bbs/bbssupervisor.err
[supervisord]
loglevel = info
[inet_http_server]
port = :9001
username = admin
password = admin123
# 配置通过 supervisorctl 管理的配置项
[supervisorctl]
serverurl = http://127.0.0.1:9001
username = admin
password = admin123
# 必须指定的 查看官方文档
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
通过 supervisord 启动 uwsgi
supervisord -c bbssupervisor.conf
通过以上启动了supervisor之后,可使用supervisorctl 管理 supervisor
supervisorctl -c bbssupervisor.conf
管理台常用命令:
status
start pargram_name
restart pargram_name
reload
quit
001 简单压力测试: ab Apache旗下的压力测试工具
yum install httpd-tools -y
[root@AL~]# ab -n 100000 -c 100 url
一次一百个请求
ab -n 1000 -c 100 http://47.101.180.183/
002 安装 supervisor 出错:
[root@AL bbs]# pip3 install supervisor
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting supervisor
Downloading http://mirrors.aliyun.com/pypi/packages/ba/65/92575a8757ed576beaee59251f64a3287bde82bdc03964b89df9e1d29e1b/supervisor-3.3.5.tar.gz (421kB)
100% |████████████████████████████████| 430kB 19.7MB/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.5 (default, Mar 16 2019, 12:35:52)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]. Please install using a supported version.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-o5f3s_ep/supervisor/
解决:
pip3 install git+https://github.com/Supervisor/supervisor