1. 程式人生 > >[學習筆記] Python 虛擬環境的安裝以及django專案部署到nginx伺服器

[學習筆記] Python 虛擬環境的安裝以及django專案部署到nginx伺服器

安裝Python虛擬環境

sudo apt install python3-pip python3-dev build-essential
sudo pip3 install --upgrade pip
sudo pip3 install virtualenv

MySQL資料庫以及sqlclient的安裝

sudo apt install mysql-server
sudo apt install libmysqlclient-dev

建立並激活一個虛擬環境

virtualenv env
source env/bin/active

在虛擬環境中安裝django flask scrapy 等Python框架

pip install mysqlclient
pip install django
pip install flask
pip install scrapy

退出虛擬環境

deactivate

Django專案部署到nginx伺服器的配置,建立單獨的nginx配置檔案加入以下內容,然後通過include方式加到nginx,conf配置檔案中

server{
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name www.wh14.club;
    ssl on;
    ssl_certificate /var/www/ssl/1_www.wh14.club_bundle.crt;
    ssl_certificate_key /var/www/ssl/2_www.wh14.club.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    charset utf-8;
    access_log /var/www/log/access_log;
    error_log /var/www/log/error_log;
    client_max_body_size 75M;
    location /static {
        alias /var/www/static;
    }
    location /{
        uwsgi_pass 127.0.0.1:8000;
        include uwsgi_params;
    }
}

安裝uwsgi,在專案中中新建uwsgi.ini 檔案

[uwsgi]

socket = 127.0.0.1:8000
chdir = /var/www
module = www.wsgi
master = true
processes = 2
threads = 2
max-requests = 6000
chmod-socket = 664
vacuum = true
;讓uwsgi在後臺執行
daemonize = /var/www/log/uwsgi.log
;編輯Python檔案後不需要重新啟動uwsgi
py-autoreload = 1 

結束uwsgi程序

sudo killall -9 uwsgi