Deploying a Vapor web app on DigitalOcean

Setting up the web framework Vapor on Ubuntu

This tutorial will walk you through the steps of setting up a Vapor app on Digital Ocean. This tutorial requires basic knowledge of Unix/Linux commands and assumes you are on macOS. Although this tutorial is for Digital Ocean. It can be applied to any VPS provider, that provides a Ubuntu image setup. Lets get started.

Getting Started with DigitalOcean

DigitalOcean Website
  1. Visit www.digitalocean.com
  2. If you haven’t already got an account with Digital Ocean, sign up here.
  3. Once you have an account with Digital Ocean, sign in and go to the droplets section.
  4. We need to create a new Droplet for our Vapor site, to do this, click the Create Droplet button in the top right corner.
  5. Now we need to configure our new Droplet.
  • In distributions we need to select Ubuntu 14.04.
  • For the size, we can select the smallest $5/mo. This should be fine for this tutorial or any basic site. If you require more server, then you can upgrade your droplet later.
  • In the SSH Keys section, you can provide your droplet with an SSH Key for you machine, this way you won’t be required to provide a password to sign into your droplet. You can find details here on how to generate an SSH key for your machine. However for this tutorial, to keep things simple ,we won’t provide one.
  • Finally we need to choose a hostname, I named mine ‘Vapor-Tutorial’ however your welcome to name your’s whatever you prefer.
Configuring new Droplet

Connecting to the Server

Now we have setup our virtual private server or VPS. We need to connect to it, and setup Vapor.

  1. In order to connect to the server. We need its IP Address and also its password, if you didn’t provide an SSH key. You can obtain your server IP address from your Droplets list in the IP Address column. If you didn’t provide an SSH key, you should have received an email from DigitalOcean with your IP address and password details in it.
  2. Now we have the details we need to connect, we can now SSH into our server. Open Terminal and type
ssh root@<SERVER IP ADDRESS>

Replace <SERVER IP ADDRESS>with your server IP. For example mine was ssh root@162.243.105.170. You may be asked if you want to continue. Type ‘yes’. If prompted to enter password, enter your ssh password you received from DigitalOcean.

Connecting to Server via SSH in Terminal

3. Now we have successfully connected to our server via SSH. You may be prompted to change your password (for security reasons). Just follow the prompts by entering your current password. And entering a new password for the root user account.


Setting up Swift on Ubuntu

  1. In order to use Vapor, we need to be able to compile swift programs and frameworks. Begin by downloading Swift with the following:
curl -O https://swift.org/builds/development/ubuntu1404/swift-DEVELOPMENT-SNAPSHOT-2016-06-06-a/swift-DEVELOPMENT-SNAPSHOT-2016-06-06-a-ubuntu14.04.tar.gz

2. Since we downloaded a tar archieve. We need to decompress it by running:

tar -zxf swift-DEVELOPMENT-SNAPSHOT-2016–06–06-a-ubuntu14.04.tar.gz

3. Rename it to ‘swift’, to make it easier to find later:

mv swift-DEVELOPMENT-SNAPSHOT-2016–06–06-a-ubuntu14.04 swift

4. Before we can start using Swift. We need to install its dependencies it requires to work. First we need to update Ubuntu source list. And then install its dependencies.

sudo apt-get update
sudo apt-get install clang libicu-dev binutils git

5. Now we need to add Swift to our Ubuntu shell so we can easily run it by simply typing ‘swift’.

echo "export PATH=/root/swift/usr/bin:\"\${PATH}\"" >> ~/.bashrc
source ~/.bashrc

This command assumes that you followed the previous steps, and your swift directory is located at /root/swift/.

6. Now lets verify that Swift is working. Type

swift — version

You should see something like below. If you do, all is well and you have successfully installed Swift onto your Ubuntu server.

Verifying Swift is working on Ubuntu

Installing Vapor

Now we have Swift up and running. We can now proceed to install Vapor.

  1. Download the Vapor CLI
curl -L cli.qutheory.io -o vapor

2. Now we have to make Vapor executable and move it into the bin directory, so we can run vapor in our shell with the ‘vapor’ command. Run the following:

chmod +x vapor
sudo mv vapor /usr/local/bin
Installing Vapor CLI

3. Let’s verify Vapor is working correctly by typing:

vapor help
Verifying Vapor

4. You may notice that Vapor is a little on the slow side. This is because Vapor is not compiled. We can make it faster by compiling it. Simply type:

vapor self compile

Compiling your Vapor app

Now that we have successfully installed Swift and Vapor. We can begin by deploying our Vapor app. For this tutorial, I’m going to use the example app provided by the Vapor CLI. However feel free to download any other Vapor app, for example cloning an existing app with ‘git clone’.

  1. Let’s create a new vapor project by running:
vapor new hello-world

2. Now we need to change directory into the vapor app.

cd hello-world

3. Now we can build our vapour app, as follows:

vapor build
Building and compiling Vapor app

If you successfully managed to compile your vapor app without any errors, then congrats, you can skip the next section. However chances are if you chose the smallest option with 512MB of ram, you may encourger a weird error, ‘Error: Could not build project.’. Don’t panic, we will fix this in the next section.

Increasing your Swap Size

If while compiling you encountered the following error:

This means that while compiling your project, Swift was unable to allocate the memory it required to run. Since on DigitalOcean we chose the option with the a small 512MB of RAM. Our server couldn’t provide enough memory to Swift, and thus it failed. Your first thought maybe to think that we may need to upgrade our server to a more expensive plan. But we have an alternative solution that won’t cost any more money. Since Swift ran out of memory, we can use part of our hard drive space to act as virtual memory for our server. This is known as a Swap. We need to increase the size of our Swap in order for Swift to run properly.

  1. We need to turn off all existing swaps by typing:
sudo swapoff -a

2. Resize the Swap to 1GB, this should be enough memory for Swift, as follows:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

3. Make our new Swapfile usable, by running:

sudo mkswap /swapfile

4. Turn on the swapfile

sudo swapon /swapfile

Now we have added a Swap of 1GB. Rerun vapor build again. And if everything worked, it should compile successfully.

Run Vapor App

Now we have successfully compiled our vapor project. We can finally run it and see the fruits of our labour in the browser.

  1. To run a vapor app. We need to run the command ‘vapor run’. We also specify the port we want vapor to run on. In this case, we are using port 80, this is the port that web browsers default to when you enter a site into the browser.
vapor run --port=80

2. Now visit your server IP in your browser of choice. You should see your vapor web app.

Congrats. You have successfully deployed a Vapor app written in Swift on your server. Remember that you will be charged $5 per month, for having your droplet. So don’t forget to delete it after you are finished.