美文网首页
Flask生产环境部署

Flask生产环境部署

作者: MononokeHime | 来源:发表于2018-06-14 08:40 被阅读0次

    本文介绍了Flask+gunicorn+nginx部署web项目生成环境

    1.安装Nginx,具体过程省略。安装完成后,最基本的配置文件会在/etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf

    Nginx启动时会加载nginx.conf,nginx.conf配置里面又定义了加载 default.conf,所以我们自定义default.conf就行了。

    server {
        listen       80;
        server_name  120.79.195.197;   #服务器的ip
     
    location / { 
                         proxy_pass http://127.0.0.1:8000;  #对应到gunicorn服务运行的端口
                         proxy_set_header Host $host; 
                         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
                 } 
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    

    2.在虚拟环境下安装gunicorn

    pip install gunicorn
    

    3.定义一个manga.py

     from flask import Flask  
       
     app = Flask(__name__)  
     
     if __name__ == '__main__':  
         app.run()  
      
     @app.route('/')  
     def index():  
         return '<html>Hello world!</html>'
    

    4.启动gunicorn

    gunicorn -w 4 -b 127.0.0.1:8000 manage:app
    
    • w 4表示启动了4个进程
    • manage表示要启动的flask入口文件
    • app表示manage里面创建的app对象

    用gunicorn启动django程序

    gunicorn project_name.wsgi:application --bind=127.0.0.1:8000 --daemon
    

    gunicorn参数:
    –bind指定侦听地址
    –daemon放到后台运行

    更好的启动方式
    4.1添加配置文件gunicorn.conf

    # 并行工作线程数
    workers = 4
    # 监听内网端口5000【按需要更改】
    bind = '127.0.0.1:8000'
    # 设置守护进程【关闭连接时,程序仍在运行】
    daemon = True
    # 设置超时时间120s,默认为30s。按自己的需求进行设置
    timeout = 120
    # 设置访问日志和错误信息日志路径
    # accesslog = './logs/acess.log'
    # errorlog = './logs/error.log'
    

    4.2运行程序

    # gunicorn 运行模块名:应用名 -c 配置文件
    gunicorn service:app -c gunicorn.conf
    

    5.Nginx重启

    nginx -s reload
    service nginx restart
    

    补充:supervisor进程管理
    supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,通过在配置文件中设置autostart=ture,可以实现对异常中断的子进程的自动重启。

    一般的项目部署,我们不会对Nginx进行进程管理,而是对uwsgi或者gunicorn进行管理。

    补充:关于sql的生成以及导入:
    数据库的导出

    mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql  #导出到当前目录下
    

    数据库的导入

    mysql>create database dbname ;
    mysql -u用户名 -p密码 数据库名 < 数据库名.sql
    

    相关文章

      网友评论

          本文标题:Flask生产环境部署

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