node, python 서버 실행 방법

공통

chmod 400 recipflash.pem
ssh -i "recipflash.pem" ubuntu@3.39.227.12
 
# swap 메모리 늘리기
free -h
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -h
 
git clone https://github.com/salmonco/recipflash.git
cd recipflash
 
sudo apt update

node 서버

  • port: 4000
  • ip: 3.35.36.242
cd apps/server
 
# node, npm 설치
sudo apt install nodejs
sudo apt install npm
 
# node 버전 업데이트
sudo apt-get install -y nodejs npm
sudo npm install npm
sudo npm install -g n
sudo n stable
hash -r
 
# pnpm 사용 시
sudo npm install -g pnpm
 
# 환경변수 파일 생성
touch .env
vim .env
 
# 빌드
pnpm install
sudo npx prisma generate
pnpm build
 
# 무중단 배포
sudo npm i pm2 -g
pm2 start dist/index.js
 
pm2 monit # 실시간 모니터링
 
pm2 kill

python 서버

  • port: 8000
  • ip: 43.203.191.200
cd apps/ai
 
# ocr 라이브러리 설치
sudo apt install tesseract-ocr -y
sudo apt install tesseract-ocr-eng tesseract-ocr-kor -y
 
# pdf2image는 내부적으로 Poppler라는 PDF 렌더링 라이브러리 사용
sudo apt install poppler-utils -y
 
# 환경변수 파일 생성
touch .env
vim .env
 
# 가상환경 설정
sudo apt install python3-venv -y
python3 -m venv venv
source venv/bin/activate
 
# 빌드
pip install -r requirements.txt
 
# 무중단 배포
sudo vim /etc/systemd/system/fastapi.service # 파일 내용은 하단 참고
 
sudo systemctl daemon-reload
sudo systemctl start fastapi
# sudo systemctl stop fastapi
# sudo systemctl restart fastapi
sudo systemctl enable fastapi
 
sudo journalctl -u fastapi -f # 실시간 모니터링

fastapi.service

[Unit]
Description=FastAPI Uvicorn Service
After=network.target
 
[Service]
# (서비스를 실행할 유저/그룹 – ubuntu 계정이면 sudo 사용 없이 동작 가능)
User=ubuntu
Group=ubuntu
 
# 프로젝트 위치
WorkingDirectory=/home/ubuntu/recipflash/apps/ai
 
# 가상환경 경로
Environment="PATH=/home/ubuntu/recipflash/apps/ai/venv/bin"
 
# 실제 FastAPI 실행 명령
ExecStart=/home/ubuntu/recipflash/apps/ai/venv/bin/uvicorn src.main:app \
    --host 0.0.0.0 \
    --port 8000
 
# 서비스 자동 재시작 옵션
Restart=always
RestartSec=3
 
[Install]
WantedBy=multi-user.target

React-Native

서버 ip주소나 도메인 바뀌면 Info.plist 변경해야 하는 문제 NSAllowsArbitraryLoads를 true → 그래도 안 됨. http면 허용 도메인을 사전에 등록해놔야 함

회피 방법

  1. 하나의 고정 도메인 + 리버스 프록시
    • 예: api.jisu-bong.com 하나를 만들고, 내부적으로 Node/Python 서버로 라우팅
    • 앱에서는 항상 api.jisu-bong.com만 호출 → info.plist 고정
  2. HTTPS 사용
    • ATS는 HTTPS 요청은 기본 허용
    • 도메인이 바뀌어도 새 도메인에 SSL 인증서를 적용하면 NSAllowsArbitraryLoads 없이도 접근 가능
  3. 서버 IP 직접 접근은 권장 X
    • IP는 ATS에서 예외 처리하기 어렵고, 도메인 안정성/보안 측면에서도 비추천

SSL 인증서 등록 (in node server)

nginx 설치 - 리버스 프록시 (in node server)
sudo apt install nginx
 
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx   # 실행 상태 확인
 
# Nginx 설정 파일 생성
# Nginx는 사이트별 설정 파일을 /etc/nginx/sites-available/에 두고, /etc/nginx/sites-enabled/에 심볼릭 링크를 걸어 활성화합니다.
sudo vim /etc/nginx/sites-available/node.jisu-bong.com
 
# 심볼릭 링크로 활성화
sudo ln -s /etc/nginx/sites-available/node.jisu-bong.com /etc/nginx/sites-enabled/
 
# 기본 사이트 비활성화(중요)
sudo rm /etc/nginx/sites-enabled/default
 
# Nginx 설정 테스트
sudo nginx -t
 
sudo systemctl restart nginx
sudo certbot --nginx -d node.jisu-bong.com

/etc/nginx/sites-available/node.jisu-bong.com

server {
    listen 80;
    server_name node.jisu-bong.com;
 
    client_max_body_size 100M; # 업로드 용량 늘리기
 
    location / {
        proxy_pass http://localhost:4000;  # Node 서버 포트
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
SSL 인증서 발급 (in ec2)
  • 전제: 80, 443포트가 필요함. → ec2 인바운드 규칙 추가하기
sudo apt install certbot
sudo apt install python3-certbot-nginx   # nginx와 함께 쓸 경우
sudo certbot certonly --manual
 
# SSL 인증서 자동 발급
sudo certbot --nginx -d node.jisu-bong.com
 
# 인증서 자동 갱신 테스트 (Let's encrypt 인증서는 90일 만료)
sudo certbot renew --dry-run
413 에러 - Request Entity Too Large → 업로드 용량 늘리기
  • 기본 Nginx는 업로드를 1MB로 제한함. nginx 설정 파일 server 블록 안에 client_max_body_size 100M; 추가