aws 배포하기

  1. aws id 생성

  2. EC2인스턴스 생성

  • ubuntu
  • t2.micro
  • key pair 없이 생성 (key pair있으면 local에서 작업가능)
  • security group = SSH, HTTP, HTTPS
  • public address 실행 시 ERR_CONNECTION_REFUSED 여야 정상, time out 이면 방화벽 문제(Inbound rules)
  1. Connect
  • sudo apt update(패키지 버전 체크..생략가능)
  • sudo apt upgrade(최신 버전으로 업그레이드)
  • sudo apt install python3-pip python3-dev python3-venv (Mac아니어도 python3)
  1. github 연동
  • ssh-keygen -t rsa -b 4096 (암호화 방식으로 인증) 이후 엔터로 진행
  • your public key has benn saved in /home/ubuntu/.ssh/id_rsa.pub (위치)
  • cat /home/ubuntu/.ssh/id_rsa.pub (정보 꺼내오기)
  • ssh-rsa 포함 전체 복사
  • github-settings-ssh and GPG keys > New SSH key > key값에 붙여넣어 생성
  • repository의 SSH 주소 복사
  • 다시 돌아와 git clone SSH주소 입력
  1. 가상환경
  • 프로젝트의 루트폴더 내(Linux의)에서 가상환경 세팅 python3 -m venv venv (Mac 아니어도 python3)
  • venv 실행(ls로 틈틈히 확인하자) source venv/bin/activate (여전히 Mac아니어도 이대로!)
  • 패키지 설치 pip install -r requirements.txt
  • !!python manage.py runserver 하면 로컬에서만 접속 할 수 있음!! 따라서 python manage.py runserver 0.0.0.0:8000 이렇게 해야함. 외부에서 접속가능. (아직 접속 안됌)
  • Inbound rule에서 8000port 열기
  1. settings 수정
  • server settings에서 DEBUG=False 해야 에러 내용 숨김. (+static파일 모아주는 게 안됌.)
  • ALLOWED_HOSTS=["localhost", "내 ec2 instance의 address"
  • STATIC_URL과 STATIC_ROOT, MEDIA_URL과 MEDIA_ROOT 설정
  • 설정 끝나면 push 잊지말기
  • Linux에서 git pull
  • python manage.py runserver 0.0.0.0:8000
  • EC2 public address에서 https > http 변경 .8000/주소/
  1. gunicorn 설정
  • (Linux) pip install gunicorn

  • sudo vim /etc/systemd/system/gunicorn.service

  • i=편집시작 esc=편집 끝 :wq저장 후 나가기

      [Unit]
      Description=gunicorn daemon
      After=network.target
    
      [Service]
      User=ubuntu
      Group=www-data
      WorkingDirectory=/home/ubuntu/루트폴더이름
      ExecStart=/home/ubuntu/루트폴더이름/venv/bin/gunicorn  --access-logfile - --workers 3 --bind unix:/home/ubuntu/루트폴더이름/프로젝트이름.sock 프로젝트이름.wsgi:application 
      [Install]
      WantedBy=multi-user.target
      ```
      
    - 루트폴더이름 = 말 그대로 프로젝트가 담겨있는 폴더
    - 프로젝트이름 = django-admin startproject로 만든 프로젝트
    - sudo systemctl enable gunicorn (instance 실행 시 자동실행)
     -sudo systemctl start gunicorn 하여 실행 (sudo systemctl status gunicorn 하여 active 확인)
     
     
  1. Nginx 설정
  • sudo apt install nginx

  • sudo vim /etc/nginx/sites-available/프로젝트이름 하여 위치 확인

  • i=편집시작 esc=편집 끝 :wq저장 후 나가기

    server {
        listen 80;
        server_name 도메인 or ip;
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/home/ubuntu/루트폴더이름/프로젝트이름.sock;
        }
    	(↓장고로 들어오는 static,media 요청을 nginx가 수행↓)
        location /static {
                root /home/ubuntu/루트폴더이름/;
        }
    
        location /media {
                root /home/ubuntu/루트폴더이름/;
        }
    
    }
  • sites-available에 예비로 파일 생성 함

  • sudo ln -s /etc/nginx/sites-available/프로젝트이름 /etc/nginx/sites-enabled/

  • 위 명령어로 예비파일을 복사해서 가져감

  • sudo nginx -t = 오타검사

  • sudo systemctl restart nginx = 재시작

  • sudo systemctl status nginx = 상태확인

  • 이제 public address:8000/주소 가 아니라 그냥 public address/주소

  1. 에러확인
  • tail -f /var/log/nginx/error.log (nginx의 에러확인)
  • 권한 설정 에러
    • namei -nom /home/ubuntu/루트폴더이름/프로젝트이름.sock
    • chmod 755 ~/루트폴더이름/
    • chmod 755 ~
  • css적용 (가상환경 켜져있는지 한 번 더 유의)
    - cd 프로젝트이름 (이동)
    • python manage.py collectstatic
profile
가보자고

0개의 댓글