Flaskã¢ããªã±ã¼ã·ã§ã³ãuwsgi + nginxã§åããã¦ã¿ã
ãã£ã¦ã¿ãçç±
å»å¹´ã®PCK(ãã½ã³ã³ç²åå)ã§ä½ã£ãã¢ããªããµãã¿ãããã§ãµã¼ãã¼å´ã¨ãã¦Flaskã¢ããªã±ã¼ã·ã§ã³ãå®è£
ãã¾ãã.
ãã®æã¯, uWSGIãnginxã¯ååã ãç¥ã£ã¦ããç¶æ
ã§PCKæ¬çªãFlaskã®runã¡ã½ããã§åããã¦ãã¾ãã.
â»Flaskã®runã¡ã½ããã¯éçºã§ä½¿ããã®ã§, å®éã«ãµã¼ãã¹ãæä¾ããããã®ãã®ã§ã¯ãªãããã§ã.
åæ¥å¤ã¨æ¬çªä¸, ãµã¼ãã¼ã®ã¡ã¢ãªä½¿ç¨çãççºãã¦æ°ååéãµã¼ãã¼ã«ã¢ã¯ã»ã¹ã§ããªãäºæ
ãèµ·ã, ä»å¾ãããåé¿ãããã uwsgi + nginx ã§ã®åããæ¹ã«ææ¦ãã¦ã¿ã¾ãã.
ç°å¢æ§ç¯
åºæ¬ç°å¢ : VMware, Ubuntu 16.04
ã¤ã³ã¹ãã¼ã«ãããã®
sudo pip3 install flask uwsgi sudo apt-get install nginx
ãã£ã¬ã¯ããªã®ä½æ
mkdir ~/python/flask/1/
å人çã«ã§ããã ã/ãã£ã¬ã¯ããªä»¥ä¸ããã¡ããã¡ããããããªãã£ãã®ã§
åºæ¬çã«homeãã£ã¬ã¯ããªã®ããã§ä½æ¥ãã¾ãã.
flaskã¢ããªã®ä½æ
cd ~/python/flask/1/ vim app.py
Flaskã®å ¬å¼ããã¥ã¡ã³ãã®ãã®ã使ãã¾ã.
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' if __name__ == '__main__': app.run()
uwsgiè¨å®ãã¡ã¤ã«ã®ä½æ
flaskã¢ããªã¨åããã£ã¬ã¯ããªã§ä½æ¥ãã¾ã.
vim uwsgi.ini
ããã§'~‘ã使ã£ã¦æå®ããã¨No such file of directoryã¨æãããã®ã§æ³¨æãã¾ããã.
[uwsgi] #application's base folder base = /home/nemu/python/flask/1/ # /home/user_name/python/flask/1/ #python module to import app = app # app = app_nameãå ¥ãã module = %(app) #socket file's location socket = %(base)/uwsgi.sock #socket = /var/www/flask/tmp/uwsgi.sock #socket = /tmp/uwsgi.sock #permissions for the socket file chmod-socket = 666 #the variable that holds a flask application inside the module imported at line #6 callable = app #location of log files logto = %(base)/log.txt #logto = /var/log/uwsgi/%n.log master = true processes = 1 vacuum = true die-on-term = true
nginxè¨å®ãã¡ã¤ã«ã®ä½æ
sudo vim /etc/nginx/sites-enable/uwsgi.conf
server { listen 8080; error_log /home/nemu/python/flask/1/error.txt warn; location / { include uwsgi_params; #uwsgi_pass unix:///tmp/uwsgi.sock; #uwsgi_pass unix:///var/www/flask/tmp/uwsgi.sock; uwsgi_pass unix:///home/nemu/python/flask/1/uwsgi.sock; } }
è¨å®ã®ç¢ºèª
sudo service nginx testconfig
[fail]ã¨ã§ããééã£ã¦ããå ´æãæ¢ãã¾ããã.
[OK]ã¨ã§ããè¨å®ãèªã¿è¾¼ã¾ãã¾ã.
sudo service nginx reload
uwsgiãåãã
flaskã¢ããªãããå ´æã§ä½æ¥ãã¾ã.
uwsgi --ini uwsgi.ini
ãã©ã¦ã¶ãã127.0.0.1:8080
ã«ã¢ã¯ã»ã¹ãã¦Hello, World!ã表示ãããã¨æåã§ã.
ãã£ã¦ã¦å°ã£ããã¨
- ‘~'ã使ã£ããã£ã¬ã¯ããªæå®ã§æããã
- /home/<user_name>/ ã§æå®ãã
- .confãã¡ã¤ã«ã§ã»ãã³ãã³ãã¤ãå¿ãã
- /var/log/nginx/ã«error.logã¨ãããã¡ã¤ã«ããããããè¦ã¦åãã£ã
- ãã®è¨äºã§ã¯ãã£ã¦ãªãã, åèãµã¤ã1ã§nginxã«æ¨©éãä¸ãã¦ããã¨ããã§ã¦ã¼ã¶ã¼æå®ãä¸æ£ã¨è¨ããã
- åèãµã¤ã2ã§
nginx:nginx
ã®å ´æã<user_name>:<user_name>
ã«ããã°ããã¨åãã£ã
- åèãµã¤ã2ã§