준비물
EC2 인스턴스와 FastAPI를 먼저 생성해 주세요.
EC2 인스턴스의 인바운드 규칙 편집을 통해 80번 포트를 개방해 주세요!
설치
Python 설치 / FastAPI 설치 / uvicorn 설치 / nginx 설치
Python 설치
각자 FastAPI를 통해 API를 만들 때 사용했던 Python 버전을 사용해 줍니다.
저는 최신 파이썬으로 하겠습니다.
설치하기
sudo apt update
sudo apt-get upgrade python3
sudo apt install python3-pip
확인하기
pip3 --version
FastAPI와 uvicorn 설치
지난 포스트에 FastAPI 시작하기를 통해서 설치할 수 있습니다.
https://wbsf.tistory.com/entry/FastAPI-Fast-API-%EC%8B%9C%EC%9E%91%ED%95%98%EA%B8%B0
[FastAPI] Fast API 시작하기
설치 pip install fastapi pip install "uvicorn[standard]" 아래에 있는 uvicorn은 Fast API를 웹 서버에 올려주는 역할을 한다. 만약 CLI앱을 구현하고자 한다면 Typer를 설치하자. https://typer.tiangolo.com/ Typer Typer, build
wbsf.tistory.com
설치가 완료되었으면 FastAPI를 실행해 봅시다.
uvicorn: command not founc
이와 같은 오류가 뜬다면...!
export PATH=$PATH:~/.local/bin
환경 변수에 사용자 설치 경로를 추가해 주면 해결할 수 있습니다.
Nginx 설치
1.18.0 ubuntu 기준입니다.
설치하기
sudo apt install nginx
확인하기
sudo service nginx status
실행하기
sudo service nginx start
재실행하기
sudo service nginx restart
종료하기
sudo service nginx stop
여기까지 설치가 마무리되었으면
nginx의 설정을 통해 nginx에서 FastAPI로 링크할 수 있도록 해줍시다.
sudo vi /etc/nginx/sites-enabled/default
vi 편집기로 열어주게 되면
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
이런 코드가 있을 겁니다.
여기서 location 부분을 수정할 겁니다.
( 혹시 vi 편집기가 처음이시라면 i를 눌러 인서트모드로 바꾸어주시면 됩니다.)
( 저장할 때는 esc를 누르시고 :wq! 를 입력해 주세요! )
기존에 있는 location을 수정한다면
IPaddress:80/ 으로 들어오는 클라이언트가 링크됩니다.
저는 /server로 FastAPI에 만들어두었으니 다음과 같이 수정하겠습니다.
location /server {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $http_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-Photo $scheme;
}
여기서 proxy_pass 부분에 링크될 주소를 적습니다.
uvicorn으로 FastAPI를 실행할 때의 초기값으로 설정해 둡니다.
이렇게 설정을 마쳤다면 Nginx를 재실행해주시고
sudo nginx -t
이 명령어를 통해서
문제가 없다고 뜨는지 확인해 주세요.
문제가 뜬다면 default 파일에 오타가 있을 겁니다...
이제 FastAPI를 실행시키고 확인해 줍시다!!
uvicorn main:app
Reference
https://hajunyoo.oopy.io/docker/fastapi-deploy-ec2#23e5b0b9-24fb-4fa7-b9bc-f2ea30ce96d6
AWS EC2 배포(FastAPI + Nginx)
AWS EC2 배포(FastAPI)에 대해 정리한 내용들
hajunyoo.oopy.io
'Cloud' 카테고리의 다른 글
[AWS] AWS CLI를 이용해서 S3 관리하기 (0) | 2023.06.06 |
---|---|
[AWS] AWS CLI를 이용해서 S3 버킷 연결하기 (0) | 2023.06.06 |
[Cloud] PuTTY를 사용하여 Windows에서 Ubuntu 인스턴스 연결하기 (AWS EC2) (0) | 2023.05.31 |