Skip to content

Instantly share code, notes, and snippets.

@joeyism
Created November 7, 2022 15:50
Show Gist options
  • Save joeyism/519858720be4ab24229eb74e049cf556 to your computer and use it in GitHub Desktop.
Save joeyism/519858720be4ab24229eb74e049cf556 to your computer and use it in GitHub Desktop.
Deploying Flask to AWS
#!/bin/bash
CURRENT_ID=$(dig +short myip.opendns.com @resolver1.opendns.com)
read -p "Server Name [$CURRENT_ID]: " SERVER_NAME
SERVER_NAME=${SERVER_NAME:-${CURRENT_ID}}
CURRENT_WORK_DIR=$(pwd)
read -e -p "Working Directory [$CURRENT_WORK_DIR]: " WORKING_DIR
WORKING_DIR=${WORKING_DIR:-${CURRENT_WORK_DIR}}
read -e -p "GUnicorn Executable (i.e. /usr/bin/gunicorn): " GUNICORN_EXEC
GUNICORN_EXEC=$(readlink -f ${GUNICORN_EXEC})
read -p "Flask Executable (i.e. app:app): " FLASK_APP
echo "Installing NGINX"
sudo apt install -y nginx
echo "Writing to /etc/nginx/sites-enabled/flask_aws"
sudo bash -c "cat <<EOF > /etc/nginx/sites-enabled/flask_aws
server {
listen 80;
server_name ${SERVER_NAME};
location / {
proxy_pass http://unix:${WORKING_DIR}/flask_aws.sock;
}
}
EOF"
echo "Restarting NGINX"
sudo service nginx restart
echo "Adding Flask App as a Service"
sudo bash -c "cat <<EOF > /etc/systemd/system/flask_aws.service
[Unit]
Description=AWS Flask app
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=${WORKING_DIR}
ExecStart=${GUNICORN_EXEC} --bind unix:flask_aws.sock ${FLASK_APP}
EOF"
echo "Adding and Reloading Service"
sudo systemctl daemon-reload
sudo service flask_aws restart
sudo service nginx restart
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment