Deploying to DigitalOcean¶
This guide will walk you through deploying a simple Hello, world Vapor application to a Droplet. To follow this guide, you must have a DigitalOcean account with billing configured.
Let's start by installing Swift on a Linux server. Use the create menu to create a new Droplet.
Under distributions, select Ubuntu 22.04 LTS. The following guide will use this version as an example.
You may select any Linux distribution with a version that Swift supports. At the time of writing, Swift 5.7.3 supports Ubuntu 18.04, 20.04, 22.04, CentOS 7, and Amazon Linux 2. You can check which operating systems are officially supported on the Swift Releases page.
After selecting the distribution, choose any plan and datacenter region you prefer. Then setup an SSH key to access the server after it is created. Finally, click create Droplet and wait for the new server to spin up.
Once the new server is ready, hover over the Droplet's IP address and click copy.
Open your terminal and connect to the server as root using SSH.
DigitalOcean has an in-depth guide for initial server setup on Ubuntu 22.04. This guide will quickly cover the basics.
Allow OpenSSH through the firewall and enable it.
ufw allow OpenSSH ufw enable
Create a new user besides
root. This guide calls the new user
Allow the newly created user to use
usermod -aG sudo vapor
Copy the root user's authorized SSH keys to the newly created user. This will allow you to SSH in as the new user.
rsync --archive --chown=vapor:vapor ~/.ssh /home/vapor
Finally, exit the current SSH session and login as the newly created user.
exit ssh vapor@your_server_ip
Now that you've created a new Ubuntu server and logged in as a non-root user you can install Swift.
Install Swift's required dependencies.
sudo apt-get update sudo apt-get install binutils git gnupg2 libc6-dev libcurl4-openssl-dev \ libedit2 libgcc-9-dev libpython3.8 libsqlite3-0 libstdc++-9-dev \ libxml2-dev libz3-dev pkg-config tzdata unzip zlib1g-dev
Download Swift Toolchain¶
This guide will install Swift 5.7.3. Visit the Swift Releases page for a link to latest release. Copy the download link for Ubuntu 22.04.
Download and decompress the Swift toolchain.
wget https://download.swift.org/swift-5.7.3-release/ubuntu2204/swift-5.7.3-RELEASE/swift-5.7.3-RELEASE-ubuntu22.04.tar.gz tar xzf swift-5.7.3-RELEASE-ubuntu22.04.tar.gz
Swift's Using Downloads guide includes information on how to verify downloads using PGP signatures.
Install Swift Toolchain¶
Move Swift somewhere easy to acess. This guide will use
/swift with each compiler version in a subfolder.
sudo mkdir /swift sudo mv swift-5.7.3-RELEASE-ubuntu22.04 /swift/5.7.3
Add Swift to
/usr/bin so it can be executed by
sudo ln -s /swift/5.7.3/usr/bin/swift /usr/bin/swift
Verify that Swift was installed correctly.
Install Vapor Using the Vapor Toolbox¶
Now that Swift is installed, let's install Vapor using the Vapor Toolbox. You will need to build the toolbox from source. View the toolbox's releases on GitHub to find the latest version. In this example, we are using 18.6.0.
Clone and Build Vapor¶
Clone the Vapor Toolbox repository.
git clone https://github.com/vapor/toolbox.git
Checkout the latest release.
cd toolbox git checkout 18.6.0
Build Vapor and move the binary into your path.
swift build -c release --disable-sandbox --enable-test-discovery sudo mv .build/release/vapor /usr/local/bin
Create a Vapor Project¶
Use the Toolbox's new project command to initiate a project.
vapor new HelloWorld -n
-n flag gives you a bare bones template by automatically answering no to all questions.
Once the command finishes, change into the newly created folder:
Open HTTP Port¶
In order to access Vapor on your server, open an HTTP port.
sudo ufw allow 8080
Now that Vapor is setup and we have an open port, let's run it.
vapor run serve --hostname 0.0.0.0 --port 8080
Visit your server's IP via browser or local terminal and you should see "It works!". The IP address is
220.127.116.11 in this example.
$ curl http://18.104.22.168:8080 It works!
Back on your server, you should see logs for the test request.
[ NOTICE ] Server starting on http://0.0.0.0:8080 [ INFO ] GET /
CTRL+C to quit the server. It may take a second to shutdown.
Congratulations on getting your Vapor app running on a DigitalOcean Droplet!
The rest of this guide points to additional resources to improve your deployment.
Supervisor is a process control system that can run and monitor your Vapor executable. With supervisor setup, your app can automatically start when the server boots and be restarted in case it crashes. Learn more about Supervisor.
Nginx is an extremely fast, battle tested, and easy-to-configure HTTP server and proxy. While Vapor supports directly serving HTTP requests, proxying behind Nginx can provide increased performance, security, and ease-of-use. Learn more about Nginx.