Django程序在Linux上的部署


Django程序在Linux上的部署

1.服務器搭建Python環境

1.1 安裝python3

  • 不過多的描述,請參考安裝文章,地址

1.2 創建虛擬環境

virtualenv envsname

1.3 代碼傳送到服務器

  • 可以使用 git 或者FZ

1.4 安裝第三方包

  • 發布上的代碼應該包含requirement.txt

    pip freeze > requirements.txt

  • 激活虛擬環境后執行,到包含requirement.txt目錄下:

    pip install -r requirements.txt

  • image-20211221174954989

1.5 遷移數據庫

  • python manage.py makemigrations

  • python manage.py migrate

  • 本博客只是做簡單發布的示例,因此暫時不做數據庫的遷移工作。注:如果服務器的數據庫支持遠程連接,亦可使用Navicat進行數據庫的遷移工作

1.6 測試

  • 查看一下django是否等夠正常啟動

  • image-20211221175227874

2.安裝Nginx

3.發布

3.1 簡單發布

  • 將程序在后台運行,並且將終端的輸出(打印)指向nohup.out文件
nohup python manage.py runserver 127.0.0.1:8000 &
  • 補充

    • 使用&命令后,作業被提交到后台運行,當前控制台沒有被占用,但是一但把當前控制台關掉(退出帳戶時),作業就會停止運行。nohup命令可以在你退出帳戶之后繼續運行相應的進程。nohup就是不掛起的意思( no hang up)。該命令的一般形式為:

      nohup command &
      
    • 默認都是輸出到nohup.out文件中,也可以修改指定的文件,這里不在過多描述,請參考這篇文章

  • 此時Django程序已經運行在后台

    image-20211221180824427

  • 使用nginx進行反向代理

  • # 修改nginx.conf的配置文件,
    server {
            listen       80;
            server_name  服務器IP;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                proxy_pass  http://127.0.0.1:8000/;#django服務啟動的地址
            }
    
  • 啟動nginx

  • cd sbin
    ./nginx
    
  • image-20211221181458562

  • 訪問成功

3.2 上線發布

  • 補充:nginx uwsgi wsgi django 這些東西究竟是什么關系.參考文獻

  • image-20211221183925202

1.安裝uwsgi

pip install uwsgi

image-20211221182355407

2.配置nginx.conf文件

# 新增加一個server配置,此過程繁多建議將文件下載下來修改后在傳到服務器上
server {
	listen 8000; #暴露給外部訪問的端口
	server_name localhost;
	charset utf-8;
	location / {
		include uwsgi_params;
		uwsgi_pass 127.0.0.1:8997; #外部訪問8996就轉發到內部8997
	}
	location /static/ {
		alias /data/pythoncode/dejangoTest/web/static/; #項目靜態路徑設置
	}
}

image-20211221190936489

3.使用uwsgi啟動django

django自帶的wsgiref 在調試模式下使用的wsgi的文件,網關接口,協議
uwsgi:協議
uWSGI:具體實現方式

  • uwsgi配置文件的格式

    conf
    py
    cnf
    xml
    json
    ini
    yaml

  • # uwsgi.ini file
    [uwsgi]
    
    # Django-related settings
    socket = :8000
    
    # the base directory (full path)
    chdir = /root/data/pythoncode/testcode/mypro
    
    # Django s wsgi file
    module = mypro.wsgi
    
    # process-related settings
    master = true
    
    # home = /root/data/envs/anydjango
    
    # maximum number of worker processes
    processes = 5
    
    #maximum number of worker threads
    threads = 5
    
    virtualenv=/root/data/envs/anydjango
    
    # try to remove all of the generated file/sockets
    vacuum = true
    
    # 使進程在后台運行,並將日志打到指定的日志文件或者udp服務器
    daemonize = /root/data/pythoncode/testcode/mypro/log/myuwsgi.log
    
  • 啟動

    uwsgi --http :8000 --module dejangoTest.wsgi
    
  • 關閉

    • killall uwsgi
      
  • image-20211221195350577

  • image-20211221195501290

  • 另一種啟動命令

    image-20211221200551420

4.設置靜態文件

SATAIC_ROOT=os.path.join(BASE_DIR,'web/static/')#由於我的靜態文件是放在應用里面的,因此我要在路徑前加上應用名

image-20211221192151565

# 執行命令
python manage.py collectstatic

image-20211221192827820

nginx中配置靜態文件;

server {
        listen       80;
        server_name  127.0.0.1;
        # charset UTF-8;

        #access_log  logs/host.access.log  main;

        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8000;
       }
	
		location /static {
			alias /www/res/statics;
		}
}

image-20220401152250453

  • 配置完成后啟動uwsgi

    uwsgi --ini uwsgi.ini
    
  • 重啟nginx

    ./nginx -s reload
    


免責聲明!

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



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