CrunchTime

|Don't mind the sarcasm|


Deployed to the WorldWideWeb
// flask, nginx, gunicorn


Finally deployed my blog to the webs server. Here's how you can finagle your way to deploying your own weblog using Flask --> Gunicorn --> Nginx

This quick tutorial consists of creating a basic flask app, and serving via gunicorn & nginx. Instructions for installing and setting up everything is provided. This is done on ubuntu 14.04 LTS, commands for other OS's will be added every once in a while.

It's not reasonable to run python web apps directly from the system's python installation, so we'll have to start by installing Virtualenv, a python virtual environment container. Everything installed within a virtualenv environment is installed upon a self-contained python installation.

sudo apt-get install virtualenv-python

To create our first container is simple:

virtualenv blog

This will create a new python environment for us to work with. To active our new container is also quite simple:

cd blog
source bin/activate

You'll notice the command line has changed it's tag to something similar to the following:

(blog)root@yourserver:

blog being the new environment we just created. Let's install pip to install python packages, and flask to create our extremely simple flask app to exemplify deploying.

sudo apt-get install python-pip

pip install flask
pip install nano

We installed flask, and nano (a unix commandline text editor). Now we'll create the python file:

nano blog.py

Because the nature of this post is for deploying reasons only I won't be explaining what's going on here:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello world!"

if __name__ == '__main__':
    app.run()

If you run the following command, you should be able to see the words "Hello World!" printed at the address http://127.0.0.1:5000/

python blog.py

W00t! We got it running, but it doesn't end here, this is just the built in flask server to help with debugging. We still need to proxy to Gunicorn and Nginx to see the outside world and process requests on port 80 (http port).

pip install gunicorn
sudo apt-get install nginx

Both applications are now installed, let's see if we can get Gunicorn processing our flask application. In the directly with the python file run:

gunicorn blog:app

You should see something similar to the following:

[2014-11-30 21:10:57 +0000] [8779] [INFO] Starting gunicorn 19.1.1
[2014-11-30 21:10:57 +0000] [8779] [INFO] Listening at: http://127.0.0.1:8000 (8779)
[2014-11-30 21:10:57 +0000] [8779] [INFO] Using worker: sync
[2014-11-30 21:10:57 +0000] [8784] [INFO] Booting worker with pid: 2344

With Gunicorn up and running let's create a quick nginx proxy. For this we need to delete the default nginx configuration in the sites-available directory.

rm /etc/nginx/sites-enabled/default

and then let's create our own ngninx config, the file will be called "blog" inside the sites-available folder.

nano /etc/nginx/sites-available/blog

This is our simple proxy configuration:

server {
    location / {
            proxy_pass http://127.0.0.1:8000;
    }
}

It literally just passes the traffic to gunicorn over localhost at port 8000. Restart ngninx

/etc/init.d/nginx restart

Lastly reload gunicorn to shoot at port 8000.

//generic 
gunicorn pythonfile:module -b localhost:8000 
//for this excercise
gunicorn blog:app -b localhost:8000

Voila, she's -a-rolling. You should be able to go to your domain and see your "hello world!" text. That's it, until next time~

Back to Blogging
// general


Good News!

I'm back to blogging, so get ready for some more hot air and useless text. This new blog runs on

  • Flask
  • Markdown
  • Flatpages
  • Nginx
  • Gunicorn

Basically it's a mostly python stack, with Nginx as a static handler while gunicorn passes requests from browser to the Python scripts through Nginx. In the past this blog has featured articles dealing with industry issues, political mishaps, or even the occasional over-technical post that I doubt people read. Welcome back and enjoy the stay~