Showing posts with label Ubuntu. Show all posts
Showing posts with label Ubuntu. Show all posts

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?

Friday, March 29, 2013

Still on the search for my next computer

Like most programmers, once I get stuck on a problem, I can't stop thinking about it until I find a solution.  These days I have been shopping for a new computer.

My current computer, a HP DV2000 laptop, circa 2006, works OK.  Over the years, I have only made two upgrades: I doubled my RAM from one to two GB and I have switched my primary operating system to Linux Ubuntu over Windows XP.  For reference, my wife and I actually have the same exact computer, except that she didn't make any upgrades.  Amazingly, both computers have been working fine for all of these years.  (The only real deterioration is that neither of our batteries hold much of a charge, which could be fixed for 20 dollars).

So, even though my current laptop functions just fine, in the last couple of months, I thought it was about time that I (and my wife) upgrade our machines.  I'm not exactly sure why.  Maybe it's well placed advertisements, maybe it's my wife's subtle coaxing, or maybe it's the stark difference between my dual display powerhouse work setup versus my aging home laptop - but I think it's about time for an upgrade.

My wife says that I am not a good shopper, and she is right.  I know that I only have a finite amount of energy per day, and finding deals is something that I just do not want to spend any attention on.  If I'm shopping for a pair of jeans, I'll walk into the Gap and buy the first pair that sort of fit me OK.  In contrast, my wife will start browsing online for Jeans, eventually go to the Gap, take a hard look at every pair they have, go home, look for deals online, order three different pairs of jeans, try each pair on a few times, return two of them, and  voila, she has a pair of jeans.  In the end, it's clear that she looks better than I do.

But for a new computer, I have taken on her exhaustive approach and I have been scouring the world wide web for my and my wife's next laptop.  For my own reference, here are the upgrades from my current laptop that I am hoping to get for both my wife and I. 
  • 2GB to 8 GB of RAM. 
  • 80 GB hard drive to at least a 128 GB solid state drive.   
  • 5.5 Lbs to less than 4lbs total weight.
  • S-video or VGA output to HDMI output.
The one place we diverge is in the operating system.  My wife is looking for a Windows machine, but I'd like to use OS X or Ubuntu.

In the end, I decided to give the 13.3-Inch Apple MacBook Air a try.  I just put in an order for a refurbished model for  $999 ($1087 with tax).  It only has 4 GB of RAM, which I couldn't upgrade, because it is refurbished.  This will be a learning experience for me, because I have never owned a Mac before.

For my wife, we decided to get the 15" Samsung Series 9 a try. It has 8 GB of RAM, runs Windows 7, and is 3.6 lbs.

It's been a quick 7 years since our last laptop purchase, and I hope these new machines could serve us for a long time as well.  If they last us through 2020, that would be pretty remarkable.

I started this post saying that once I get a problem in my head, I can't stop thinking about it until I reach some conclusion.  After several days of shopping, reading reviews, and evaluating my needs, I couldn't take shopping anymore.  So, the only resolution was to make a decision and commit to one choice. I'm glad I made a choice so that I can go back to building apps, learning about coding and programming and doing anything else besides shopping.

Sunday, March 3, 2013

Taxes

I just did my taxes today.  Here are some observations.

Turbotax is not linux friendly.
I am running Ubuntu and my default browser is chromium.  After logging into their site, I got a warning saying that I had to use either Internet Explorer, Firefox, Safari or Chrome.  I guess Chromium isn't the same as Chrome. Ok, no problem, I have firefox.  I switched browsers, logged in again, but was denied access again.  I didn't feel like debugging the problem, so I decided to log off of my ubuntu operating system and use Windows.  That's what dual booting is all about.  So there's one more reason why I can't wipe Windows from my life completely.

Turbotax is pretty user friendly.
I used Turbotax last year, and it was able to remember some of my information.  Since none of the major financial details in my life changed at all (lived in the same place, worked for the same employers, and so on), it was great that Turbotax was able to auto-populate a lot of details.  I'm not sure why, but it was able to import alot of my wife's information while I had to input some more items, but it was more or less very easy to use.  I finished inputting the details in about 15 minutes.

No default is the default.
For many screens in the Turbotax wizard there are a series of options with the final one being none of the above.  For example, they might ask, have you worked over seas, or worked in the military, or made money gambling etc ... and you can check yes to one or more of these.  But there's also another choice at the end that says, None of the above.  I hate it when that None of the above choice isn't selected by default.  I guess that's just a pet-peave of mine.  If it weren't for having to click on that choice, I might have been able to finish my taxes in 10 minutes.

Sunday, January 13, 2013

Next computer

I'd really like a new computer.  Below, I listed some contenders.
The System 76 is the biggest, heaviest, and cheapest, and I like Ubuntu OS the best.

System 76 Lemur 4 Laptop
  • Preloaded with Ubuntu
  • $849 (with the RAM and Hard drive specs outlined below)
  • 4.5 lbs
  • 14.1 inch display
  • 8 GB DDR3 SDRAM 1600 Mhz, 2 x 4 GB
  • 120 GB SSD Hard drive
  • OS X Mountain Lion 
  • $1499 (with Specs below)
  • 4.5 lbs
  • 13.3 inch display
  • 8 GB DDR3 SDRAM 1600 Mhz, 2 x 4 GB
  • 128 GB SSD Hard Drive
  • OS X Mountain Lion
  • $1299 (with Specs below)
  • 2.96 lbs
  • 13.3 inch display
  • 8 GB DDR3 SDRAM 1600Mhz
  • 128 GB SSD Hard Drive
Dell XPS 13 Laptop, Developer Edition
  • Preloaded with Ubuntu
  • Has "developer profiles", like one for Ruby development or Android development, that makes installing all of the components needed to develop a specific type of application seamless.
  • $1549
  • 2.99 lbs
  • 13.3 inch display
  • 8 GB DDR3L RAM
  • 256 GB SSD Hard drive

Gnome 3

For my home computer, I have been using the Ubuntu operating system with the Unity interface for the last year.  I chose the Unity interface, because it was the default one chosen for me.  About a week ago, I was having some issues with remote desktops not opening nicely in Ubuntu+Unity so I started using the Gnome 3 interface.  Here are some notes on window management with Gnome and Ubuntu.

Workspaces
For those who don't know, "Workspaces" are a way to group windows.  I typically put all social (time-wasting) programs into one workspace, things I'm actively working with in another window, and some reference material in another workspace.  Here is a screen shot of my desktop showing active windows in the current workspace and a few other workspaces shown on the right.

You can see your workspaces by:
  1. Move your mouse to the top left hand corner of the screen.
  2. Press Alt+F1.
  3. Press the Windows button.
You can switch workspaces, by pressing Alt+Ctrl+Up or Down direction.

You can switch between windows that are in any workspace by pressing Alt+Tab. I'm not sure if there is a way to make alt+tab to work for only windows in the current workspace.

Window size management
To maximize your window:
  1. Drag the window to the top of the screen.  If you hold it there for a moment, and then release, it just works.
  2. Press Windows+Up direction.
To change the window to half of the screen:
  1. Drag the window to the left or right of the screen.
  2. Press Windows+Left or Right direction.
Other Resources
http://www.filiwiese.com/installing-gnome-on-ubuntu-12-04-precise-pangolin/