From d20fdb8fa25a052a4267f80b54eb4213cd414d08 Mon Sep 17 00:00:00 2001
From: Miguel Grinberg <miguel.grinberg@gmail.com>
Date: Sat, 14 Oct 2017 00:03:00 -0700
Subject: [PATCH] Chapter 17: Deployment on Linux (v0.17)

---
 Vagrantfile                          |  7 ++++++
 deployment/nginx/microblog           | 37 ++++++++++++++++++++++++++++
 deployment/supervisor/microblog.conf |  8 ++++++
 3 files changed, 52 insertions(+)
 create mode 100644 Vagrantfile
 create mode 100644 deployment/nginx/microblog
 create mode 100644 deployment/supervisor/microblog.conf

diff --git a/Vagrantfile b/Vagrantfile
new file mode 100644
index 0000000..d40c820
--- /dev/null
+++ b/Vagrantfile
@@ -0,0 +1,7 @@
+Vagrant.configure("2") do |config|
+  config.vm.box = "ubuntu/xenial64"
+  config.vm.network "private_network", ip: "192.168.33.10"
+  config.vm.provider "virtualbox" do |vb|
+    vb.memory = "1024"
+  end
+end
diff --git a/deployment/nginx/microblog b/deployment/nginx/microblog
new file mode 100644
index 0000000..784d997
--- /dev/null
+++ b/deployment/nginx/microblog
@@ -0,0 +1,37 @@
+server {
+    # listen on port 80 (http)
+    listen 80;
+    server_name _;
+    location / {
+        # redirect any requests to the same URL but on https
+        return 301 https://$host$request_uri;
+    }
+}
+server {
+    # listen on port 443 (https)
+    listen 443 ssl;
+    server_name _;
+
+    # location of the self-signed SSL certificate
+    ssl_certificate /home/ubuntu/microblog/certs/cert.pem;
+    ssl_certificate_key /home/ubuntu/microblog/certs/key.pem;
+
+    # write access and error logs to /var/log
+    access_log /var/log/microblog_access.log;
+    error_log /var/log/microblog_error.log;
+
+    location / {
+        # forward application requests to the gunicorn server
+        proxy_pass http://localhost:8000;
+        proxy_redirect off;
+        proxy_set_header Host $host;
+        proxy_set_header X-Real-IP $remote_addr;
+        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+    }
+
+    location /static {
+        # handle static files directly, without forwarding to the application
+        alias /home/ubuntu/microblog/app/static;
+        expires 30d;
+    }
+}
diff --git a/deployment/supervisor/microblog.conf b/deployment/supervisor/microblog.conf
new file mode 100644
index 0000000..134f18b
--- /dev/null
+++ b/deployment/supervisor/microblog.conf
@@ -0,0 +1,8 @@
+[program:microblog]
+command=/home/ubuntu/microblog/venv/bin/gunicorn -b localhost:8000 -w 4 microblog:app
+directory=/home/ubuntu/microblog
+user=ubuntu
+autostart=true
+autorestart=true
+stopasgroup=true
+killasgroup=true