AWS 배포

by yuyeol3, 2025-02-11


1. 개요

flask 웹앱의 AWS EC2 배포를 정리한 글이다.

2. webapp 설치

배포할 웹앱을 ec2에 올린다

git clone "주소"

3. gunicorn 설정

3.1 설치

sudo apt install gunicorn

3.2 동작 확인

gunicorn3 --bind 0.0.0.0:5000 app:app &

3.3 리눅스 서비스 등록

# cd /etc/systemd/system/wigit.service [Unit] Description=gunicorn daemon After=network.target [Service] User=root Group=root WorkingDirectory=/home/ubuntu/wigit ExecStart=/home/ubuntu/wigit/virt/bin/gunicorn \ --workers 5 \ --threads 4 \ --bind unix:/home/ubuntu/wigit/gunicorn.sock \ --access-logfile /home/ubuntu/wigit/access.log \ --error-logfile /home/ubuntu/wigit/error.log \ --preload \ main:app & [Install] WantedBy=multi-user.target

--preload를 설정해야 flask-login 기능이 정상적으로 동작했다.(아마 --preload가 프로세스 간 메모리를 공유하게 해서 그런 것 같다)

3.4 서비스 등록

sudo systemctl daemon-reload
sudo systemctl enable wigit
sudo systemctl start wigit

등록 후 정상 동작하는지 확인한다.

sudo systemctl status wigit

4. nginx 설정

기존 default파일 내용을 지우고 다음으로 대체한다.

# /etc/nginx/sites-available server { listen 80 default_server; listen [::]:80 default_server; server_name 0.0.0.0; root /home/ubuntu/wigitback; location / { try_files $uri $uri/ @app; } location @app { proxy_pass http://unix:/home/ubuntu/wigit/gunicorn.sock; access_log off; } location /assets/ { alias /home/ubuntu/wigit/assets/; autoindex on; } }

설정 후 다음 커멘드로 구문 확인과 리로드를 수행한다.

$ sudo nginx -t         # 구문 테스트
$ sudo nginx -s reload  # 리로드

5. 502 Bad Gateway 발생시

  • 1.폴더 권한 및 .sock 권한 확인
$ chmod 755 /home/ubuntu
$ chmod 660 /home/ubuntu/wigit/gunicorn.sock
  • 2.에러 로그 확인 후 대처..
$ sudo tail -f /var/log/nginx/error.log  # nginx
$ tail -f ~/wigit/error.log              # gunicorn