1. 程式人生 > >django + gunicorn+ nginx部署

django + gunicorn+ nginx部署

部署環境: python3.6 + 騰訊雲伺服器

開始用uwsgi部署,不太通用,坑賊多,阻礙了很長時間。最後還是選擇了gunicorn。

  1. 安裝django,編寫程式碼,除錯執行通過。
    ip:port訪問成功
    在這裡插入圖片描述

  2. 安裝gunicorn & 配置gunicorn

  • 安裝gunicorn
 pip install gunicorn
  • 設定gunicorn的軟連線
 ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn
  • project_name/gunicorn-config.py(與setting.py同級目錄)
import multiprocessing

bind = ["0.0.0.0:8199"]    # 與nginx配置的埠一致
chdir = "/usr/fin/test_blog/project_name"
timeout = 30
errorlog = "/usr/fin/test_blog/project_name/logs/error.log"
#accesslog = "/usr/fin/test_blog/project_name/logs/access.log"
#loglevel = 'debug'
proc_name = 'project_name'      # 工程名

keepalive = 6
timeout = 65
graceful_timeout = 10
worker_connections = 100

  • 啟動gunicorn
方法一:
gunicorn -c project_name/gunicorn-config.py project_name.wsgi

方法二:
gunicorn --bind 0.0.0.0:8199 project_name.wsgi:application

啟動成功後,http://123.207.255.182:8199/index/可以訪問成功,如上圖所示。

  1. 配置nginx
  • /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /usr/fin/nginx/logs/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    # include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  123.207.255.182;
        root         /usr/fin/nginx/html;

        # Load configuration files for the default server block.
        # include /etc/nginx/default.d/*.conf;

        location / {
            include uwsgi_params;
            proxy_pass http://123.207.255.182:8199;
            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-Forwarded-Proto $scheme;
            index index.html index.htm;
        }

        location /static {
            alias /usr/fin/test_blog/project_name/static/;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
  • 檢測nginx配置檔案的語法
/etc/nginx/nginx.conf
  • 啟動nginx
service nginx restart 
  • 通過nginx訪問django

在這裡插入圖片描述