Skip to content

Instantly share code, notes, and snippets.

@hoto17296
Created September 6, 2017 10:53
Show Gist options
  • Save hoto17296/aecb87f6ec414ee5d294a88ac3ac11ef to your computer and use it in GitHub Desktop.
Save hoto17296/aecb87f6ec414ee5d294a88ac3ac11ef to your computer and use it in GitHub Desktop.
Nginx + uWSGI + Flask on Amazon Linux
from flask import Flask
application = Flask(__name__)
@application.route("/")
def hello():
return "Hello World!"
# やったことメモ
# もろもろインストール
sudo yum update -y
sudo yum groupinstall "Development Tools"
sudo yum install -y python35-pip python35-devel nginx
# ディレクトリ掘る
sudo mkdir /var/app
sudo chown ec2-user:ec2-user /var/app
cd /var/app
# アプリケーションと uWSGI の設定
vim app.py
sudo pip-3.5 install flask uwsgi
# init.d の設定
vim init.sh
chmod +x init.sh
sudo ln -s /var/app/init.sh /etc/init.d/uwsgi
# Nginx の設定
sudo ln -s /var/app/nginx.conf /etc/nginx/conf.d/uwsgi.conf
sudo vim /etc/nginx/nginx.conf # 実行ユーザを root に変更 & デフォルトサーバを消す
# 自動起動設定
sudo chkconfig uwsgi on
sudo chkconfig nginx on
# サービス起動
sudo service uwsgi start
sudo service nginx start
# 確認
curl localhost
#!/bin/bash
#
# uWSGI
#
# chkconfig: - 58 74
# Source function library.
. /etc/init.d/functions
PATH=$PATH:/usr/local/bin
prog=uwsgi
lockfile=/var/lock/subsys/$prog
app_dir=/var/app
start() {
echo -n $"Starting $prog: "
daemon $prog --socket $app_dir/app.sock --wsgi-file $app_dir/app.py
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
}
stop() {
echo -n $"Shutting down $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
server {
location / {
include uwsgi_params;
uwsgi_pass unix:/var/app/app.sock;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment