flask部署
ssh连接服务器
-
putty连接服务器(大家应该都知道了)
-
ssh保持长时间连接不断
- 编辑/etc/ssh/sshd_config,添加配置项:
-
ClientAliveInterval 600 ClientAliveCountMax 10
- ClientAliveInterval 600 表示每600秒发送一次请求, 从而保持连接。
ClientAliveCountMax 10 表示服务器发出请求后客户端没有响应的次数达到10次,就自动断开连接。
则无响应的SSH客户端将在大约600x10=6000秒后断开连接。
查看系统版本
-
查看版本
cat /etc/redhat-release
显示:
CentOS Linux release 7.0.1406 (Core)
可以看到版本是7.0.1406
-
查看系统是32位或者64位
64-bit
显示:
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0xec333a104e045327c5e3d0ca6dda16c610a210f3, stripped
可以看到 ELF 64-bit LSB 所以该系统为64位。
安装宝塔版面
-
安装对应版本宝塔版本。
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh
显示:
外网面板地址: http://111.67.201.147:8888/9a18d317
内网面板地址: http://111.67.201.147:8888/9a18d317
username: tqczxnrp
password: 005edfb2
If you cannot access the panel,release the following panel port [8888] in the security group若无法访问面板,请检查防火墙/安全组是否有放行面板[8888]端口
-
登录宝塔面板。
- 我可以直接登录 证明我服务器的8888端口是打开的。(自己进不去的,可能是服务器的安全端口没有开放)
- 开设一个站点
python3安装
which python
mkdir /usr/local/python3
cd /usr/local/python3
安装依赖
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
安装python3(3.6.2)
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
蓝奏云下载:https://www.lanzous.com/i7x8kxg 密码:3bhk
tar -xvJf Python-3.6.2.tar.xz
cd Python-3.6.2
./configure --prefix=/usr/local/python3
make && make install
创建软链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
搭建web环境
- 创建虚拟环境
- 安装flask
- 安装和配置uwsgi
- 配置nginx
pip3 install --upgrade virtualenv
mkdir test
cd test
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
virtualenv -p python3 .env
进入虚拟环境安装flask
source .env/bin/activate
pip3 install flask
deactivate
- 写一个简单的测试程序test.py
from flask import Flask
app = Flask(__name__)
@app.route("/helloWorld")
def helloWorld():
return "Hello World"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8787, debug=True)
-
试一下这个网站是否可以使用
-
安装和配置uwsgi
pip3 install uwsgi find / -name uwsgi ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
如果安装失败的话就安装一下
python3.6-dev
-
创建uwsgi.ini配置文件
[uwsgi] chdir=/www/wwwroot/test # 工程目录 home=/www/wwwroot/test/.env # 虚拟环境目录 module=test # 启动flask应用的文件名,不用加.py callable=app # 应用名,与我们hell master=true processes=2 # worker的进程个数 chmod-socket=666 logfile-chmod=644 procname-prefix-spaced=test # uwsgi的进程名称前缀,启动后可通过ps -ef | grep test查找到这个进程 py-autoreload=1 #py文件修改,自动加载,也就是设置热启动了 #http=0.0.0.0:8080 #监听端口,测试时使用 vacuum=true # 退出uwsgi是否清理中间文件,包含pid、sock和status文件 socket=%(chdir)/uwsgi/uwsgi.sock # socket文件,配置nginx时候使用 stats=%(chdir)/uwsgi/uwsgi.status # status文件,可以查看uwsgi的运行状态 pidfile=%(chdir)/uwsgi/uwsgi.pid # pid文件,通过该文件可以控制uwsgi的重启和停止 daemonize=%(chdir)/uwsgi/uwsgi.log # 设置后台模式,然后将日志输出到uwsgi.log。当调试时,可先注释掉此内容 *************************************************************************** ********************************************************************************** [uwsgi] chdir=/www/wwwroot/test home=/www/wwwroot/test/.env module=test callable=app master=true processes=2 chmod-socket=666 logfile-chmod=644 procname-prefix-spaced=test py-autoreload=1 #http=0.0.0.0:8080 vacuum=true socket=%(chdir)/uwsgi/uwsgi.sock stats=%(chdir)/uwsgi/uwsgi.status pidfile=%(chdir)/uwsgi/uwsgi.pid daemonize=%(chdir)/uwsgi/uwsgi.log
-
手动创建uwsgi运行所需要的文件
mkdir uwsgi cd uwsgi vi uwsgi.pid vi uwsgi.sock vi uwsgi.status vi uwsgi.log
-
uwsgi 常用命令:
uwsgi --ini uwsgi.ini # 启动 uwsgi --reload uwsgi.pid # 重启 uwsgi --stop uwsgi.pid # 关闭
-
配置nginx
location / { include uwsgi_params; uwsgi_pass unix:/www/wwwroot/test/uwsgi/uwsgi.sock; }
git
- 下载一个项目进行部署
git clone https://github.com/greyli/sayhello.git
- 在git出现问题的时候 自行解决一下
git config --global --unset https.proxy
flask启动
`flask run --host 0.0.0.0 `