Ubuntu16.04下Django項目的部署

起飛前的準備

# 首先在Ubuntu的當前用戶zhang下新建data文件夾,而後在data文件夾下新建你的項目目錄
root@zhang-virtual-machine:/home/zhang/data# mkdir my_web_project root@zhang-virtual-machine:/home/zhang/data# ls my_web_project root@zhang-virtual-machine:/home/zhang/data# cd my_web_project/

# virtualenv --no-site-paceages env 建立虛擬環境,--no-site-packages 表示一個乾淨的目錄,不會安裝不必的包 root@zhang-virtual-machine:/home/zhang/data/my_web_project# virtualenv --no-site-packages env Using base prefix '/usr' New python executable in /home/zhang/data/my_web_project/env/bin/python3 Also creating executable in /home/zhang/data/my_web_project/env/bin/python Installing setuptools, pip, wheel...done. root@zhang-virtual-machine:/home/zhang/data/my_web_project# ls env root@zhang-virtual-machine:/home/zhang/data/my_web_project# cd env/ root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# ls bin include lib pip-selfcheck.json

# 激活虛擬環境 root@zhang
-virtual-machine:/home/zhang/data/my_web_project/env# source bin/activate

#查看版本,查看到當前的環境下沒有太多的包 (env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip -V pip 10.0.0 from /home/zhang/data/my_web_project/env/lib/python3.5/site-packages/pip (python 3.5) (env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip list Package Version ---------- ------- pip 10.0.0 setuptools 39.0.1 wheel 0.31.0

# 退出虛擬環境 (env) root@zhang
-virtual-machine:/home/zhang/data/my_web_project/env# deactivate root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# source bin/activate
# 安裝1.10.4版本的Django
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip install django==1.10.4 Collecting django==1.10.4
# 安裝uwsgi
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip install uwsgi Collecting uwsgi

第一步:uwsgi

首先測試uwsgi,並用uwsgi啓動服務html

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello uwsgi!"]
剛開始按照網上說的用這個方法啓動總是報錯
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# python app.py 
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# uwsgi --http :8008 --wsfi-file app.py 
uwsgi: unrecognized option '--wsfi-file'
getopt_long() error

最後發現啓動的方式默認是socket的方式
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# uwsgi --http-socket :8008 --wsgi-file app.py 
*** Starting uWSGI 2.0.17 (64bit) on [Tue Apr 17 19:05:22 2018] ***
compiled with version: 5.4.0 20160609 on 18 April 2018 01:30:18
os: Linux-4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48 UTC 2017

pkill -f -9 uwsgi #中止uwsgi

第二步:nginx

# 直接在命令行下運行 apt-get install nginx
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# apt-get install nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
nginx的啓動與關閉
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# service nginx stop
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# ps aux | grep nginx
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# service nginx start
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# ps aux | grep nginx
root      55075  0.0  0.0 124968  1420 ?        Ss   19:24   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  55076  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
www-data  55077  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
www-data  55078  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
www-data  55079  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
root      55082  0.0  0.0  14220   988 pts/20   S+   19:24   0:00 grep --color=auto nginx
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# 

nginx的配置文件都在python

# /etc/nginx/...
(env) root@zhang-virtual-machine:/etc/nginx# ls
conf.d        fastcgi_params  koi-win     nginx.conf    scgi_params      sites-enabled  uwsgi_params
fastcgi.conf  koi-utf         mime.types  proxy_params  sites-available  snippets       win-utf

此時訪問瀏覽器能夠看到以下:nginx

第三步:Django+uwsgi+nginx

nginx的配置文件所在路徑:
/etc/nginx/nginx.conf
uwsgi 所在路徑:項目更目錄
啓動項目:
uwsgi --ini demo.ini &    #利用配置文件啓動,默認不在終端顯示
/etc/init.d/nginx start     #啓動nginx

附加:web

supervisor是一個對進程管理的軟件,能夠幫助咱們啓動uwsgi並維護(uwsgi進程關閉時,自動將其啓動起來)。django

安裝:apt-get install supervisor
配置 vim /etc/supervisor/supervisor.conf
[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
;http_port=127.0.0.1:9001  ; (alternately, ip_address:port specifies AF_INET)
;sockchmod=0700              ; AF_UNIX socketmode (AF_INET ignore, default 0700)
;sockchown=nobody.nogroup     ; AF_UNIX socket uid.gid owner (AF_INET ignores)
;umask=022                   ; (process file creation umask;default 022)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)

;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;http_username=user          ; (default is no username (open system))
;http_password=123           ; (default is no password (open system))
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;user=chrism                 ; (default is current user, required if root)
;directory=/tmp              ; (default is not to cd during start)
;environment=KEY=value       ; (key value pairs to add to environment)

[supervisorctl]
serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat            ; the program (relative uses PATH, can take args)
;priority=999                ; the relative start priority (default 999)
;autostart=true              ; start at supervisord start (default: true)
;autorestart=true            ; retstart at unexpected quit (default: true)
;startsecs=10                ; number of secs prog must stay running (def. 10)
;startretries=3              ; max # of serial start failures (default 3)
;exitcodes=0,2               ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT             ; signal used to kill process (default TERM)
;stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
;user=chrism                 ; setuid to this UNIX account to run the program
;log_stdout=true             ; if true, log program stdout (default true)
;log_stderr=true             ; if true, log program stderr (def false)
;logfile=/var/log/cat.log    ; child log path, use NONE for none; default AUTO
;logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
;logfile_backups=10          ; # of logfile backups (default 10)



[program:oldboy]
command=/usr/local/bin/uwsgi /data/oldboy/oldboy.ini ;命令
priority=999                ; 優先級(越小越優先)
autostart=true              ; supervisord啓動時,該程序也啓動
autorestart=true            ; 異常退出時,自動啓動
startsecs=10                ; 啓動後持續10s後未發生異常,才表示啓動成功
startretries=3              ; 異常後,自動重啓次數
exitcodes=0,2               ; exit異常拋出的是0、2時才認爲是異常
stopsignal=QUIT             ; 殺進程的信號
stopwaitsecs=10             ; 向進程發出stopsignal後等待OS向supervisord返回SIGCHILD 的時間。若超時則supervisord將使用SIGKILL殺進程 
user=chrism                 ; 設置啓動該程序的用戶
log_stdout=true             ; 若是爲True,則記錄程序日誌
log_stderr=false            ; 若是爲True,則記錄程序錯誤日誌
logfile=/var/log/cat.log    ; 程序日誌路徑
logfile_maxbytes=1MB        ; 日誌文件最大大小
logfile_backups=10          ; 日誌文件最大數量
配置文件
啓動:
supervisord /etc/supervisor.conf       #這個老是報錯,不知道什麼緣由
/etc/init.d/supervisor start              #這個比較好用

# 經過配置文件啓動
        uwsgi --ini uwsgi.ini           # 這樣啓動以後後臺能夠看到,不方便
     uwsgi --ini uwsgi.ini & # 建議這種,啓動以後後臺運行
# 會生成兩個文件
            PID文件 他是標識這個程序所處的狀態
            SOCK文件  他是用來和其餘程序通訊的
    # 中止uwsgi
        uwsgi --stop uwsgi.pid

    # 重載配置
        uwsgi --reload uwsgi.ini

 

相關文章
相關標籤/搜索