- Visit https://aws.amazon.com and sign up for an account.
- 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"
- Go to https://namecheap.com and create an account.
- Search for a domain name and add to cart and purchase.
- Go to the account dashboard at https://ap.www.namecheap.com/.
- Next to your domain name click "Manage" then click "Advanced DNS".
- Click "Add new record" and create an
A Record
for host@
with the IP address from above. Click the checkmark to save.
- ssh into the instance you created earlier:
ssh ubuntu@ipaddress
- 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
- Also in
app
create a file namedroutes.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
- Install flask and gunicorn. Then in the directory above
app
rungunicorn
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!
.
- Now that it's successfull you can kill
gunicorn
withCTRL+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.
-
Install nginx.
-
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;
}
}
}
- Restart nginx.
- Now visit your domain name in a web browser. You should see "It works!".
Follow the instructions located here: https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx