[python] flask+gunicorn3+ngnix

spring·2021년 12월 26일
0

본 설정은 네이버클라우드 Ubuntu18.04를 기준으로 합니다.

기본 설정

패키지 설치

sudo apt-get install nginx gunicorn3 -y

gunicorn 연결 테스트

파일이 index.py이고 flask 변수 이름이 app일 때 아래와 같이 실행해서 정상 동작하는지 확인한다.

gunicorn3 --bind 0.0.0.0:80 index:app

gunicorn sock 생성테스트

gunicorn3 --bind unix:`pwd`/gunicorn.sock index:app

리눅스 서비스 생성

vim /etc/systemd/system/<이름>.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=<플라스크 파일 디렉토리 위치>
ExecStart=/usr/bin/gunicorn3 \
        --workers 4 \
        --bind unix:/<플라스크 파일 디렉토리 위치>/gunicorn.sock index:app &

[Install]
WantedBy=multi-user.target

nginx 포트 변경

vim /etc/nginx/sites-enabled/default

기본 포트를 8080같은걸로 변경하고 ipv6를 사용하지 않으면 2번째 줄은 주석 처리한다.

server {
    listen 8080 default_server;
    #listen [::]:80 default_server;
    .
    .
    .
 }

서비스 등록

sudo systemctl daemon-reload
sudo systemctl enable <이름>
sudo systemctl start <이름>

서비스 구동 확인

sudo systemctl status <이름>

nginx 사이트 설정

도메인이 있다면 server_name에는 도메인을 넣으면 된다.

vim /etc/nginx/conf.d/<이름>.conf
server {
    listen 80;
    server_name <IP>;
	client_max_body_size 20M;
    location / {
        proxy_pass http://unix:/root/hq/gunicorn.sock;
    }
}

위에서 client_max_body_size로 파일 전송의 최대 크기를 지정할 수 있다.

ngnix 재시작

sudo systemctl restart nginx <이름>
profile
Researcher & Developer @ NAVER Corp | Designer @ HONGIK Univ.

0개의 댓글