本篇導航:html
項目開發完畢,在部署以前須要再配置文件中將 ALLOWED_HOSTS配置設置爲:當前服務器IP或*,如:python
ALLOWED_HOSTS = ["*",]
示例源碼:猛擊下載nginx
而後將源碼上傳至服務器指定目錄,如:/data/ ,而後執行命令來運行:django
注: 上傳命令: scp /Users/wupeiqi/PycharmProjects/oldboy-1.zip root@192.168.10.33:/data/vim
解壓,進入目錄並執行如下命令:服務器
python3 mange.py runserver 0.0.0.0:8000
Django框架運行依賴wsgi(本質提供socket服務端),衆多模塊實現了wsgi規範,而django框架中默認使用wsigiref模塊來實現,他因爲性能比較低,因此用於本地開發和測試,而線上部署時須要使用uwsgi來代替。負載均衡
一、在服務器上安裝uwsgi框架
pip3 install uwsgi
二、單文件使用uwsgi
在服務器上編寫一個Python文件:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"]
在服務器上執行命令啓動Web服務器:
uwsgi --http :9001 --wsgi-file app.py # 或 uwsgi --http :9002 --wsgi-file foobar.py --master --processes 4
訪問查看便可:
三、 django程序使用uwsgi
將開發好的django程序拷貝到服務器目錄【同示例一】,即:/data/oldboy ,執行命令啓動:
uwsgi --http :9005 --chdir /data/oldboy/ --wsgi-file oldboy/wsgi.py --master --processes 4
1. 建立配置文件 oldboy.ini [uwsgi] http = 0.0.0.0:9005 chdir = /data/oldboy/ wsgi-file = oldboy/wsgi.py processes = 4 static-map = /static=/data/oldboy/allstatic 2. 根據配置文件啓動uwsgi uwsigi --ini oldboy.ini 配置文件的形式啓動uwsgi
PS: 若是有虛擬環境能夠添加上虛擬環境路徑配置: virtualenv
=
/env
/oldboy
_venv
此時訪問時,會出現找不到靜態文件的錯誤。
想要uwsgi處理靜態文件,須要先將django的靜態文件收集到制定目錄,而後再設置對應關係。
訪問地址,便可看到你的女友們了....
利用Nginx作反向代理、負載均衡以及處理靜態文件。
一、安裝Nginx
yum install nginx
二、配置nginx
user root; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; include /etc/nginx/mime.types; default_type application/octet-stream; upstream django { server 127.0.0.1:8001; # server 192.1.123.1; } server { listen 80; charset utf-8; # max upload size client_max_body_size 75M; location /static { alias /data/oldboy/allstatic; } location / { uwsgi_pass django; include uwsgi_params; } } }
確保nginx配置文件目錄下有uwsgi_params文件(默認)
三、配置uwsgi
爲了確保讓全部請求均經過80端口來訪問網站,將uwsgi的配置文件修改成:
[uwsgi] socket = 127.0.0.1:9005 chdir = /data/oldboy/ wsgi-file = oldboy/wsgi.py processes = 4 logto = /tmp/oldboy.log
四、啓動uwsgi和nginx
uwsgi --ini /data/oldboy/oldboy.ini & /etc/init.d/nginx start
supervisor是一個對進程管理的軟件,能夠幫助咱們啓動uwsgi並維護(uwsgi進程關閉時,自動將其啓動起來)。
一、安裝
yum install supervisor
二、配置 vim /etc/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