AWS EC2, RDS를 이용한 DJANGO 서버 구축 | Build DJANGO Server(AWS EC2, RDS) | JMON

JMON·2021년 3월 19일
0
post-thumbnail

📄 설명

📣 설명

  1. AWS : 아마존 웹 서비스
  2. EC2 : 가상 컴퓨팅 임대 서비스
  3. RDS : 관계형 데이터베이스

📄 사용방법

📣 준비

✍ 기본 설정

  1. EC2 프리티어 Ubuntu 20.04 서버 구축
    (방화벽은 80, 443, 22 포트만 활성화)
  2. RDS > PostgreSQL 서버 구축
    (RDS 방화벽은 EC2 보안그룹 id를 적용)
  3. EC2 생성간 발급 받은 pem 파일을 이용하여
    VS CODE의 원격 접속툴로 접속
    (ssh -i "<KEY_PATH>/<YOUR_KEY_NAME>.pem" ubuntu@<YOUR_EC2_URL>)

✍ 서버 세팅

$ sudo apt-get update -y
$ sudo apt-get upgrade -y
$ sudo apt-get install -y python3 python3-dev python3-pip
$ sudo apt-get install -y git vim
$ sudo apt-get install -y nginx
$ pip3 install django gunicorn
$ django-admin startproject <PROJECT_NAME>

...
# Django 프로잭트 설정
...

✍ Let's Encrypt 설치

# 우분투 20.04
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo apt-get update

# 우분투 16.04, 18.04
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update

# 공통
$ sudo apt-get install certbot python3-certbot-nginx

📣 설정

✍ DJANGO

  1. settings.py 에 STATIC_ROOT 변수 생성
    (ex. STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles"))
  2. python3 manage.py makemigrations
  3. python3 manage.py migrate
  4. python3 manage.py collectstatic

✍ GUNICORN

$ which gunicorn
## GUNICORN 위치 확인

$ sudo vi /etc/systemd/system/gunicorn.service
## 서비스 파일 생성
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=<USER_NAME>
Group=www-data
WorkingDirectory=<YOUR_PROJECT_PATH>
ExecStart=<GUNICORN_PATH> \
        --workers 3 \
        --bind 0.0.0.0:8000 \
        <PROJECT_NAME>.wsgi:application

[Install]
WantedBy=multi-user.target

✍ GUNICORN 상태 확인

$ sudo systemctl start gunicorn
$ sudo systemctl enable gunicorn

✍ NGINX

# sudo vi /etc/nginx/sites-available/default
# configuration of the server
server {

    listen      80;
    listen [::]:80;
    
    server_name <YOURE_DOMIAN>;
    charset     utf-8;

	location = /favicon.ico { access_log off; log_not_found off; }

	location /static/ {
		alias /<YOUR_PROJECT_PATH>/staticfiles/;
	}
	location / {
		include proxy_params;
        proxy_pass http://<YOUR_DOMAIN>:8000;
	}

    # max upload size
    client_max_body_size 75M;
}

✍ NGINX 상태 확인

$ sudo nginx -t
$ sudo systemctl restart nginx

✍ Let's Encrypt 적용

$ sudo certbot --nginx -d <YOUR_DOMAIN>
# 도메인 적용

...
# 문의의 맞춰 진행
...

$ sudo certbot renew --dry-run
# 자동 갱신

📄 참고자료

🎈 Let's Encrypt : https://letsencrypt.org/ko/

profile
Fullstack Developer

0개의 댓글