uwsgi 에서 공식적으로 작성한 문서가 있다.
여기서는 정말 필요한 부분만 따로 공부용으로 작성한다.
자세한 예제와 설명은 공식사이트 참조
리눅스에 대한 기본적인 이해가 있다는 가정하에
파이썬 가상환경 생성은 건너뛴다
nginx - uwsgi - django 웹서버 구축. 차례대로 하면됨
대략적인 서비스 구조
web client <=> web server (nginx) <=> unix socket <=> uwsgi <=> django
장고 설치
pip install django
프로젝트 생성
django-admin startproject mysite
runserver를 통해 제대로 작동하는지 확인
python manage.py runserver 0.0.0.0:8000(AWS ec2에서는 0.0.0.0 이라는 아이피를 붙여주지 않으면 안된다.
이유는 잘 모르겠지만...참고)
nginx 설치
sudo apt install nginx
80번 포트로 제대로 뜨는지 확인
uwsgi 설치
pip install uwsgi만약에 에러가 뜬다면
sudo apt install python3.7-dev (자신의 파이썬 버전에 맞게 설치하면 됨)참고자료 클릭
먼저 설치 후 다시 uwsgi 설치
nginx 설정
여러가지 방법이 있지만 그중 가장 빠르고 오버헤드 적다고 공식사이트에서 추천하는
유닉스 소켓방법(파일소켓) 을 이용한다.
/etc/nginx/sites-available/ 폴더에서 mysite_nginx.conf 파일을 생성.
포트번호가 기본적으로 8000으로 되어있는데 이걸 80으로 바꾸면 된다.
유닉스소켓은 생성해둘 필요가 없고 나중에 저절로 생긴다.
만들고싶은 위치와 소켓 이름만 지정해두면 된다.
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket
}
# 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 /path/to/your/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 /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
/etc/nginx/uwsgi_params
를 프로젝트 내 폴더로 옮긴다.
없을경우 그냥 파일을 하나 만들어서 프로젝트폴더안에 넣는다. (소스코드 링크)
/etc/nginx/sites-enabled/ 폴더에 심볼릭 링크 생성
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
Static files 수집 (css, javascript 등)
왜그런지는 모르겠는데 이과정이 없다면 css에서 에러가 난다.
프로젝트폴더안 setting.py 마지막에 다음 코드를 추가한다.
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
python manage.py collectstatic
nginx -- uwsgi -- django 연결
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666
666으로 하니 잘 작동하였다.
ini파일 생성 (좀더 간단한 명령어가 된다.)
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
아래 코드로 실행
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
Emperor (황제) 모드
설정파일이 변경되었을때 자동으로 restart를 시켜준다고 하는데 글쎄.. 쓸일이 많을까 싶다
그래도 일단은.
아래의 쉘 스크립트를 통해 적절한 폴더를 생성한다
# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
최종 파일 트리
myproject
ㄴmyproject
ㄴmanage.py
ㄴstatic
ㄴadmin
ㄴmysite_uwsgi.ini
ㄴuswgi_params
ㄴmyproject
ㄴ__init__.py
ㄴsettings.py
ㄴurls.py
ㄴwsgi.py
부팅시 자동실행
윈도우 부팅시마다 자동으로 uwsgi가 실행되게 해야한다
크게 2가지 방법이 있는데
1. rc.local에 등록하는 방법
2. service daemon 등록하는 방법
공식사이트에서는 1번으로 알려줬다.
하지만 최신트렌드는 2번방법이고 더 좋다고 한다.
서비스 데몬 등록하는 방법
No comments:
Post a Comment