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

django+uwsgi+nginx+sqlite3部署

cli ups proc .py nginx 配置 char 端口 true 進程

一:項目(github)

ssh root@server ip        # 連接你的服務器

git clone -b https://mygithub.com # -b 指定分支

安裝virtualenv及配置環境變量 詳細見:https://www.cnblogs.com/tangpg/p/8458233.html

mkvirtualenv envname -p python3 # 創建項目使用的python版本的虛擬環境,創建成功默認回進入該虛擬環境
pip install -r requirements.txt  # 安裝項目依賴
python manage.py runserver  #
保證項目能夠運行

note:!!!

使用數據庫為sqlite3可能會出現如下錯誤:
django.db.utils.NotSupportedError: URIs not supported
應該修改根據錯誤提示的路徑下的base.py文件
if self.features.can_share_in_memory_db:
            kwargs.update({uri: True})  # 將此處的True改為False
        return kwargs

二:配置uwsgi

pip install uwsgi # 安裝

[uwsgi]
# 取決於nginx配置 upstream
socket = 127.0.0.1:9000 socket = 127.0.0.1:9001 # 項目根目錄 chdir = /home/jason/projectName# Django的wsgi文件 module = projectName.wsgi # Python虛擬環境的路徑 home = /home/user/.virtualenvs/envname/bin/python # 進程相關的設置 # 主進程 master = true # 最大數量的工作進程 processes = 10 # 設置socket的權限
chmod-socket = 666 # 退出的時候是否清理環境 vacuum = true # 靜態文件 絕對地址 static-map = /static=/home/user/projectName/static

三:nginx 配置

安裝nginx 詳情:https://www.cnblogs.com/tangpg/p/8962773.html

mkdir conf.d  # 在nginx.conf目錄下創建文件
cd conf.d  # 進入
touch project.conf  # 創建nginx配置文件,配置此項目的配置信息
在nginx.conf中的http塊的最後面, include usr/local/nginx/conf/conf.d/project.conf # 將該配置文件加載到nginx.conf配置中
nginx -s reload # 重啟nginx服務
upstream anyname {
    server 127.0.0.1:9000;  # 兩臺服務器 與 wsgi配置相關
    server 127.0.0.1:9001;
}

# 配置服務器
server {
    # 監聽的端口號,通過服務器ip監聽的端口
    listen      80;
    # 域名
    server_name your server ip;
    charset     utf-8;

    # 最大的文件上傳尺寸
    client_max_body_size 75M;

    # 靜態文件訪問的url, nginx再次加載靜態文件
    location /static {
        # 靜態文件地址
        alias /home/user/projectName/static;
    }

    # 最後,發送所有非靜態文件請求到django服務器
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://anyname;  # upstream anyname 
            break;
        }
    }
}

django+uwsgi+nginx+sqlite3部署