ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • <Django>[uwsgi + nginx 설정]
    Flower in my dev/Django 2016. 10. 23. 19:14

    [uwsgi + nginx 설정]

     

    1. 프로젝트 생성

     - django-admin.py startproject [프로젝트명]


    2. uwsgi 설치

     - sudo pip3 install uwsgi



    3. 프로젝트를 uwsgi로 실행

     - uwsgi --http :8000 --module flower.wsgi


    4. 결과 확인


    5. nginx 설치

     - sudo apt-get install nginx


    6. nginx 설정

     - vi flower_nginx.conf



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    # flower_nginx.conf
     
    # the upstream component nginx needs to connect to
    upstream django {
        # server unix:///opt/webapp/flower/flower.sock; # for a file socket
        server 192.168.0.200:8080# 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 www.flower.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 /opt/webapp/flower/media;  # your Django project's media files - amend as required
        }
     
        location /static {
            alias /opt/webapp/flower/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     /opt/webapp/flower/uwsgi_params; # the uwsgi_params file you installed
        }
    }
    cs


    7. nginx 설정 파일 링크 걸기

    - cd /etc/nginx/sites-enabled/

    - sudo ln -s /opt/webapp/flower/flower_nginx.conf ./

     - default 링크는 삭제


    8. static 설정 추가

     - vi flower/settings.py

     - STATIC_ROOT = os.path.join(BASE_DIR, "static/") <--추가

    - python3 manage.py collectstatic



    9. nginx와 uwsgi 사이에 socket 추가


    - vi uwsgi_params



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    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;
    cs
    - nginx 재시작 (nginx는 설정이 바뀔때면 재시작)

    - sudo /etc/init.d/nginx restart

     uwsgi --socket /home/pi/flower/flower.sock --module flower.wsgi --chmod-socket=666


    10. 디렉토리 확인


    11. uwsgi 설정 파일 만들기

     - vi flower_uwsgi.ini



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # flower_uwsgi.ini
    [uwsgi]
     
    # Django-related settings
    # the base directory (full path)
    chdir           = /opt/webapp/flower/
    # Django's wsgi file
    module          = flower.wsgi
    master          = true
     
    # maximum number of worker processes
    processes       = 5
    # the socket (use the full path to be safe
    socket          = /opt/webapp/flower/flower.sock
    # ... with appropriate permissions - may be needed
    chmod-socket    = 666
    # clear environment on exit
    vacuum          = true
    cs


    12. uwsgi 설정 파일로 실행

     - uwsgi --ini flower_uwsgi.ini


    13. Emperor 모드로 실행

     - Emperor 모드 : uwsgi 설정의 Watch Dog 기능

     - sudo mkdir /etc/uwsgi

     - sudo mkdir /etc/uwsgi/vassals

     - mkdir -p : 상위경로까지 한번에 생성

     - uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data


    14. 마지막 디렉토리 확인


    댓글

Designed by Tistory.