deploying flask with nginx & uwsgi

19
Deploying Flask with nginx & uWSGI Kim Jeong Ju ([email protected] ) 13723화요일

Upload: -

Post on 15-Jan-2015

5.827 views

Category:

Technology


6 download

DESCRIPTION

for Python Korea 강남 Flask 스터디

TRANSCRIPT

Page 1: Deploying flask with nginx & uWSGI

Deploying Flask with nginx & uWSGIKim Jeong Ju ([email protected])

13년 7월 23일 화요일

Page 2: Deploying flask with nginx & uWSGI

Agenda

• Flask Document

• Prepare

• VirtualEnv(Wrapper)

• uWSGI

• nginx

• Tips & Pitfalls

13년 7월 23일 화요일

Page 3: Deploying flask with nginx & uWSGI

Flask Documents

• Deploying to a Web Server• http://flask.pocoo.org/docs/quickstart/#deploying-

to-a-web-server

• Deploying with Distribute• http://flask.pocoo.org/docs/patterns/distribute/

• Deployment Options• http://flask.pocoo.org/docs/deploying/

13년 7월 23일 화요일

Page 4: Deploying flask with nginx & uWSGI

Prepare

• gcc & python-dev• sudo apt-get update

• sudo apt-get install build-essential python-dev python-pip

• Make User (Don’t use ‘root’ user)• sudo useradd -r -g www-data -s /bin/false uwsgi

• Make dir• sudo mkdir -p /var/www/myapp

13년 7월 23일 화요일

Page 5: Deploying flask with nginx & uWSGI

Prepare - VirtualEnv

• Install• sudo pip install virtualenvwrapper

• Config• export WORKON_HOME=~/VirtualEnvs

• mkdir -p $WORKON_HOME

• source /usr/local/bin/virtualenvwrapper.sh

13년 7월 23일 화요일

Page 6: Deploying flask with nginx & uWSGI

• Shell Script (~/.profile)• export WORKON_HOME=~/VirtualEnvs

• source /usr/local/bin/virtualenvwrapper.sh

• Make Env. & Enter• mkvirtualenv myapp

• workon myapp

Prepare - VirtualEnv

13년 7월 23일 화요일

Page 7: Deploying flask with nginx & uWSGI

Prepare - Cont.

• Install Flask

• Do(or Clone) your app

• Permission• sudo chown uwsgi:www-data /var/www/myapp -R

• sudo chmod 775 application.py

• Add group• sudo usermod -a -G www-data myuser

13년 7월 23일 화요일

Page 8: Deploying flask with nginx & uWSGI

uWSGI

• Install• pip install uwsgi

• Make log file• sudo mkdir -p /var/log/uwsgi

• sudo touch /var/log/uwsgi/myapp.log

• Config & Run• sudo /home/ubuntu/VirtualEnvs/myapp/bin/uwsgi -x

uwsgi.xml

13년 7월 23일 화요일

Page 9: Deploying flask with nginx & uWSGI

uwsgi.xml<?xml version="1.0"?><uwsgi id="myapp"> <module>application:app</module> <virtualenv>/home/myuser/VirtualEnvs/myapp</virtualenv> <socket>/tmp/myapp.sock</socket> <processes>4</processes> <chdir>/var/www/myapp</chdir> <pidfile>/tmp/myapp.pid</pidfile> <daemonize>/var/log/uwsgi/myapp.log</daemonize> <stats>/tmp/myapp.stats</stats> <log-format>``%(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size)`` "%(referer)" "%(uagent)"</log-format> <uid>uwsgi</uid> <gid>www-data</gid> <chmod-socket>775</chmod-socket></uwsgi>

13년 7월 23일 화요일

Page 10: Deploying flask with nginx & uWSGI

nginx

• Install• sudo apt-get install nginx

• Start• sudo service nginx start

13년 7월 23일 화요일

Page 11: Deploying flask with nginx & uWSGI

nginx config

server { listen 8080; server_name myapp;

root /var/www/myapp;

try_files $uri @uwsgi; location @uwsgi { include uwsgi_params; uwsgi_pass unix:/tmp/myapp.sock; }}

13년 7월 23일 화요일

Page 12: Deploying flask with nginx & uWSGI

nginx

• Write config file• sudo vi /etc/nginx/sites-available/www

• Symbolic link• sudo ln -s /etc/nginx/sites-available/www /etc/

nginx/sites-enabled/

• remove default from sites-enabled/

• Restart• sudo service nginx restart

13년 7월 23일 화요일

Page 13: Deploying flask with nginx & uWSGI

Done!

13년 7월 23일 화요일

Page 14: Deploying flask with nginx & uWSGI

Tips - Scripts

• start• sudo /home/myuser/VirtualEnvs/myapp/bin/uwsgi -

x ./uwsgi.xml

• stop

• sudo /home/myuser/VirtualEnvs/myapp/bin/uwsgi --stop /tmp/myapp.pid

• reload• sudo /home/myuser/VirtualEnvs/myapp/bin/uwsgi --

reload /tmp/myapp.pid

13년 7월 23일 화요일

Page 15: Deploying flask with nginx & uWSGI

Tips - Logging Errors

• In production, you won’t see log messages

• Send Error Mails!

• Install Mail Server• sudo apt-get install postfix

ADMINS = ['[email protected]']if not app.debug: import logging from logging.handlers import SMTPHandler mail_handler = SMTPHandler('127.0.0.1', '[email protected]', ADMINS, 'YourApplication Failed') mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler)

13년 7월 23일 화요일

Page 16: Deploying flask with nginx & uWSGI

Tips - Configuration

• Best Practice - http://flask.pocoo.org/docs/config/#configuration-best-practices

• make_config.py

13년 7월 23일 화요일

Page 17: Deploying flask with nginx & uWSGI

Pitfall - ‘print’

• Unicode Encode Error

• No Daemon - OK, Daemon - Error!• msg = u”한글”; print msg

• -> msg = u”한글”; print msg.encode(‘utf-8’)

13년 7월 23일 화요일

Page 18: Deploying flask with nginx & uWSGI

Pitfall - Pass Env Vars

• nginx UWSGI_SETENV not work (Bug!)• uwsgi_param UWSGI_SETENV

DJANGO_SETTINGS_MODULE=myapp.settings;

• Use uwsgi ‘env’• <env>MYAPP_DEBUG=False</env>

13년 7월 23일 화요일

Page 19: Deploying flask with nginx & uWSGI

References

• http://blog.naver.com/PostView.nhn?blogId=ez_&logNo=140138625021&parentCategoryNo=1&viewDate=&currentPage=1&listtype=0

• http://blog.kramerapps.com/post/22551999777/flask-uwsgi-nginx-ubuntu

• http://blog.naver.com/PostView.nhn?blogId=ez_&logNo=140138625021&parentCategoryNo=1&viewDate=&currentPage=1&listtype=0

• http://flask.pocoo.org/docs/errorhandling/

13년 7월 23일 화요일