|
| 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 | + |
| 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