Skip to content

Instantly share code, notes, and snippets.

@wesbarnett
Last active July 4, 2019 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesbarnett/169afe97e47628bcb6342552a0c13249 to your computer and use it in GitHub Desktop.
Save wesbarnett/169afe97e47628bcb6342552a0c13249 to your computer and use it in GitHub Desktop.

Server setup

AWS

  1. Visit https://aws.amazon.com and sign up for an account.
  2. Install and configure the aws-cli program according to here.

Then do:

#!/bin/bash
set -e

tag="insight"
security_group="insight"

aws ec2 create-security-group --group-name $security_group --description "My security group"
aws ec2 authorize-security-group-ingress --group-name $security_group --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name $security_group --protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name $security_group --protocol tcp --port 80 --cidr 0.0.0.0/0

aws ec2 run-instances --key-name "aws" --image-id ami-026c8acd92718196b --count 1 --instance-type t2.nano --security-groups $security_group --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value='$tag'}]'

instance_id=$(aws ec2 describe-instances --filters Name=tag:Name,Values="$tag" --query 'Reservations[*].Instances[*].[InstanceId]' --output text)
echo $instance_id

state="pending"
while [ "$state" == "pending" ]; do
    state=$(aws ec2 describe-instances --instance-id $instance_id | jq '.Reservations[0].Instances[0].State.Name')
    echo "state = $state"
    echo "Instance is not yet running. Pausing for 30 seconds."
    sleep 30
done

sleep 5
ip=$(aws ec2 allocate-address | jq -r '.PublicIp')
aws ec2 associate-address --public-ip $ip --instance-id $instance_id

echo "You can now login:"
echo "ssh -i ~/.ssh/aws.pem ubuntu@$ip"

Domain name

  1. Go to https://namecheap.com and create an account.
  2. Search for a domain name and add to cart and purchase.
  3. Go to the account dashboard at https://ap.www.namecheap.com/.
  4. Next to your domain name click "Manage" then click "Advanced DNS".
  5. Click "Add new record" and create an A Record for host @ with the IP address from above. Click the checkmark to save.

Setup flask and gunicorn

  1. ssh into the instance you created earlier:
ssh ubuntu@ipaddress
  1. Create a directory named app. Inside this directory create a file named __init__.py with the following contents:
from flask import Flask

app = Flask(__name__)

from app import routes
  1. Also in app create a file named routes.py with the following contents:
from app import app

@app.route('/')
def index():
    return "It works!"

After doing the above the file tree should look like this

.
└── app
    ├── __init__.py
    └── routes.py
  1. Install flask and gunicorn. Then in the directory above app run gunicorn as follows:
gunicorn -b 0.0.0.0:8080 app:app

In a separate terminal, ssh into the instance and do:

curl localhost:8080

You should see It works!.

  1. Now that it's successfull you can kill gunicorn with CTRL+C and start it in daemon mode:
gunicorn -D -b 0.0.0.0:8080 app:app

When you modify anything in your flask project, simply kill gunicorn and rerun the above command.

nginx

  1. Install nginx.

  2. Create a file at /etc/nginx/nginx.conf with the following contents:

worker_processes                auto;

error_log                       /var/log/nginx/error.log warn;
pid                             /var/run/nginx.pid;

events {
    worker_connections          1024;
}

http {
    include                     /etc/nginx/mime.types;
    default_type                application/octet-stream;
    sendfile                    on;
    access_log                  /var/log/nginx/access.log;
    keepalive_timeout           3000;

    server {

        listen 80;
        listen [::]:80;
        server_name test.wbarnett.us;

        location / {
            proxy_set_header Host              $host;
            proxy_set_header X-Real-IP         $remote_addr;
            proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host              $http_host;
            proxy_pass http://localhost:8080;

        }
    }
}
  1. Restart nginx.
  2. Now visit your domain name in a web browser. You should see "It works!".

certbot

Follow the instructions located here: https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment