nginx uwsgi flask相关配置

Posted Kuai Jiajin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx uwsgi flask相关配置相关的知识,希望对你有一定的参考价值。

一、安装nginx

在 /home/download下下载压缩包
wget https://nginx.org/download/nginx-1.12.2.tar.gz
解压缩
tar zxvf nginx-1.12.2.tar.gz
添加环境变量:

vim ~/.bashrc

//添加
#NGINX
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

source ~/.bashrc//激活

一些nginx相关命令

nginx -s stop //重启nginx
nginx //启动
pkill -9 nginx //强制停止

修改nginx配置

//路径
vim /usr/local/nginx//conf/nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
    #                  ‘$status $body_bytes_sent "$http_referer" ‘
    #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  40.125.161.3;

        #charset koi8-r;

        access_log  /home/Web/JulyNovel/logs/access.log;
        error_log /home/Web/JulyNovel/logs/error.log;

        location / {
            include     uwsgi_params;
            uwsgi_pass  localhost:5000;
            uwsgi_param UWSGI_PYHOME    /root/anaconda3/envs/WebServer;
            uwsgi_param UWSGI_CHDIR     /home/Web/JulyNovel;
            uwsgi_param UWSGI_SCRIPT    manage:app;
        }

        #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   html;
        }

二、uwsgi安装

先安装uwsgi
pip install uwsgi
在你的项目根目录下创建一个配置文件uwsgiconfig.ini(uwsgi支持多种配置文件格式,xml,ini,json等)

[uwsgi]

socket = 127.0.0.1:8001     //启动程序时所使用的地址和端口,通常在本地运行flask项目,
                            //地址和端口是127.0.0.1:5000,
                            //不过在服务器上是通过uwsgi设置端口,通过uwsgi来启动项目,
                            //也就是说启动了uwsgi,也就启动了项目。
chdir = /home/www/     //项目目录

wsgi-file = manage.py      //flask程序的启动文件,通常在本地是通过运行  
                           //      python manage.py runserver 来启动项目的

callable = app      //程序内启用的application变量名

processes = 4     //处理器个数

threads = 2     //线程个数

stats = 127.0.0.1:9191      //获取uwsgi统计信息的服务地址

启动、停止uwsgi
前提:1yum install psmisc
新建manage_uwsgi.sh
sh manage_uwsgi.sh stop

#!/bin/bash
if [ ! -n "$1" ]
then
    echo "Usages: sh uwsgiserver.sh [start|stop|restart]"
    exit 0
fi

if [ $1 = start ]
then
    psid=`ps aux | grep "uwsgi" | grep -v "grep" | wc -l`
    if [ $psid -gt 4 ]
    then
        echo "uwsgi is running!"
        exit 0
    else
        uwsgi /etc/uwsgi.ini
        echo "Start uwsgi service [OK]"
    fi
    

elif [ $1 = stop ];then
    killall -9 uwsgi
    echo "Stop uwsgi service [OK]"
elif [ $1 = restart ];then
    killall -9 uwsgi
    /usr/bin/uwsgi --ini /etc/uwsgi.ini
    echo "Restart uwsgi service [OK]"

else
    echo "Usages: sh uwsgiserver.sh [start|stop|restart]"
fi

保存配置文件,我们可以通过键入 uwsgi uwsgiconfig.ini 来启动uwsgi。

三、anaconda3配置

在 /home/download下下载压缩包
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
·bash Anaconda3-5.1.0-Linux-x86_64.sh·

常用命令

conda env list //
conda create  --name //
source activate <evn-name>

Anaconda详细教程

四、前往我的Github下载源码

JulyNovel-Github

五、启动程序,玩起来吧

localhost:5000
配置好MariaDB和Redis前提下:
localhost:5000/missionStart
配置好MariaDB前提下:
localhost:5000/graphql
















以上是关于nginx uwsgi flask相关配置的主要内容,如果未能解决你的问题,请参考以下文章

记录一下在ubuntu 上配置nginx+uwsgi+flask出现的一点问题.

Flask+uwsgi+nginx 部署

使用flask和flask-socketio配置nginx、uwsgi

Ubuntu+Nginx+uWSGI+Flask应用

nginx+uwsgi+flask 服务器配置

flask项目下的uwsgi配置方式及示例