1. 程式人生 > 實用技巧 >Django部署到伺服器

Django部署到伺服器

以下都是ubuntu系統的環境

233沒有一連串的具體操作只有需要記的命令~

1.安裝python依賴

#啟用虛擬環境
source env/bin/activate

#1.通過requirements檔案來直接安裝所有的環境依賴
pip3 install -r requirements.txt
#2.收集所有的靜態檔案
python manage.py collectstatic
#3.資料庫配置略~

2.nginx配置

相關nginx指令

#檢視當前nginx服務狀態
systemctl status nginx.service
#啟動nginx
sudo /etc/init.d/nginx start
#重啟nginx
service nginx restart

#4.移動到.conf生效的資料夾下
cd /etc/nginx/sites-enabled
#5.新建一個配置檔案配置下面的資訊(如果有原來佔用80埠的配置檔案那就把他卡擦了
vim mysite.conf

mysite.conf配置

server_name為域名或者伺服器名, location /static 裡邊的alias後接Django的靜態檔案目錄

server {
    charset utf-8;
    listen 80;
    server_name moon-ice.com;

    location /static {
        alias /home/ubuntu/django-blog/statics; 
    }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/moon-ice.com.socket;
    }
}

3.gunicorn啟動應用

pip3 install gunicorn
#啟動應用
gunicorn --bind unix:/tmp/nanfeng.site.socket blogproject.wsgi:application
#6.啟動應用並在後臺執行輸出到nohup檔案中
nohup gunicorn --bind unix:/tmp/nanfeng.site.socket blogproject.wsgi:application &