django + uwsgi + supervisor + nginx 배치

3299 단어
환경 설치
yum install python-devel.x86_64 nginx supervisor gcc python-pip -y
pip install uwsgi

테스트 virtualenv Django 환경 구축
pip install virtualenv
mkdir myproject
cd myproject/
virtualenv --no-site-packages venv
source venv/bin/activate
pip install Django==1.9
django-admin startproject mysite
cd mysite/
python manage.py startapp blog
python manage.py runserver 0.0.0.0:8080

테스트 uwsgi Django 프로그램 시작
# uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi

uwsgi --http :8001 --chdir /root/myproject/mysite  --home=/root/myproject/venv --module mysite.wsgi

uwsgi 프로필 만 들 기
mysite-uwsgi.ini
[uwsgi]
# http = :8080
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /root/myproject/mysite
# the virtualenv directory(full path)
home = /root/myproject/venv
# Django's wsgi file
wsgi-file = mysite/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
 
#monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum          = true

supervisor 서비스 설정
/etc/supervisord.d/mysite.ini
[program:mysite]
command=/usr/bin/uwsgi --ini /root/myproject/mysite-uwsgi.ini
directory=/root/myproject/mysite
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

슈퍼 바 이 저 서비스 시작
systemctl enable supervisord
systemctl start supervisord

nginx 서비스 설정
/etc/nginx/conf.d/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///root/myproject/mysite/mysite.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 192.168.75.104; # 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 /root/myproject/mysite/media;  # your Django project's media files - amend as required
    }
 
    location /static {
        alias /root/myproject/mysite/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     uwsgi_params; # the uwsgi_params file you installed
    }
}

nginx 서비스 시작
#              
nginx -t
#     
systemctl enable nginx && systemctl start nginx

참고 문서
  • https://alanhou.org/centos-7-uwsgi-nginx-django/
  • https://code.ziqiangxuetang.com/django/django-nginx-deploy.html
  • https://www.cnblogs.com/alex3714/p/6538374.html
  • https://stackoverflow.com/questions/32974204/got-no-such-file-or-directory-error-while-configuring-nginx-and-uwsgi

  • 공식 문서
  • https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#configure-nginx-for-your-site
  • 좋은 웹페이지 즐겨찾기