nginx uwsgi flask相關配置

  • 2019 年 10 月 5 日
  • 筆記

一、安裝Nginx

在 /home/download下下載壓縮包 wget https://nginx.org/download/nginx-1.12.2.tar.gz 解壓縮

tar zxvf nginx-1.12.2.tar.gz    ./configure  make  make install

添加環境變數:

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  xxx.xxx.xxx.xxx;            #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 前提:yum 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.0.1-Linux-x86_64.sh ·bash Anaconda3-5.0.1-Linux-x86_64.sh·

常用命令

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

Anaconda詳細教程

四、前往我的Github下載源碼

JulyNovel-Github

五、啟動程式,玩起來吧

localhost:5000 主從共四台伺服器配置好Python uWSGI Nginx環境後,額外為主伺服器配置MariaDB和Redis環境: 別忘了差異化配置config.py 好了,可以開始愉快地Spider了 xxx.xxx.xxx.xx/missionStart

順便,任何一台伺服器都可以訪問Graphql介面測試文檔: xxx.xxx.xxx.xx/graphql