Showing posts with label DigitalOcean. Show all posts
Showing posts with label DigitalOcean. Show all posts

Saturday, June 18, 2016

Paid to exercise

We live in a weird world. Recently, I've been getting paid to exercise.

My company has partnered with another company called Jiff. After I registered my fitbit account to Jiff, Jiff gives me points based on how many steps I take and how much I sleep. I can then redeem those points for prepaid cards or I can enter raffles.

Here are some numbers for you:

  • 600 points will give you a $20 gift card.
  • 50 points will give you a raffle ticket for things like an ipad mini or a new fitbit.

For the last few months, I have gotten around 500 points per month. It could be a little bit higher if 1) I always remembered to wear my fitbit and 2) I entered things into a food log (which I always forget to do). All in all, it takes me a little over a month to make $20.

So far, I've entered a couple of raffles (but haven't won anything yet) and I have gotten 1 visa prepaid debit card.

I had a little trouble using the virtual $20 prepaid card. First, I wanted to use it with iTalki. But I found out that there is a processing fee. If I used it there, I'd have a remaining balance, and then I'd be stuck trying to figure out how to spend the leftover balance. Then I tried to buy some credits for my digital ocean account. For some reason, the charge was rejected. Finally, I successfully was able to spend it all on credits for indoor hoops.

It is fitting that I was able to use money generated from my fitness activities to do more exercise.

Saturday, June 11, 2016

Nginx Gunicorn Flask stack

I followed this wonderful guide to set up my flask + gunicorn + nginx application jjjdddfff.com on digital ocean. If you want to do the same, you should probably go there. Here are some additional notes that I took for myself.

What is the stack made of?

nginx is your http server. You tell the nginx server what port to listen to and where to pass requests. It can handle some requests on its own, like serving static resources like images, or redirects from one url to another. If it doesn't handle a request on its own, it can pass it to your app server, which in my case is gunicorn.

gunicorn is your python app server. It translates http requests into wsgi (web server gateway interface) compatible requests, which are forwarded to whatever will process the request.

flask is a python web framework. It will contain all the business logic of creating a response for a given request.

An alternative stack might be something like apache/uWsgi/django. Because it's all new to me, so I can't say with any authority which is better. I just needed to start somewhere, and after googling/reading a bit, I settled on this stack. At the worst, I think all of the pieces are plug and play. If I am unhappy with any piece of the puzzle, I don't think it will be too much trouble to switch out pieces of the stack. Right?

What do you need to do

You should look to the guide for the explicit steps, but I think it boils down to the following. First, install the software you need (nginx and virtualenv) and then within a virtual envinronment (gunicorn and flask). Then you need to write a couple of scripts.

Configure a web server (nginx)

In script, /etc/nginx/sites-enabled/homepage, I set up my web server (nginx) to listen to http requests and pass them to our app server (gunicorn) via a sock file.

server {
    listen 80;
    server_name jjjdddfff.com;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/username/homepage/homepage.sock;
    }
}
server {
    server_name www.jjjdddfff.com;
    return 301 $scheme://jjjdddfff.com$request_uri;
}

Set up application server (gunicorn)

In script /etc/init/homepage.conf, I created an upstart file that tells ubuntu to start your web application (homepage) that is bound to that "sock" file used in the web server.

description "Gunicorn application server running homepage"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid username
setgid www-data

script
    cd /home/username/homepage
    . venv/bin/activate
    gunicorn --workers 1 --bind unix:homepage.sock -m 007 homepage
end script

Create a web application (flask)

In script, /home/username/homepage/homepage.py, I set up a flask application which will handle all the business logic of processing requests.

from flask import Flask
application = Flask(__name__)

@application.route("/")
def home():
    return "Hello world"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

Some helpful commands

Test the web server.

sudo nginx -t
sudo service nginx restart

Test the application server.

sudo stop homepage
sudo start homepage

Sunday, June 5, 2016

My Virtual Private Server

I bought a server

As a professional computer programmer I have written and deployed a lot of code. Almost all of this code has run on machines that someone else had set up for me. This is a gap of knowledge and understanding and experience that I want to shrink.

This weekend, I made a step towards reducing my knowledge gap by purchasing access to a VPS (virtual private server) from Digital Ocean. With it, I can now start up a linux server "in the cloud" in a couple of minutes. I didn't and don't fully understand what that meant (because of my previously noted knowledge gap), but I signed up for an account and bought some credits anyway.

What did I buy?

I bought something that Digital Ocean calls 'droplets'. Droplets are a virtual server that has an operating system, like Ubuntu, and a set of resources like a hard drive, RAM, bandwidth and an IP address. You can then connect to your droplet via ssh and do whatever unix stuff you like.

I bought the cheapest one which is an Ubuntu server with 512 MB of RAM, 20 GB hard disk and 1000 GB of transfer. It costs $5/month or .007 cents per hour. I used a coupon code, for $10, so I won't start paying for a little while.

For now, I'm still in the process of configuring it. Until I have something like a web server running on it, I have been shutting down the server whenever I am not actively working on it. I think this will save me some cents.

Oh yeah, like I said, I'm a dummy - so without understanding things, I also bought a domain name from google domains. It's jjjdddfff.com. I'm still learning how the server works, so getting a domain name is a bit premature, and money not well spent. Oh well, I guess it'll be motivation to get something working sooner than later.

What have I done so far

So far, I've followed some of digital ocean's tutorials and done the basics. I used ssh to connect to my virtual server from my home machine. I added a non root unix user that has sudo privileges. I added some firewalls.

Next, I plan on installing components for a web stack. And after that, who knows?