chmod 600 키페어
키페어 권한 변경
ssh -i [full path to keypair file] root@[EC2 instance hostname or IP address]
root -> linux는 ec2-user, Ubuntu는 ubuntu이다.
cp pem파일 ~/.ssh/
위 명령어를 치면 루트의 ssh폴더로 간다.
그렇다면 명령어가 다음과 같이 된다.
ssh -i ~/.ssh/pem키 root@[EC2 instance hostname or IP address]
파이썬 설치
sudo apt update
sudo apt upgrade
sudo apt install python3-pip
Django 실행
django-admin startproject django_demo
cd django_demo/
python manage.py runserver
pws: 현재 경로
ls: 현재 폴더 파일들 출력
cd: 디렉토리 변경
ll: 현재 폴더 파일들 정보 출력
vi django_demo/settings.py
vi편집기로 settings.py파일을 열고 host에 aws instance 주소를 적어주고 저장해준다.
django를 포트 8000으로 실행시켜준다.
python manage.py runserver 0.0.0.0:8000
보안탭의 보안 그룹으로 이동해서 포트 8000을 열어주고 anywhere IPv4로 설정 저장
git profile -> settings -> SSH and GPG keys
New SSH keys를 추가 해준다.
맥북 기준으로
cd .ssh
ssh-keygen
cat id_rsa.pub
위 명령어를 입력하면 key가 나타나게 되는데 이를 New SSH Keys에 추가해준다.
이후 ssh를 통해서 git repository에 push한다.
pip install gunicorn
gunicorn은 Ubuntu에서 우리의 서버가 꺼지지 않게 도와준다.
프로젝트에 gunicorn폴더를 만들고 gunicorn.service파일을 만들고 아래와 같이 내용을 입력한다.
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/<프로젝트 경로>/gunicorn
ExecStart=/home/ubuntu/<프로젝트 경로>/<프로젝트 가상환경>/bin/gunicorn \
--access-logfile gunicorn/gunicorn-access.log \
--error-logfile gunicorn/gunicorn-error.log \
--workers 3 \
--bind unix:/home/ubuntu/<프로젝트 경로>/gunicorn/django.sock \
<프로젝트 이름>.wsgi:application
[Install]
WantedBy=multi-user.target
배울때는 가상환경을 그대로 같이 올려서 다운 받았는데 관리를 위해 따로 requirements.txt파일을 만들어서 관리하는 것을 추천한다.
pip freeze > requirements.txt
nginx/django_demo
server {
listen 80;
server_name <서버 ip 주소>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/ubuntu/<프로젝트 이름>/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/<프로젝트 이름>/gunicorn/django.sock;
}
}
STATIC_ROOT = "./static/" # 추가
gunicorn --bind 0.0.0.0:8000 프로젝트이름.wsgi
sudo apt install nginx # 설치
sudo systemctl status nginx # 확인 (q로 종료)
이제 주소로 이동하면 Welcome to nginx를 볼 수 있다.
무한로딩이 걸릴경우
aws에서 인바운드 규칙 추가를 해줘야한다.
새로 aws에서 가상환경을 생성해서 진행해야한다.
pip install -r requirements.txt
django project로 이동해서 gunicorn.service 파일의 ExecStart이후 부분을 복사해서 터미널에서 실행시킨다.
다음 gunicorn폴더로 이동하고 다음을 실행한다.
sudo cp gunicorn.service /etc/systemd/system/
ll /etc/systemd/system/ | grep gunicorn
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl status gunicorn.service
정상적으로 생성이 gunicorn이 생성된 것을 확인하면 이제 nginx폴더로 이동한다.
cd nginx
다음 명령어를 순서대로 실행한다.
sudo cp django_demo /etc/nginx/sites-available/
cd /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/django_demo /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service