Deploy Vapor 2 app With Nginx and Supervisor on AWS or any cloud platform running Ubuntu 16.04

Amandeep Singh
Jun 22, 2017 · 4 min read

This article guide you by following steps one by one:

  1. Install and Run Nginx server (to run server on ubuntu).
  2. Install vapor latest stable version.
  3. Deploy your app on cloud.
  4. Config Nginx’s “default” file to let Nginx Serve your Vapor app.
  5. Install and Run Supervisor (so that server will be in running state even when you close your terminal)

This article is extending article written by ankit with few changes to work with Vapor 2.

Install Nginx :

Run these commands to install Nginx:

sudo apt-get update
sudo apt-get install nginx

Add a rule to firewall to allow TCP connections on port 80. (Only needs to be done if firewall is enabled)

sudo ufw allow 'Nginx HTTP'
sudo ufw status

Check if Nginx is running systemctl status nginx

Output should look something like this, saying Active: active (running)

Now test your Nginx web page working, by going to browser and opening the server domain or ip, and you will be able to see the webpage like this:

If you are not able to see this web page at this moment, then may be you missed something. For more details follow this page

Install and Run Vapor :

Run these commands to install swift and latest stable version of vapor:

eval "$(curl -sL"
sudo apt-get install swift vapor
eval "$(curl -sL"

If this will outputs “Compatible with Vapor 2”, you did a great job.

Note: if commands didn’t worked for you then don’t copy and paste them, try to type them because of “”.

Now you can either create a new Vapor project or clone it using git from you github or bitbuket repo.

At this step please dont forget to install dependencies: example to use postgresql, I have to install libpq using command:

sudo apt-get install libpg-dev

Otherwise, when we will try to build our vapor app, it will throw errors for these dependencies, If you missed some dependencies to install before building vapor app, don’t worry just note down the dependencies you need and install them and rebuild your vapor app.

Configure “default” file to let Nginx serve Vapor app:

Go to “/etc/nignx/sites-available” and open “default” file with sudo permission using command:

sudo vim default

and edit the file to look like below:

server {        server_name;
listen 80 default_server;
listen [::]:80 default_server;
root /home/userName/appName/Public; try_files $uri @proxy; location @proxy {
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header Server;
proxy_connect_timeout 3s;
proxy_read_timeout 10s;

Note: Replace userName everywhere in the code by your system user name who is having sudo previlage, and appName with the name of your app(folder). Now you can restart Nginx and build & run your vapor app using these commands:

sudo systemctl restart nginx
cd ~/appName
vapor build
vapor run

Now again open your server domain or ip in the browser and check your vapor app is running, Great!

But what if you close your terminal, your vapor app will also be terminated.

To keep your app running even if you close your terminal and let it run in background and restart the app if got killed. We need to use Supervisor.

Install and Setup Supervisor :

Run these commands to install supervisor

sudo apt-get update
sudo apt-get install supervisor

Now go to “/etc/supervisor/conf.d” and create a new conf file for your app as shown below from your root.

cd /etc/supervisor/conf.d

sudo vim appName.conf

Edit the appName.conf to look like:

command=vapor run --env=production
directory=/home/userName/appName/ # Put correct path here
user=userName # Put username here

Save these settings and close the file.

Now load the configuration file and run “app” in background using supervisor by following commands:

sudo supervisorctl reread
sudo supervisorctl add appName
sudo supervisorctl start appName

You can check the status of your app with “sudo supervisorctl status”

Now you can close the terminal and check your website by going to your server domain or ip in your browser. It will running your Vapor app now.

To secure your server using Let’s Encrypt please follow this tutorial:

Amandeep Singh

Written by

Swift Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade