人生襍多

Nginx Flask 연동 - 초간단 본문

카테고리 없음

Nginx Flask 연동 - 초간단

이혁진 2023. 1. 2. 14:42

0. 연동 후 운영 명령어

# 처음 실행 시 (소켓생성)

$ sudo service uwsgi restart

# 설정사항 혹은 flask 웹 어플리케이션 소스 변경 후 적용

$ sudo service uwsgi reload

# nginx 설정파일 문법오류 체크

$ nginx -t

# 위에서 success가 되었다면

$ service nginx restart

1. 연동 설치

 

Flask 설치

$ pip install flask

샘플 웹프로그램 testapp.py 예제

from flask import Flask app = Flask(__name__) def index(): return "Hello Flask" if __name__ == '__main__': app.run()

 

uWSGI 설치 - uWSGI가 nginx와 Flask 앱 중간에서 미들웨어 역할

# uWSGI

$ apt-get install uwsgi

WSGI는 Web Server Gateway Interface 웹 서버와 웹어플리케이션 간의 소통을 정의해 애플리케이션과 서버가 독립적으로 운영

uWSGI 플러그인 설치

#uwsgi를 설치하고, python과 uwsgi를 연결하는 플러그인을 설치한다.

# python과 uWSGI를 연결하는 플러그인

# python3에 맞게 uwsgi-plugin-python3 설치

$ apt-get install uwsgi-plugin-python3

 

uWSGI 서버 실행을 위한 모듈 생성

#기존 플라스크 앱 메인 파일 외에 uWSGI 서버 실행을 위한 모듈을 생성.

#flask 어플리케이션과 uwsgi를 연결시켜주기 위한 파일 생성

# testwsgi.py 파일 생성

$ vi [프로젝트경로]/testwsgi.py from testapp import app as tttapp if __name__ == "__main__": tttapp.run()

uWSGI 설정 파일

/etc/uwsgi/app-available 폴더에 uwsgi.ini이라는 설정 파일을 생성한다.

파일 생성 후 심볼릭 링크 설정

$ ln -s /etc/uwsgi/apps-available/uwsgi.ini /etc/uwsgi/apps-enabled/

$ vi /etc/uwsgi/apps-available/uwsgi.ini [uwsgi] chdir = [프로젝트 경로] module = [실행할파일이름] // testwsgi callable = [어플리케이션이름] // tttapp plugin = python3 master = true socket = /tmp/uwsgi.sock [소켓파일명] chmod-socket = 666 v accuum = true d ie-on-term = true ignore-sigpipe=true i gnore-write-errors=true disable-write-exception=true

uWSGI 설정 파일 적용 명령어

# 처음 실행 시 (소켓생성) uwsgi 재시작 명령어

$ sudo service uwsgi restart

# 설정사항 혹은 flask 웹 어플리케이션 소스 변경 후 적용

$ sudo service uwsgi reload

 

 

Nginx 설치

$ apt-get install nginx

 

Nginx 설정 파일 생성

/etc/nginx/sites-available 폴더 아래에 설정 파일 생성

파일 생성 후 심볼릭 링크 생성

$ ln -s /etc/nginx/sites-available/[설정파일명] /etc/nginx/sites-enabled

$ vi /etc/nginx/sites-available/[설정파일명] upstream flask{ server unix:/tmp/uwsgi.sock; // 이런식으로 소켓을 uWSGI와 공유 } server { listen 80; server_name [도메인];;// localhost에서 테스트할 경우에는 server_name를 localhost로 지정 root [프로젝트폴더]; // 굳이 안 적어도 되긴함 location / { # uwsgi_pass unix:/tmp/uwsgi.sock; // 이건필요 없음 try_files $uri @app; access_log off; } location /favicon.ico { deny all; log_not_found off; access_log off; } location @app { include uwsgi_params; uwsgi_pass flask; access_log /var/log/nginx/access.log; uwsgi_pass unix:/tmp/uwsgi.sock; // 이하 안적어도 되긴함 uwsgi_max_temp_file_size 20480m; uwsgi_buffering off; uwsgi_ignore_client_abort on; uwsgi_buffers 2560 160k; uwsgi_buffer_size 2560k; uwsgi_connect_timeout 30s; uwsgi_send_timeout 30s; uwsgi_read_timeout 30s; uwsgi_busy_buffers_size 2560k; uwsgi_temp_file_write_size 2560k; proxy_read_timeout 30s; proxy_connect_timeout 75s; } }

이런식으로 접속 포트가 다른 웹어플리케이션을 사용할 수 있다.

/etc/nginx/sites-enabled$ cat hjkim.ini server { listen 81 default_server; root /home/hjkim/www; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } }

Nginx 재시작

# nginx 설정파일 문법오류 체크

$ nginx -t

# 위에서 success가 되었다면

$ service nginx restart

 

> uWSGI 소켓 연결 전이라면, 서버 접속 시 502 Bad Gateway 가 뜬다.

 

Nginx + uWSGI + Flask 전체 프로세스

 


Comments