반응형
장고 프로젝트를 배포하여 http 서버에 연결하려면 중간 역할을 하는 WSGI(웹 서버 게이트웨이 인터페이스)가 필요합니다.
대표적인 WSGI로는 Gunicorn과 uWSGI가 있는데 그 중에 Gunicorn을 사용해 보았습니다.
Gunicorn 연동을 위한 장고 프로젝트 세팅
- 먼저 장고 프로젝트의 settings.py에 STATIC_ROOT를 설정합니다.
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
- STATIC_ROOT에 모든 정적 파일을 모아놓습니다. 그래야 Nginx에서 포워딩을 설정하기 편합니다.
python manage.py collectstatic
- pip로 Gunicorn 설치 (Windows에서 실행은 지원하지 않으므로 Docker에 설치를 추천합니다.)
pip install gunicorn
- Gunicorn은 윈도우에서는 실행이 안되지만 Docker로 테스트 가능합니다.
참고 : https://github.com/light-and-salt/django-gunicorn-nginx-docker - 아래 명령어 만으로도 8000 포트 서비스가 가능합니다.하지만 static 파일을 서비스하고 https 설정을 위해 nginx로 포팅해야 합니다
gunicorn --bind 0.0.0.0:8000 프로젝트명.wsgi:application
우분투에서 Gunicorn을 서비스 데몬으로 등록
- gunicorn 경로를 모를 경우 다음 명령어로 확인
which gunicorn
- vi /etc/systemd/system/gunicorn.service
[Unit] Description=gunicorn daemon After=network.target [Service] User=유저명 Group=그룹명 WorkingDirectory=/프로젝트경로 ExecStart=/path/of/gunicorn \ --workers 3 \ --bind unix:/프로젝트경로/gunicorn.sock \ 프로젝트명.wsgi:application [Install] WantedBy=multi-user.target
- 서비스 활성 및 시작
# service 파일을 처음 생성한 후 다시 수정했을 경우에만 실행 systemctl daemon-reload # 서비스 활성 systemctl start gunicorn systemctl enable gunicorn # 서비스 시작 service gunicorn start
Nginx를 Gunicorn으로 포워딩
- vi /etc/nginx/sites-available/default
server { ... location /media { alias /프로젝트경로/media; } location /static { alias /프로젝트경로/static; } location / { ... proxy_pass http://unix:/프로젝트경로/gunicorn.sock; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
nginx -t service nginx start
링크) [Nginx] Let's Encrypt를 통해 Nginx에서 무료로 https 설정하기(Ubuntu 14.04)
반응형
'Software Development' 카테고리의 다른 글
WSL을 활용한 윈도우 개발 환경 설정 가이드 (0) | 2023.09.13 |
---|---|
Git version 2.35.2 - 'git status' 에러 코드 128 (0) | 2022.04.15 |