zoukankan      html  css  js  c++  java
  • 使用ngnix通过uwsgi app容器部署django项目

    nginx再linux下可以自己编译,我采用的编译选项为:

    --prefix=/home/hzh/soft/softy/nginx-1.18.0 
    --with-threads 
    --with-file-aio 
    --with-http_ssl_module 
    --with-http_v2_module 
    --with-http_realip_module 
    --with-http_addition_module 
    --with-http_xslt_module 
    --with-http_image_filter_module 
    --with-http_geoip_module 
    --with-http_sub_module 
    --with-http_dav_module 
    --with-http_flv_module 
    --with-http_mp4_module 
    --with-http_gunzip_module 
    --with-http_gzip_static_module 
    --with-http_auth_request_module 
    --with-http_random_index_module 
    --with-http_secure_link_module 
    --with-http_degradation_module 
    --with-http_slice_module 
    --with-http_stub_status_module 
    --with-http_perl_module 
    --with-mail 
    --with-mail_ssl_module 
    --with-stream 
    --with-stream_ssl_module 
    --with-stream_realip_module 
    --with-stream_geoip_module 
    --with-stream_ssl_preread_module 
    --with-cpp_test_module 
    --with-compat 
    --with-pcre 
    --with-pcre-jit 
    --with-libatomic 
    --with-debug 
    --with-perl_modules_path=/home/hzh/soft/softy/nginx-1.18.0/perl_modules

    其中 --with-perl_modules_path 是 make install 时存放 nginx.pm 的路径,最好放到安装目录里面,不然挪动这个文件路径之后,nginx就运行不成功,路径似乎是配置后硬编译进可执行文件里去了。

    并且安装make install 好后,你就不能移动(包括重命名)该安装目录,一旦移动之后,就不能运行,会提示 Can't locate nginx.pm in @INC 或 could not open error log file 等错误。如果真的要移动到别的地方,只能通过目录软链接。

    uwsgi 采用 pip 来安装:

    $  vf activate env3.8.2
    $  pip install uwsgi

    nginx和uwsgi部署django项目的文档大致有如下参考:

    https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

    https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/uwsgi/

    https://uwsgi-docs.readthedocs.io/en/latest/Management.html

    我的部署示例:

    我的django项目位于:   /home/hzh/develop/u-chuang/webs/device_register

    项目目录结构:

    device_register
    ├── db.sqlite3
    ├── device_register
    │   ├── asgi.py
    │   ├── __init__.py
    │   ├── __pycache__
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── manage.py
    ├── register
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   ├── 0002_auto_20201127_0317.py
    │   │   ├── 0003_auto_20201127_0512.py
    │   │   ├── 0004_auto_20201127_0515.py
    │   │   ├── 0005_auto_20201127_0630.py
    │   │   ├── 0006_auto_20201128_1122.py
    │   │   ├── 0007_auto_20201128_1403.py
    │   │   ├── __init__.py
    │   │   └── __pycache__
    │   ├── models.py
    │   ├── __pycache__
    │   ├── static
    │   │   └── register
    │   │       └── style.css
    │   ├── templates
    │   │   └── register
    │   │       └── waiting_lists.html
    │   ├── tests.py
    │   ├── urls.py
    │   └── views.py
    ├── static
    │   ├── admin
    │   ├── grappelli
    │   └── register
    │       └── style.css
    └── templates
        └── admin-hzh
            ├── actions.html
            ├── app_list.html
            ├── base_site.html
            ├── change_list_results.html
            └── index.html

    其中 device_register/static 目录是静态目录,里面包含了app register的目录及 grappelli(一个admin美化模块) 目录。

    部署配置文件结构:

    nginx-uwsgi-config
    ├── nginx-config
    │   ├── mime.types
    │   ├── my-nginx.conf
    │   ├── nginx.conf
    │   └── uwsgi_params
    └── uwsgi-config
        └── uwsgi.ini

    其中的 mime.types 和 uwsgi_params 是nginx自带的,没有任何改变; nginx.conf 大部分是nginx自带的,有点改变,全文如下:

    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #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  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /tmp/hzh/nginx-uwsgi-config/nginx-config/my-nginx.conf;
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    }

    其中 my-nginx.conf 是与自己项目相关的配置:

    # the upstream component nginx needs to connect to
    upstream django {
        server unix:///tmp/hzh/device_register-uwgsi-nginx.sock;    # for a file socket
        # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
    }
    
    # configuration of the server
    server {
        # the port your site will be served on
        listen      8000;
        # the domain name it will serve for
        server_name example.com; # substitute your machine's IP address or FQDN
        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 /home/hzh/develop/u-chuang/webs/device_register/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     /tmp/hzh/nginx-uwsgi-config/nginx-config/uwsgi_params; # the uwsgi_params file you installed
        }
    }

    其中 uwsgi.ini 是uwsgi的配置文件:

    [uwsgi]
    
    # Django-related settings
    # the base directory (full path)
    chdir           = /home/hzh/develop/u-chuang/webs/device_register
    # Django's wsgi file
    module          = device_register.wsgi:application
    # the virtualenv (full path)
    home            = /home/hzh/.virtualenvs/env3.8.2
    env             = DJANGO_SETTINGS_MODULE=device_register.settings
    
    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 10
    # the socket (use the full path to be safe
    socket          = /tmp/hzh/device_register-uwgsi-nginx.sock
    # ... with appropriate permissions - may be needed
    chmod-socket    = 664
    pidfile         = /tmp/hzh/device_register-uwsgi.pid
    # clear environment on exit
    vacuum          = true

    重要,建议nginx与uwsgi通信使用 file socket,而不是使用 port socket,因为 port socket 相当于暴露了 uwsgi 给外部,安全级别不好。

    这些配置文件准备好后,就可以运行uwsgi和nginx了:

    $  uwsgi --ini uwsgi.ini
    $  /home/hzh/soft/nginx/sbin/nginx -c /tmp/hzh/nginx-uwsgi-config/nginx-config/nginx.conf
  • 相关阅读:
    eclipse中不能识别enum
    The source attachment does not contain the source for the file Activity.class
    ASP.NET应用程序脱机问题
    鱼仔系统部署教程
    鱼仔系统开发教程
    mysql innodb cluster 无感知集群
    Mysql 8.0 新特性测试
    随笔1
    音频输出格式
    2012.02.03(S3C6410中文手册笔记)(一)
  • 原文地址:https://www.cnblogs.com/welhzh/p/14079210.html
Copyright © 2011-2022 走看看