flask + apache2

글쓴이 admin 날짜

아파치2, wsgi 설치

sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
sudo apt-get install libapache2-mod-wsgi-py3


apache2 site 정보 작성

/etc/apache2/sites-available/flask_wsgi.conf

<VirtualHost *>
ServerName {앱이름}
WSGIDaemonProcess {앱이름} user={사용자이름} threads=5
WSGIScriptAlias / {앱경로}/app.wsgi
<Directory {앱경로}>
WSGIProcessGroup {앱이름}
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *>
ServerName example.jhin.kr
WSGIDaemonProcess hello_world user=philomoral threads=5 #실행할 앱 이름 helloworld, user는 philomoral
WSGIScriptAlias / /home/philomoral/web_test/flask_test/app.wsgi
<Directory /home/philomoral/web_test/flask_test>
WSGIProcessGroup hello_world #앱 이름 helloworld
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>

사이트 비활성화/활성화

sudo a2dissite 000-default
sudo a2ensite flask_wsgi

ssl

SSL 페이지 정리 참고하여 작성한 후 사이트 설정을 다음과 같이 바꾼다.

일반 사이트 설정과 비슷하나 주요 차이점으로는 다음과 같다.

  • Virtualhost의 포트를 443으로 설정하는 점
  • SSL관련 설정 내용이 추가되는 점
  • IfModule 영역으로 감싸는 점
  • ServerName, WSGIDaemonProcess 이름을 일반 사이트 설정과 중복되지 않게 설정해야하는 점
<IfModule mod_ssl.c>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
ServerName tero.example.com:443
WSGIDaemonProcess hello_world_ssl user=philomoral threads=5
WSGIScriptAlias / /home/philomoral/web_test/flask_test/app.wsgi
<Directory /home/philomoral/web_test/flask_test>
WSGIProcessGroup hello_world_ssl
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>
</IfModule>

flask 앱 작성

앱생성경로: /home/philomoral/web_test/flask_test

Virtualenv 경로: /home/philomoral/web_test/venv

/home/philomoral/web_test/app.wsgi

#가상환경 활성화
activate_this = '/home/philomoral/web_test/venv/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(file=activate_this))
#가상환경 활성화 완료
import sys
sys.path.insert(0,"/home/philomoral/web_test/flask_test") #앱 경로
from hello_world import app as application #hello_world.py 앱 실행

가상환경에서는 psycopg2 라이브러리가 잘 작동되지 않는 문제가 있음. 로컬환경에서는 적용 가능

hello_world.py

from flask import Flask
app = Flask(__name__)
@app.route('/')def home():    return 'Hello World!'
if __name__ == '__main__':    app.run(host="0.0.0.0",port=5050)


업로드파일 파일이름 한글 저장 (인코딩)

/etc/apache2/envvars 파일의 해당부분을 다음과 같이 변경한다.

	#export LANG=C
	export LANG=ko_KR.utf8


0개의 댓글

답글 남기기

아바타 플레이스홀더

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다