Skip to content

Instantly share code, notes, and snippets.

@dreamorosi
Last active April 27, 2018 01:20
Show Gist options
  • Save dreamorosi/ab97dd5fb0114e8686862796d7a9bff1 to your computer and use it in GitHub Desktop.
Save dreamorosi/ab97dd5fb0114e8686862796d7a9bff1 to your computer and use it in GitHub Desktop.
Flask - Gunicorn - upstart socket API configuration
#!/bin/bash
sudo rm -rf /home/ec2-user/api
sudo rm /etc/nginx/conf.d/api.conf
sudo rm /etc/init/api.conf
echo "Done."
sudo reboot
#!/bin/bash
STEP1=$'\n\n--> System configured 1/5\n\n'
STEP2=$'\n\n--> App installed 2/5\n\n'
STEP3=$'\n\n--> Added upstart conf 3/5\n\n'
STEP4=$'\n\n--> Added Nginx conf 4/5\n\n'
STEP5=$'\n\n--> Installed opencv conf 5/5\n\n'
STEP6=$'\n\n--> Done.\n\n'
# Install system dependencies
sudo yum update -y
sudo yum install python-pip python-devel nginx git cmake numpy -y
sudo yum groupinstall "Development Tools" -y
sudo chkconfig nginx on
sudo pip install virtualenv
echo "$STEP1"
# Install app environment and dependencies
git clone https://github.com/dreamorosi/flask-gunicorn-api.git api
virtualenv api/venv
. api/venv/bin/activate
pip install -r api/requirements.txt
deactivate
echo "$STEP2"
# Set up upstart script
wget -O api.conf https://gist.github.com/dreamorosi/ab97dd5fb0114e8686862796d7a9bff1/raw/upstart_app.conf
sudo mv ./api.conf /etc/init/
echo "$STEP3"
# Set up nginx conf
wget -O api.conf https://gist.github.com/dreamorosi/ab97dd5fb0114e8686862796d7a9bff1/raw/nginx_app.conf
public_ip=$( dig +short myip.opendns.com @resolver1.opendns.com )
sed -r 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b'/$public_ip/ -i ./api.conf
sudo mv ./api.conf /etc/nginx/conf.d/
echo "$STEP4"
# Install opencv
wget -o- https://github.com/opencv/opencv/archive/2.4.13.5.zip
unzip 2.4.13.5.zip
mkdir opencv-2.4.13.5/build
cd opencv-2.4.13.5/build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_EXECUTABLE=/usr/bin/python2.7 -D BUILD_EXAMPLES=ON ..
make -j36
sudo make install
ln opencv-2.4.13.5/build/lib/cv2.so api/venv/lib/python2.7/site-packages/
echo "$STEP5"
echo "$STEP6"
sudo reboot
# /etc/nginx/conf.d/app.conf
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://unix:/home/ec2-user/api/api.sock;
}
}
#!/bin/bash
sudo restart api
sudo service nginx restart
sudo chown nginx:nginx /home/ec2-user/api/api.sock
#!/bin/bash
sudo start api
public_ip=$( dig +short myip.opendns.com @resolver1.opendns.com )
sudo sed -r 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b'/$public_ip/ -i /etc/nginx/conf.d/api.conf
sudo service nginx start
sudo chown nginx:nginx /home/ec2-user/api/api.sock
# /etc/init/app.conf
description "Gunicorn application server running app"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
env PATH=/home/ec2-user/api/venv/bin
env PROGRAM_NAME="api"
env USERNAME="ec2-user"
# Main script to be run
script
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Ready to run..." >> /var/log/$PROGRAM_NAME.sys.log
export HOME="/home/ec2-user"
echo $$ > /var/run/$PROGRAM_NAME.pid
cd /home/ec2-user/api
# exec sudo -u ec2-user gunicorn --workers 3 --bind unix:api.sock -m 000 wsgi >> /var/log/$PROGRAM_NAME.sys.log 2>&1
# exec su -s /bin/sh -c 'exec "$0" "$@"' ec2-user -- gunicorn --workers 3 --bind unix:api.sock -m 000 wsgi >> /var/log/$PROGRAM_NAME.sys.log 2>&1
exec gunicorn --workers 3 --bind unix:api.sock -m 000 wsgi:app >> /var/log/$PROGRAM_NAME.sys.log 2>&1
end script
# Script for debug purpose, run before starting
pre-start script
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/$PROGRAM_NAME.sys.log
end script
# Script for debug purpose, run before stopping
pre-stop script
rm /var/run/$PROGRAM_NAME.pid/
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$PROGRAM_NAME.sys.log
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment