django+uwsgi+nginx的部署

1.下載與項目對應的django版本
pip3 install django==1.11.16 -i https://pypi.douban.com/simple/
2.用django內置的wsgi模塊測試項目是否能夠正常運行
python manage.py runserver 0.0.0.0:8080
3.下載uwsgi--此文所用版本:2.0.17.1
pip install uwsgi
4.測試uwsgi是否能夠正常使用
  1>測試文件test.pyhtml

def application(env, start_response):
        start_response('200 OK',[('Content-Type', 'text/html')])
        return [b'Hello world'] # Python3

  2>測試語句
  uwsgi --http 0.0.0.0:8080 --wsgi-file test.py
  3>選作:能夠直接命令測試django項目是否能夠用uwsgi運行
  切換到django的項目根目錄中python

[root@VM_0_3_centos day18_crm]# ls
crm  day18_crm  manage.py  nohup.out  rbac  README3  script  static  templates  
[root@VM_0_3_centos day18_crm]# pwd
/data/day18_crm

  注意:運行以前必須收集django項目的靜態文件 --若是你的項目是每一個APP下都有static文件夾的話
  python manage.py collectstatic
  uwsgi --http 0.0.0.0:8080 --file day18_crm/wsgi.py --static-map=/static=static
5.在django項目根目錄中建立uwsgi.ini,將uwsgi參數寫到配置文件中,後續與nginx配合
  1>文件內容以下mysql

[uwsgi]
#
uwsgi +django直接用的時候web端口號 #http= :9000 #niginx將動態請求代理回此端口號 socket=0.0.0.0:8080 #項目目錄 chdir= /data/day18_crm #wsgi.py wsgi-file=day18_crm/wsgi.py processes=4 #進程 threads=2 #線程 stats =:9191 #服務中止的時候清除 socket環境 vacuum=true

6.用uwsgi啓動django
  1>正常啓動
    uwsgi --ini uwsgi.ini
  2>後臺掛起式啓動
    nohup uwsgi uwsgi.ini &
7.下載安裝nginx--此文所用版本:nginx version: nginx/1.12.2
  1>下載
    yum -y install nginx
  2>測試--查看xx.xx.xx.xx:80 nginx是否正常
    systemctl start nginx.service
8.nginx配置
  1>在django項目根目錄建立uwsgi_params(注意文件名字不要錯),改爲777的權限,文件內容以下nginx

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

  2>切換到nginx服務
    cd /etc/nginx/conf.d/
  3>建立配置文件--crm_nginx.confweb

upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 0.0.0.0:8080; # for a web port socket (we'll use this first) 與uwsgi.ini的socket ip保持一致
}
 
# configuration of the server
server {
    # the port your site will be served on 服務訪問的時候的IP xx.xx.xx.xx:8000/crm/login
    listen      8000;
    # the domain name it will serve for
    server_name 0.0.0.0; # substitute your machine's IP address or FQDN 服務器IP
charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media #location /media { # alias /path/to/your/mysite/media; # your Django project's media files - amend as required # } location /static { alias /data/day18_crm/static; # your Django project's static files - amend as required 收集的靜態文件目錄 } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; #代理塊 include /data/day18_crm/uwsgi_params; # the uwsgi_params file you installed 與nginx通訊用的,在django的根目錄中存在的文件 } }

  4>查看django項目根目錄文件sql

[root@VM_0_3_centos day18_crm]# ls
crm  day18_crm  manage.py  nohup.out  rbac  README3  script  static  templates  uwsgi.ini  uwsgi_params (倆個重要的文件)

9.啓動nginx
  1>啓動
  systemctl start  nginx.service
  2>查看如今須要的端口是否都啓動
  netstat -tunlpdjango

tcp        0      0 0.0.0.0:9191            0.0.0.0:*               LISTEN      20406/uwsgi         
tcp 0 0 0.0.0.0:8080            0.0.0.0:*               LISTEN      20406/uwsgi     #動態文件交給uwsgi處理   
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12460/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      16787/sshd          
tcp 0 0 0.0.0.0:8000            0.0.0.0:*               LISTEN      12460/nginx: master #niginx處理靜態文件的端口
tcp6       0      0 :::3306                 :::*                    LISTEN      14978/mysqld        
tcp6       0      0 :::80                   :::*                    LISTEN      12460/nginx: master 
udp        0      0 0.0.0.0:68              0.0.0.0:*                           880/dhclient        
udp        0      0 172.27.0.3:123          0.0.0.0:*                           550/ntpd            
udp        0      0 127.0.0.1:123           0.0.0.0:*                           550/ntpd            
udp        0      0 0.0.0.0:123             0.0.0.0:*                           550/ntpd            
udp        0      0 0.0.0.0:44221           0.0.0.0:*                           22821/ntpdate       
udp        0      0 0.0.0.0:59948           0.0.0.0:*                           880/dhclient        
udp6       0      0 :::41081                :::*                                880/dhclient        
udp6       0      0 :::123                  :::*                                550/ntpd    

  3>再收集一遍靜態文件
    注意:必定要在django的settings.py中須要配置static根目錄
    STATIC_ROOT =  os.path.join(BASE_DIR,'static')
    python manage.py collectstaticcentos

相關文章
相關標籤/搜索