Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit ccaff14

Browse files
authored
Add DigitalOcean deployment guide (#8)
1 parent 8043c2a commit ccaff14

7 files changed

+264
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+

digital-ocean.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Deploying to DigitalOcean
2+
3+
This guide will walk you through setting up an Ubuntu virtual machine on a DigitalOcean [Droplet](https://www.digitalocean.com/products/droplets/). To follow this guide, you will need to have a [DigitalOcean](https://www.digitalocean.com) account with billing configured.
4+
5+
## Create Server
6+
7+
Use the create menu to create a new Droplet.
8+
9+
![Create Droplet](images/digital-ocean-create-droplet.png)
10+
11+
Under distributions, select Ubuntu 18.04 LTS.
12+
13+
![Ubuntu Distro](images/digital-ocean-distributions-ubuntu-18.png)
14+
15+
> Note: You may select any version of Linux that Swift supports. You can check which operating systems are officially supported on the [Swift Releases](https://swift.org/download/#releases) page.
16+
17+
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.
18+
19+
Once the new server is ready, hover over the Droplet's IP address and click copy.
20+
21+
![Droplet List](images/digital-ocean-droplet-list.png)
22+
23+
## Initial Setup
24+
25+
Open your terminal and connect to the server as root using SSH.
26+
27+
```sh
28+
ssh root@<server_ip>
29+
```
30+
31+
DigitalOcean has an in-depth guide for [initial server setup on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04). This guide will quickly cover the basics.
32+
33+
### Configure Firewall
34+
35+
Allow OpenSSH through the firewall and enable it.
36+
37+
```sh
38+
ufw allow OpenSSH
39+
ufw enable
40+
```
41+
42+
Then enable a non-root accessible HTTP port.
43+
44+
```sh
45+
ufw allow 8080
46+
```
47+
48+
### Add User
49+
50+
Create a new user besides `root` that will be responsible for running your application. This guide uses a non-root user without access to `sudo` for added security.
51+
52+
The following guides assume the user is named `swift`.
53+
54+
```sh
55+
adduser swift
56+
```
57+
58+
Copy the root user's authorized SSH keys to the newly created user. This will allow you to use SSH (`scp`) as the new user.
59+
60+
```sh
61+
rsync --archive --chown=swift:swift ~/.ssh /home/swift
62+
```
63+
64+
Your DigitalOcean virtual machine is now ready. Continue using the [Ubuntu](ubuntu.md) guide.
70.8 KB
Loading
98.2 KB
Loading

images/digital-ocean-droplet-list.png

43.7 KB
Loading
452 KB
Loading

ubuntu.md

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# Deploying on Ubuntu
2+
3+
Once you have your Ubuntu virtual machine ready, you can deploy your Swift app. This guide assumes you have a fresh install with a non-root user named `swift`. It also assumes both `root` and `swift` are accessible via SSH. For information on setting this up, check out the platform guides:
4+
5+
- [DigitalOcean](digital-ocean.md)
6+
7+
The [packaging](packaging.md) guide provides an overview of available deployment options. This guide takes you through each deployment option step-by-step for Ubuntu specifically. These examples will deploy SwiftNIO's [example HTTP server](https://github.com/apple/swift-nio/tree/master/Sources/NIOHTTP1Server), but you can test with your own project.
8+
9+
- [Binary Deployment](#binary-deployment)
10+
- [Source Deployment](#source-deployment)
11+
12+
## Binary Deployment
13+
14+
This section shows you how to build your app locally and deploy just the binary.
15+
16+
### Build Binaries
17+
18+
The first step is to build your app locally. The easiest way to do this is with Docker. For this example, we'll be deploying SwiftNIO's demo HTTP server. Start by cloning the repository.
19+
20+
```sh
21+
git clone https://github.com/apple/swift-nio.git
22+
cd swift-nio
23+
```
24+
25+
Once inside the project folder, use the following command to build the app though Docker and copy all build arifacts into `.build/install`. Since this example will be deploying to Ubuntu 18.04, the `-bionic` Docker image is used to build.
26+
27+
```sh
28+
docker run --rm \
29+
-v "$PWD:/workspace" \
30+
-w /workspace \
31+
swift:5.2-bionic \
32+
/bin/bash -cl ' \
33+
swift build && \
34+
rm -rf .build/install && mkdir -p .build/install && \
35+
cp -P .build/debug/NIOHTTP1Server .build/install/ && \
36+
cp -P /usr/lib/swift/linux/lib*so* .build/install/'
37+
```
38+
39+
> Tip: If you are building this project for production, use `swift build -c release`, see [building for production](README.md#building-for-production) for more information.
40+
41+
Notice that Swift's shared libraries are being included. This is important since Swift is not ABI stable on Linux. This means Swift programs must run against the shared libraries they were compiled with.
42+
43+
After your project is built, use the following command to create an archive for easy transport to the server.
44+
45+
```sh
46+
tar cvzf hello-world.tar.gz -C .build/install .
47+
```
48+
49+
Next, use `scp` to copy the archive to the deploy server's home folder.
50+
51+
```sh
52+
scp hello-world.tar.gz swift@<server_ip>:~/
53+
```
54+
55+
Once the copy is complete, login to the deploy server.
56+
57+
```sh
58+
ssh swift@<server_ip>
59+
```
60+
61+
Create a new folder to hold the app binaries and decompress the archive.
62+
63+
```sh
64+
mkdir hello-world
65+
tar -xvf hello-world.tar.gz -C hello-world
66+
```
67+
68+
You can now start the executable. Supply the desired IP address and port. Binding to port `80` requires sudo, so we use `8080` instead.
69+
70+
[TODO]: <> (Link to Nginx guide once available for serving on port 80)
71+
72+
```sh
73+
./hello-world/NIOHTTP1Server <server_ip> 8080
74+
```
75+
76+
You may need to install additional system libraries like `libxml` or `tzdata` if your app uses Foundation. The system dependencies installed by Swift's slim docker images are a [good reference](https://github.com/apple/swift-docker/blob/master/5.2/ubuntu/18.04/slim/Dockerfile).
77+
78+
Finally, visit your server's IP via browser or local terminal and you should see a response.
79+
80+
```
81+
$ curl http://<server_ip>:8080
82+
Hello world!
83+
```
84+
85+
Use `CTRL+C` to quit the server.
86+
87+
Congratulations on getting your Swift server app running on Ubuntu!
88+
89+
## Source Deployement
90+
91+
This section shows you how to build and run your project on the deployment server.
92+
93+
## Install Swift
94+
95+
Now that you've created a new Ubuntu server you can install Swift. You must be logged in as `root` (or separate user with `sudo` access) to do this.
96+
97+
```sh
98+
ssh root@<server_ip>
99+
```
100+
101+
### Swift Dependencies
102+
103+
Install Swift's required dependencies.
104+
105+
```sh
106+
sudo apt update
107+
sudo apt install clang libicu-dev build-essential pkg-config
108+
```
109+
110+
### Download Toolchain
111+
112+
This guide will install Swift 5.2. Visit the [Swift Downloads](https://swift.org/download/#releases) page for a link to latest release. Copy the download link for Ubuntu 18.04.
113+
114+
![Download Swift](images/swift-download-ubuntu-18-copy-link.png)
115+
116+
Download and decompress the Swift toolchain.
117+
118+
```sh
119+
wget https://swift.org/builds/swift-5.2-release/ubuntu1804/swift-5.2-RELEASE/swift-5.2-RELEASE-ubuntu18.04.tar.gz
120+
tar xzf swift-5.2-RELEASE-ubuntu18.04.tar.gz
121+
```
122+
123+
> Note: Swift's [Using Downloads](https://swift.org/download/#using-downloads) guide includes information on how to verify downloads using PGP signatures.
124+
125+
### Install Toolchain
126+
127+
Move Swift somewhere easy to acess. This guide will use `/swift` with each compiler version in a subfolder.
128+
129+
```sh
130+
sudo mkdir /swift
131+
sudo mv swift-5.2-RELEASE-ubuntu18.04 /swift/5.2.0
132+
```
133+
134+
Add Swift to `/usr/bin` so it can be executed by `swift` and `root`.
135+
136+
```sh
137+
sudo ln -s /swift/5.2.0/usr/bin/swift /usr/bin/swift
138+
```
139+
140+
Verify that Swift was installed correctly.
141+
142+
```sh
143+
swift --version
144+
```
145+
146+
## Setup Project
147+
148+
Now that Swift is installed, let's clone and compile your project. For this example, we'll be using SwiftNIO's [example HTTP server](https://github.com/apple/swift-nio/tree/master/Sources/NIOHTTP1Server).
149+
150+
First let's install SwiftNIO's system dependencies.
151+
152+
```sh
153+
sudo apt-get install zlib1g-dev
154+
```
155+
156+
### Clone & Build
157+
158+
Now that we're done installing things, we can switch to a non-root user to build and run our application.
159+
160+
```sh
161+
su swift
162+
cd ~
163+
```
164+
165+
Clone the project, then use `swift build` to compile it.
166+
167+
```sh
168+
git clone https://github.com/apple/swift-nio.git
169+
cd swift-nio
170+
swift build
171+
```
172+
173+
> Tip: If you are building this project for production, use `swift build -c release`, see [building for production](README.md#building-for-production) for more information.
174+
175+
### Run
176+
177+
Once the project has finished compiling, run it on your server's IP at port `8080`.
178+
179+
```sh
180+
.build/debug/NIOHTTP1Server <server_ip> 8080
181+
```
182+
183+
If you used `swift build -c release`, then you need to run:
184+
185+
```sh
186+
.build/release/NIOHTTP1Server <server_ip> 8080
187+
```
188+
189+
Visit your server's IP via browser or local terminal and you should see a response.
190+
191+
```
192+
$ curl http://<server_ip>:8080
193+
Hello world!
194+
```
195+
196+
Use `CTRL+C` to quit the server.
197+
198+
Congratulations on getting your Swift server app running on Ubuntu!

0 commit comments

Comments
 (0)