Build nginx flask vagrant vbox on windows

Before this post I wrote and article on how to serve flask on ubuntu.
I share with you my first contact with vagrant, on a windows box I build a virtualbox vm that run nginx, and a flask “hello world” application.

here the Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

hostname = "propus"
locale = "en_GB.UTF.8"

# Box
config.vm.box = "ubuntu/trusty64"

# Shared folders
config.vm.synced_folder ".", "/var/www/ovpn"

# Port forwarding
config.vm.network :forwarded_port, guest: 80, host: 8080

# Setup
config.vm.provision :shell, :inline => "touch .hushlogin"
config.vm.provision :shell, :inline => "hostnamectl set-hostname #{hostname} && locale-gen #{locale}"
config.vm.provision :shell, :inline => "apt-get update --fix-missing"
config.vm.provision :shell, :inline => "apt-get install -q -y g++ make git vim"

# Lang
config.vm.provision :shell, :inline => "apt-get install -q -y python python-dev python-distribute python-pip"

# nginx
config.vm.provision :shell, :inline => "apt-get install -q -y nginx"

config.vm.provision :shell, :path => "bootstrap.sh"

end

And here is the bootstrap.sh script content

#!/bin/bash

pip install virtualenv
mkdir -p /var/www/ovpn && cd /var/www/ovpn
mkdir /var/log/uwsgi/
virtualenv -p /usr/bin/python venv
source venv/bin/activate
pip install uwsgi
pip install flask
rm -f /etc/nginx/conf.d/default
cat <> /etc/nginx/conf.d/ovpn

server {
listen 80;
server_name 10.0.1.15;
charset utf-8;
client_max_body_size 75M;

location / { try_files $uri @ovpn; }
location @ovpn {
include uwsgi_params;
uwsgi_pass unix:/tmp/demoapp_uwsgi.sock;
}
}
EOF

cat <>/var/www/ovpn/demoapp_uwsgi.ini
[uwsgi]
#application's base folder
base = /var/www/ovpn

#python module to import
app = hello
module = %(app)

home = %(base)/venv
pythonpath = %(base)

#socket file’s location
socket = /tmp/%n.sock

#permissions for the socket file
chmod-socket = 666

#the variable that holds a flask application inside the module imported at line #6
callable = application

#location of log files
logto = /var/log/uwsgi/%n.log
EOF

/etc/init.d/nginx restart

Place the Vagrantfile and the bootstrap.sh in the same directory.
and from powershell console Run:

vagrant up

This will build the vm image. have a fun !

Build nginx flask vagrant vbox on windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s