liunx 使用flask + nginx + gunicorn 部署项目

一、上传项目,用gunicorn启动项目

  • 1.生成requirements.txt文件,执行如下命令
pip freeze > requirements.txt
  • 2.上传flask项目
  • 3.安装项目依赖,首先创建虚拟环境
vituralenv flaskEnv  # 创建虚拟环境
cd flaskEnv/bin  # 进入到bin目录
source activate  #进入虚拟环境
pip install -r requirements.txt
  • 4.安装Gunicorn
pip install gunicorn
gunicorn -w 4 -b 127.0.0.1:8080 app:app 
# -w 指定进程数
# 第一个app是文件名,第二个app是文件内的注册对象

二、安装nginx

  • 1.安装nginx
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel  # 安装依赖
# 下载nginx-1.9.9.tar.gz
wget https://nginx.org/download/nginx-1.9.9.tar.gz
# 解压
tar -zxvf nginx-1.9.9.tar.gz -C /opt/module/
#进入nginx目录
cd nginx-1.9.9
# 配置安装
./configure --prefix=/usr/local/nginx
make
make install
  • 2.测试安装是否成功
# 进入到nginx目录
cd /usr/local/nginx/ 
./sbin/nginx -t

  • 3.启动nginx
cd sbin
# 启动
./nginx


在浏览器上输入本机ip

  • 4.配置nginx开机自启(可配可不配)
vim /etc/rc.d/rc.local

三、修改配置文件

  • 1.进入目录
cd /usr/local/nginx/conf/

  • 2.修改配置文件
[root@hdp-2 conf]# vim nginx.conf
修改server里面的内容
server {
        listen       80;
        server_name  localhost;

        location / {
	proxy_pass http://localhost:8000;
	proxy_redirect          off;
	proxy_set_header        Host                    $host;
	proxy_set_header        X-Real-IP               $remote_addr;
	proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
	proxy_set_header        X-Forwared-Proto        $scheme;
        }
  • 3.重新启动服务
 
停止nginx
 
nginx -s stop
 
重启nginx
 
nginx -s reload

猜你喜欢

转载自www.cnblogs.com/hziwei/p/12893773.html