Django EC2 배포
AWS EC2 설정
-
AWS EC2 프리티어 생성
-
EC2 생성 시 만든 키페어 (예 capstone.pem) 파일을 이용하여 ssh 로그인
-
탄력적 IP 생성 후 생성한 인스턴스에 할당
-
ssh 로 접속
ssh -i "capstone.pem" ec2-user@{퍼블릭 IPv4 DNS}
- EC2 스왑 파일 추가 (AWS 프리티어로 EC2를 사용하면서 발생하는 느려지는 문제 해결 방법)
$ sudo dd if=/dev/zero of=/swapfile bs=128M count=16 # swap 메모리를 할당 $ sudo chmod 600 /swapfile # 스왑 파일에 대한 읽기 및 쓰기 권한 업데이트 $ sudo mkswap /swapfile # Linux 스왑 영역을 설정 $ sudo swapon /swapfile # 스왑 공간에 스왑 파일을 추가하여 스왑 파일을 즉시 사용할 수 있도록 만듦 $ sudo swapon -s # 성공했는지 확인
- /etc/fstab 파일을 편집하여 부팅 시 스왑 파일을 활성화
$ sudo nano /etc/fstab # 파일 끝에 다음 줄을 새로 추가하고 파일을 저장(Ctrl+x 누른 후 y)한 다음 종료 /swapfile swap swap defaults 0 0
- 확인
$ free -h
-
한국시간 설정
$ sudo rm /etc/localtime $ sudo ln -s/usr/share/zoneinfo/Asia/Seoul/etc/localtime
- /etc/sysconfig/clock 파일을 열어 ZONE 값을 UTC에서 Asia/Seoul 으로 변경해준다.
$ sudo nano /etc/sysconfig/clock
- AWS Console 에서 인스턴스 재부팅
EC2 인스턴스에 NGINX + GUNICORN 설치 및 Django 배포
-
Django 설치
$ sudo yum install python3-virtualenv $ virtualenv baseball $ cd baseball $ source bin/activate
-
Mysqlclient 설치
$ sudo su – $ dnf -y localinstall https://dev.mysql.com/get/mysql80-community-release-el9-4.noarch.rpm $ dnf -y install mysql mysql-community-client $ exit $ pip install --upgrade pip $ pip install --upgrade setuptools wheel $ sudo yum install mysql-devel -y $ sudo yum install gcc $ sudo yum search python3 | grep devel $ sudo yum install python3-devel.x86_64 # 이거는 필요한지? $ pip install pymysql $ pip install mysqlclient
-
Git 설치 및 소스 clone (github 리포지토리가 public 인 경우는 아래와 같으나 private 인 경우는 ssh 설정해야함)
$ sudo yum install git $ git --version $ git config --global user.name “github아이디" $ git config --global user.email "github 메일주소“ $ git clone https://github.com/xxxxxxxxxx/yyyyyyyyyy.git $ git pull origin feat/backend-account # requirements.txt 파일이 있어야 함 $ pip install -r requirements.txt
-
Gunicorn 설치
$ cd ~/baseball $ source bin/activate $ pip install gunicorn $ mkdir run $ cd /home/ec2-user/baseball/yyyyyyyyyy/back_end # gunicorn 동작 확인 $ gunicorn --bind 0.0.0.0:8888 baseball.wsgi:application 결과 [2024-06-09 20:43:20 +0900] [92123] [INFO] Starting gunicorn 22.0.0 [2024-06-09 20:43:20 +0900] [92123] [INFO] Listening at: http://0.0.0.0:8888 (92123) [2024-06-09 20:43:20 +0900] [92123] [INFO] Using worker: sync [2024-06-09 20:43:20 +0900] [92124] [INFO] Booting worker with pid: 92124
-
Gunicorn 서비스 등록
$ sudo nano /etc/systemd/system/gunicorn.service
- 아래 내용 추가
[Unit] Description=gunicorn daemon After=network.target [Service] User=ec2-user Group=ec2-user WorkingDirectory=/home/ec2-user/baseball/Baseball-Analytics-Tool/back_end ExecStart=/home/ec2-user/baseball/bin/gunicorn \ --workers 3 \ baseball.wsgi:application [Install] WantedBy=multi-user.target # 저장은 Ctrl+x 누른 후 y
# 서비스 시작 $ sudo systemctl start gunicorn.service # 서비스 상태 조회 $ sudo systemctl status gunicorn.service # 서비스 부팅 시 시작 등록 $ sudo systemctl enable gunicorn.service
-
Nginx 설치 (1)
$ sudo yum install nginx # baseball 웹 configuration 파일 추가 $ cd /etc/nginx/conf.d $ sudo nano baseball.conf
- 아래 내용 추가
server { listen 80; server_name "ec2-43-200-93-66.ap-northeast-2.compute.amazonaws.com"; location / { proxy_pass http://127.0.0.1:8000; # Assuming Gunicorn is running on the default port proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location /static { root /home/ec2-user/baseball/Baseball-Analytics-Tool/back_end; } } # 저장은 Ctrl+x 누른 후 y
- nginx.conf 수정
$ cd .. $ sudo nano nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user ec2-user; # ec2-user로 변경 (nginx로 되어 있음) worker_processes auto; error_log /var/log/nginx/error.log notice; pid /run/nginx.pid; http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; types_hash_max_size 4096; server_names_hash_bucket_size 128; # 추가
-
Nginx 서비스 실행
# 서비스 시작 $ sudo systemctl start nginx.service # 서비스 상태 조회 $ sudo systemctl status nginx.service # 서비스 부팅 시 시작 등록 $ sudo systemctl enable nginx.service
-
보안그룹 80 포트 오픈