美文网首页
nginx-gunicorn-flask 服务器部署的那些坑

nginx-gunicorn-flask 服务器部署的那些坑

作者: 股金杂谈 | 来源:发表于2017-04-18 12:09 被阅读2571次

以下所有操作均在ubuntu操作系统中进行

sudo apt-get update

sudo apt-get upgrade

首先停止tomcat7的运行不然后占用80端口导致nginx无法运行

sudo service tomcat7 stop

全局安装的工具
1.安装mongoDB
Install MongoDB Community Edition on Ubuntu
安装之后建议停掉mongoDB的进程, 重新启动一次, 防止下次启动时报下面这个错误

mongodb exception in initAndListen: 29 Data directory /data/db not found

报上面那个错误的原因是, 没有用指定数据库路径的命令启动, 如果用下面的命令启动, 则不会报上面那个错误

 mongod --dbpath /var/lib/mongodb --auth

或者 用指定的配置文件启动数据库, 也不会报上面那个错误

mongod --config /etc/mongod.conf

如果报上面那个错误则, 使用

#用root权限创建db文件
sudo mkdir -p /data/db

启动数据库

mongod

mongoDB的使用
创建超级管理员

mongo
use admin
db.createUser({user:'root',pwd:'root',roles:[{ "role" : "root", "db" : "admin"}]});
exit

关闭数据库

mongod  --shutdown  --dbpath /database/mongodb/data/

用验证模式启动数据库

mongod --auth

创建es_testing_db数据库
可以在本地客户端创建

use es_testing_db 使用es_testing_db数据库, 没有会自动创建

db.createUser({user:"superLi",pwd:"xxxxxx",roles:[{role:"dbOwner",db:"es_testing_db"}]})

创建用户
退出mongo交互
exit

mongoDB后台运行, daemon简单的是命令后面加“&”

mongod --config /etc/mongod.conf --auth & 

获取Document的_id

pk_dict = order._object_key
            object_id = pk_dict['pk']
            orderInfo["order_id"] = str(object_id)

更新数据

task = 'task 1'
todo = Todo.objects(task=task).first()  # 先查找
if not todo:
    return "the task doesn't exist!"

todo.update(is_completed=True)   # 再更新

2.安装git

sudo apt-get install git

3.安装nginx服务器
阿里云服务器有时候已经安装好了, 自带了nginx服务器
查找nginx配置文件

find / -name nginx.conf

删除阿里自带的nginx php 和Tomcat7

sudo service nginx stop
或者
pgrep nginx
kill 1127
然后
cd /alidata
sudo rm -rf server/
sudo apt-get install nginx

运行nginx服务器

sudo /etc/init.d/nginx start

实时查看nginx日志更新

tail -f EnShengServerError.log

4.安装supervisor

sudo apt-get install supervisor

5.安装pip

sudo apt-get install python-pip

5.安装python开发环境, 不然pycrypto无法安装

sudo apt-get install python-dev

7.安装virtualenv

sudo pip install virtualenv virtualenvwrapper

配置virtualenv
不管python是什么版本,都要执行下面两句:

echo "export WORKON_HOME=~/Env" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

可以重启系统激活,也可以运行:

source ~/.bashrc

然后就可以开始建立虚拟环境了

8.下载代码

git clone https://xxxxxxxxxxxx

进入项目目录
创建虚拟环境

virtualenv venv

激活虚拟环境

source venv/bin/activate

在虚拟环境里执行
9..执行

pip install -r requirements.txt

的时候提示'pycrypto'安装失败
原来是因为, 需要先安装python-dev
执行

sudo apt-get install python-dev

后在执行pip install pycrypto,pycrypto安装成功

10.配置nginx服务器
cd /etc/nginx/sites-available
vim enshengserver

server {
    listen 80;

    location /static {
        alias /home/enshengserver/static;
    }

    location / {
        proxy_pass http://127.0.0.1:9000;
    }
}

https版nginx配置

server {
    listen 443 ssl;
    server_name misite.ltd;

    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    location /static {
        alias /home/enshengserver/static;
    }

    location / {
        proxy_pass http://127.0.0.1:9000;
    }
}

任意http跳转https的配置

server {
    listen *:80;

    return 301 https://www.your_domain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name www.your_domain.com;

    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    location /static {
        alias /home/static;
    }

    location / {
        root /home/webSite/;
    }

    location /en_sheng_api {
        proxy_pass http://127.0.0.1:9000;
    }
}

创建配置文件的软连接

sudo ln -s /etc/nginx/sites-available/enshengserver /etc/nginx/sites-enabled/

重新加载nginx配置文件

sudo service nginx reload

2.supervisor的使用
编写配置文件
cd /etc/supervisor
cd conf.d/
sudo vim app.conf

[program:EnShengServer]#应用程序名称
autostart = True #在 supervisord 启动的时候也自动启动
autorestart = True #程序异常退出后自动重启
environment = PATH = "/home/enshengserver/venv/bin" #可以通过 environment 来添加需要的环境变量,一种常见的用法是使用指定的 virtualenv 环境
command = python EnShengServer.py #启动命令,与手动在命令行启动的命令是一样的
directory = /home/enshengserver #程序的启动目录
stderr_logfile = /var/log/supervisor/EnShengServerError.log #错误日志输出路径
stdout_logfile = /var/log/supervisor/EnShengServer.log #日志输出路径

注意:

directory不能放在command上面 否则无法启动
用虚拟环境启动也可以写成

command = /home/enshengserver/venv/bin/gunicorn -b 127.0.0.1:9000 EnShengServer:
app

ubuntu查看进程指令

sudo netstat -antup

查看指定程序的进程

pgrep nginx

杀掉进程

kill 1313

启动supervisor
sudo service supervisor startsupervisorctl reload重新加载配置
进入supervisor控制台

sudo supervisorctl

status查看进程状态
stop EnShengServer停止程序运行
start EnShengServer运行程序
3.mongoDB的安装
参考Ubuntu16.04安装mongodb
ubuntu 12.04 Server 上安装 MongoDB及运行
ubuntu12.04上的mongodb卸载

4.Nginx https配置的关键是监听443端口

运维方案的实现
fabric安装
Mac下安装fabric

brew install fabric

ubuntu安装fabric

pip install fabric

5.自签名证书crt到cer格式转化

openssl x509 -in nginx.crt -out misite.cer -outform der

6.flask-RESTFul 接收数组参数

orderPostParser.add_argument('validate_car_images[]', type=unicode, required=True, help='验车照片不能为空', action='append') #重点在action='append'

相关文章

  • nginx-gunicorn-flask 服务器部署的那些坑

    以下所有操作均在ubuntu操作系统中进行 首先停止tomcat7的运行不然后占用80端口导致nginx无法运行 ...

  • Docker danriti/nginx-gunicorn-fl

    现成的镜像,已经配置好nginx-gunicorn-flask,可直接部署flask 项目 安装镜像 如果默认源比...

  • 服务器nacos-docker集群部署

    服务器nacos-docker集群部署(阿里云服务器) 首先写贴上配置文件,与具体的操作,后面再叙述遇到的坑 部署...

  • 项目部署(一):防火墙端口开放、配置

    摘要:因为项目开发工作接近尾声,需要将项目部署在服务器上,之前也没怎么玩过服务器,在部署过程中也遇到些坑,在此记录...

  • Gitlab CI 自动部署vue前端项目

    最近用到了gitlab自动部署项目,折腾了很久,踩了不少坑,这里记录一下。 gitlab是现成的,部署程序的服务器...

  • WebApi调试

    这些天在搞 WebApi ,踩了不少坑。总结下经验教训:1、如果WebApi部署在本机上正常,但是部署到服务器就不...

  • Windows Server 2008 R2 IIS部署Flas

    介绍 大家都知道服务器使用linux部署的好,但总有一些例外。 这次部署,看了很多的教程,但还是遇到很多坑。所以写...

  • Web项目部署踩坑之websocket

    今天上午给客户部署系统的时候,居然一不小心踩坑了。因为服务器的部分环境部署我安装的,所以有点坑。我们在项目中使用w...

  • node 环境配置

    总结一下本地 Vue 项目部署到 阿里云服务器 的步骤和遇到的坑。1、服务器购买 服务器购买可以选择国内和国外的服...

  • Flask启动app.run(host='0.0.0.0',po

    这段时间用Flask写了一个小项目, 尝试部署到阿里云服务器上,本来项目并不复杂,可是部署的时候却踩了很多坑。通过...

网友评论

      本文标题:nginx-gunicorn-flask 服务器部署的那些坑

      本文链接:https://www.haomeiwen.com/subject/ersfzttx.html