728x90
반응형

[Python & FastAPI] FastAPI와 Gunicorn을 사용한 서버 설정 (systemctl 에 설정하기)
FastAPI와 Gunicorn을 연동하여 서버를 설정을 해보았습니다 !
Gunicorn 시스템 서비스 파일 설정
우선, FastAPI 애플리케이션을 Gunicorn과 함께 실행하기 위해 시스템 서비스 파일을 설정해야 합니다. 아래는 systemd에서 사용하는 서비스 파일의 예제입니다.
vi /etc/systemd/system/gunicorn.service
# gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/user_project
ExecStart=/home/ubuntu/.local/bin/gunicorn main:app \
--workers 2 \
--worker-class uvicorn.workers.UvicornWorker \
--bind unix:/tmp/myapi.sock
[Install]
WantedBy=multi-user.target
위 설정 파일은 Gunicorn이 FastAPI 애플리케이션을 /home/ubuntu/user_project 디렉토리에서 실행하도록 설정합니다. 또한, unix:/tmp/myapi.sock 소켓으로 바인딩하며, Uvicorn 워커 클래스를 사용하여 2개의 워커 프로세스를 실행합니다.
서비스 파일 작성
파일 권한 설정 및 systemctl 적용
sudo chmod 644 /etc/systemd/system/gunicorn.service
sudo systemctl daemon-reload
gunicorn 경로 확인 및 작동 테스트
위에 작성 했던 경로 및 명령어가 잘 작동되는지 체크 !
# 구니콘 위치 확인
$ which gunicorn
# 작동테스트
$ /home/ubuntu/.local/bin/gunicorn main:app --workers 2 --worker-class uvicorn.workers.UvicornWorker --bind unix:/tmp/myapi.sock
실행
$ sudo systemctl start gunicorn.service # 실행
$ sudo systemctl status gunicorn.service # 상태확인
$ sudo journalctl -u gunicorn.service # 작동 안될 경우 체크
728x90
반응형
'Coding > Python' 카테고리의 다른 글
Python을 이용한 POST 요청 보내기 - 웹사이트에 로그인하는 방법 (0) | 2024.09.30 |
---|---|
[Selenium] 셀레니움으로 크롤링 할 때 시크릿 모드 브라우저 열기 (0) | 2024.09.11 |
[Python] Django, Flask, FastAPI 대표 프레임워크의 장점과 단점 (0) | 2024.07.30 |
[Python & Matplotlib] Matplotlib 한글이 깨지는 문제 해결하는 방법 !! (0) | 2024.06.27 |
[Python & PyAutoGUI] 일정시간 마다 스크린샷을 찍는 자동화 만들기 !! (0) | 2024.06.26 |
댓글