일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Github
- pandas
- EC2
- pytorch
- ollama
- team_project
- aws
- 파이썬
- ML
- finpilot
- mifare
- ai_캠프
- conda
- chromeextention
- mysql
- streamlit
- lightsail
- seaborn
- Jupyterlab
- sLLM
- Python
- ai캠프
- 티스토리챌린지
- 머신러닝
- 로컬 런타임
- 정치기 필기
- ai 캠프
- djangorestframework
- django
- 오블완
- Today
- Total
greatsangho의 이야기
캠프 79일차 - AWS 웹 페이지 배포 간단 정리 본문
EC2
Ubuntu 24 기준
8000 포트 개방
ssh 연결 설정
RDS MySQL 설정
sudo apt-get update
sudo apt install python3-venv
python3 -m venv <가상환경 이름>
pip install django
pip install wheel
pip install markdown
sudo apt-get install mysql-server
sudo apt-get install build-essential python3-dev default-libmysqlclient-dev pkg-config
gcc --version
sudo apt-get install gcc
sudo apt install libmysqlclient-dev -y
pip install mysqlclient
mysql -h mysql-db.<db_id>.<region>.rds.amazonaws.com -P 3306 -u your_database_user -p
password : your_database_password
show databases
CREATE DATABASE your_database_name
git clone <깃허브 레포지토리> .
vim settings.py
ALLOWED_HOSTS = ['*'] # 모든 호스트를 허용( 개발용 )
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database_name', # 사용할 데이터베이스 이름
'USER':'your_database_user', # RDS 설정 이름
'PASSWORD':'your_database_password', # RDS에서 설정한 암호
'HOST':'mysql-db.<db_id>.<region>.rds.amazonaws.com', # RDS 엔드포인트
'PORT':'3306',
'default-character-set' : 'utf8',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
...
# Static files (CSS, JavaScript, Images)
import os
STATIC_URL = 'static/'
# STATIC_ROOT 배포용 디렉터리
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
python manage.py createsuperuser
python manage.py collectstatic
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0:8000
<ip 주소>:8000/admin 으로 접속 확인
pip install gunicorn
gunicorn --bind 0.0.0.0:8000 <프로젝트 이름>.wsgi:application
gunicorn --bind unix:/tmp/gunicorn.sock myproject.wsgi:application
유닉스 소켓은 단독으로 독작하지 못하므로 nginx 필요
(venv) ubuntu@ip:~/venv$ vi myproject.env
DJANGO_SETTINGS_MODULE=myproject.settings
sudo vim /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/prj/myproject
EnvironmentFile=/home/ubuntu/venv/myproject.env
ExecStart=/home/ubuntu/venv/bin/gunicorn \
--workers 2 \
--bind unix:/tmp/gunicorn.sock \
myproject.wsgi:application
[Install]
WantedBy=multi-user.target
프로젝트 위치, gunicorn 위치 고려
sudo systemctl daemon-reload
sudo systemctl start gunicorn.service
sudo systemctl enable gunicorn.service
systemctl status gunicorn
sudo apt install nginx
sudo vi /etc/nginx/sites-available/myproject
server {
listen 80;
server_name <EC2 퍼블릭 IP>;
location / {
proxy_pass http://unix:/home/ubuntu/projects/myproject/gunicorn.sock; # EC2
서버에서 Gunicorn 실행 중
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
alias /home/ubuntu/myproject/staticfiles/; # staticfiles 파일 경로
}
location /media/ {
alias /home/ubuntu/myproject/media/; # media 파일 경로
}
}
심볼릭 링크 만들기
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
Nginx 설정 테스트
sudo nginx -t
sudo systemctl restart nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
'프로그래밍 > SK AI 캠프' 카테고리의 다른 글
FastAPI 입문 (0) | 2025.01.02 |
---|---|
19주차 후기 - Django Rest Framework (2) | 2024.12.28 |
캠프 78일차 - AWS EC2(Ubuntu)및 RDS(MYSQL), Nginx와 Gunicorn 사용한 Django 웹 구현 (0) | 2024.12.16 |
캠프 77일차 - AWS EC2 Amazon Linux 인스턴스 설정 및 Nginx + Gunicorn + Django (1) | 2024.12.13 |
캠프 76일차 - AWS lightsail 사용하여 AWS 입문하기 (0) | 2024.12.12 |