Ubuntu下部署Django項目


1.首先要進入本地環境把項目所需要的環境導出來

pip freeze >requirements.txt

2.安裝python3.6.8版本

3.安裝得到的requirements.txt 命令:pip3 install -r requirements.txt  全部安裝

如果遇到HTTPConnectionPool(host=''xx.xx.xx.xx', port=xx): Max retries exceeded with url: (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000237EE44EF98>: Failed to establish a new connection: [WinError 10060]錯誤

處理方法:https://www.cnblogs.com/erhangboke/p/11663057.html

4.修改項目的settings文件

DEBUG = False(線上的項目一定要修改成False)

ALLOWED_HOSTS = ["*"]

5.在django的settings文件中,添加下面一行內容:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

7.配置路由(貌似不是必須)

url(r'^static/(?P<path>.*)',serve,{"document_root":settings.STATIC_ROOT}),

6.進入項目目錄執行下面代碼

收集admin后台的靜態文件,如果不執行,將來訪問linux的mysite的admin/xadmin將沒有樣式:

python manage.py collectstatic 會提示 是否繼續這樣做? yes

7.進入linux通過cd切換到項目的目錄下 輸入下列命令可以運行證明已經完成了大部分操作

python3 manage.py runserver 0.0.0.0:8000 

本地訪問xxx:8000/xxx地址可以訪問成功,說明環境OK

 

8.完成了上一步發現當終止回話時,就不能在訪問了,下面就是部署終止客戶端會話亦可以訪問

9、安裝必備內容

  9.1安裝nginx

    sudo apt-get update

    apt-get install nginx(目錄在/etc/nginx下)

    鏈接到uwsgi的啟動配置文件中

    ln -s /etc/nginx/nginx.conf  /etc/nginx/conf.d/

    安裝完成后,使用service nginx start命令啟動后,本地訪問10.205.1.203,顯示welcome to nginx即安裝成功

    service nginx restart(start/status/stop/reload)

  9.2安裝SSH

    apt-get install openssh-server

  9.3安裝 uwsgi

    sudo apt-get install python3-setuptools

    sudo apt-get install python3-dev

    pip install uwsgi

 

10.在你的項目下/data/jenkins/mysite 創建一個uwsgi.ini的配置文件

[uwsgi]

#Django-related settings

#uwsgi這個服務監聽的是9001端口,如果nginx想要向uwsgi轉發請求,那么nginx必須使用9001端口發送請求。

socket = 127.0.0.1:9001

#項目絕對路徑(手動設置)

chdir           = /data/jenkins/mysite

#Django的wsgi文件相對路徑(不要寫絕對路徑,寫相對路徑,自己設置)

wsgi-file       =mysite/wsgi.py

#process-related settings

#master

master          = True

#最大進程數

processes       = 4

#線程數

threads         = 2

#設置此參數,有一個主進程

master=True

#守護進程的方式運行,log日志存在此log文件里

deamonize=/var/log/uwsgi/mysite.log

disable-logging = true

#主進程id寫入文件里

pidfile= /var/log/nginx/uwsgi.pid

#... with appropriate permissions - may be needed

#chmod-socket    = 664

#退出時,清理環境

vacuum          = True

reload-mercy    = 10

max-requests    = 5000

limit-as        = 512

buffer-size     = 30000

 

11. 輸入下面命令,查看nginx默認配置文件的目錄

nginx -t 

默認文件目錄為/etc/nginx/nginx.conf

然后cd到/etc/nginx/下

輸入vim.nginx.conf

可以看出會加載/etc/nginx/conf.d文件夾下的任意.conf結尾的文件

cd到etc/nginx/conf.d文件夾,然后ls發現里面沒有文件,所以需要自己新建文件dj_nginx.conf(文件名隨便起,.conf結尾就可以)##

 

12.在dj_nginx.conf ##輸入一下代碼.一定要記得改路徑

server {

    listen         8000; 

    server_name    0.0.0.0; 

    access_log      /var/log/nginx/mysite_access.log;

    error_log       /var/log/nginx/mysite_error.log;

    charset     utf-8;

    keepalive_timeout 65;

    client_max_body_size 75M;

    root /data/jenkins/mysite; #修改路徑

    location / { 

        include uwsgi_params;

        uwsgi_pass 127.0.0.1:9001;

        uwsgi_read_timeout 300; //這個時間可以設置長一寫,防止請求頁面出現504超時問題

    }   

    location /static/ {

        expires 30d;

        autoindex on; 

        add_header Cache-Control private;

        alias /data/jenkins/mysite/static/;#修改路徑

     }

 

    location /media/ {

        expires 30d;

        autoindex on; 

        add_header Cache-Control private;

        alias /data/jenkins/mysite/media/;#修改路徑

     }

 }

 

先用nginx -t查看是否有錯,有錯解決,沒錯重啟service nginx restart/reload

10.205.123::8000/xxx

 

最好在項目下創建start.sh的文件,內容uwsgi -i uwsgi.ini

或者命令行執行也可以

如果遇到:uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory錯誤

解決方法:https://www.cnblogs.com/erhangboke/p/11673156.html

 

netstat -apn | grep 8000/lsof -i :8000 查看8000端口是誰在使用

kill -9 pid號 強制關閉占用端口的程序(或者更換監聽的端口)

來個更強大的命令:ps -ef|grep uwsgi|awk {'print $2'}|xargs kill -9

 

問題來了:

部署完成后發現執行了uwsgi -i uwsgi.ini命令后沒有后台運行,停止了命令,項目仍然停止了

優化:

在項目中創建一個.sh.結尾的shell文件 start.sh

由於執行uwsgi -i uwsgi.ini會產生一些log內容,創建一個mysite.log文件,將產生的log打印到mysite.log(路徑可以自己定)

 

shell代碼:

#!/bin/bash

dir="/data/jenkins/workspace/RRD_mysite/logs/mysite.log"

if [ ! -d "$dir" ];then

 touch $dir

 echo "創建文件成功"

else

 echo "文件已經存在"

fi

nohup uwsgi -i uwsgi.ini >$dir 2>&1 &

 

 

可以用jenkins配合部署:shell命令:

#!/bin/bash

BUILD_ID=DONTKILLME

echo "kill PID!"

ps -ef|grep uwsgi|awk {'print $2'}|xargs kill -9

sleep 2

cd $WORKSPACE

rm -rf report20*

rm -rf __pycache__

rm -rf logs/mysite.log

chmod +x start.sh

echo "start service!"

./start.sh


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM