Deploying Server Side Swift to Linode

Jonathan Guthrie
Server Side Swift and More
5 min readMar 17, 2017

Once you’ve finished developing your app, the next stage is to make it available it to your audience… so the natural desire is to deploy it to a server.

In this article I’m going to demonstrate deploying a Server Side Swift app to a Linode container.

There are three prerequisites if you want to follow along: the Perfect Assistant application, a working Docker application on your Mac, and a Linode account.

Server Setup

The first step is going to be setting up a new Linode container.

In your Linode Manager console, Click the “Add a Linode” link. Select whatever size you need and the datacenter location.

You’re returned to the list of your Linodes, with the new one listed. Click into it, then press “Deploy an Image”. Here we could just create a new Ubuntu 16 Linode, but we’re going to use a “StackScript” to do all of the setup heavy lifting for us, so click the “Deploying using StackScripts” link.

In the “Community StackScripts: keywords” search, enter “Swift”. This will bring up a few, but the one you want is the “jono / Server-Side-Swift” script.

On the next screen, make sure Ubuntu 16.04 is selected, and enter a disk size, swap disk size, and a root password, then press “Deploy”.

Wait until the “Host Job Queue” jobs have all completed, then press “Boot”.

You can watch the progress of the boot and StackScript by selecting “Remote Access”, and launching the “Lish via Browser” option. The StackScript performs a complete update of all packages to the latest available, installs dependencies that Swift needs for many of the common libraries that Perfect will be asked to compile against. It then installs Swift 3.0.2 and makes a few linkages for you.

When you’re finally presented with the login prompt, your Linode is ready.

Make a note of your new Linode’s IP address, we will need this soon.

Compiling the code

Switching now to the Perfect Assistant, lets grab the “Perfect App Template” from GitHub. On the Welcome screen, click on “Create New Project”, the “Custom Repository URL”. Find a home for your project by clicking “Browse” next to the location, and then paste in the URL for the template: “https://github.com/PerfectlySoft/PerfectAppTemplate.git"

Leave the “Integrate Linux builds with Xcode project” checked, as we are targeting Linux deployment in this phase.

Once you click “Save” the system will start it’s initial clone of the project for the macOS side.

Doing a Linux build is as simple as clicking the “BUILD: Linux” button. What this is doing behind the scenes is getting a Docker container ready, cloning the project’s dependencies into a sandboxed location, and doing a Linux build that is sandboxed as well.

Once it’s finished, you should see the last 3 lines as something like:

Compile Swift Module ‘Perfect_App_Template’ (10 sources)
Linking ./.build_lin/release/Perfect-App-Template
Success.

This shows you the location of the executable we just built. Using the Assistant to open our project directory, open the .build_linux/release directory. This is made much easier if you have enabled the view of hidden files in the Finder.

There are a lot of files in here, most of which we can ignore.

Deploying

Open your favourite SFTP app... I personally like and use Transmit.

Open a new SFTP configuration, and enter the IP address of your new Linode, as well as the username (root), and the password you used when setting the Linode up, then connect.

Once Transmit has connected, go to the root of the disk, and create a new directory called “perfect-deployed”. Inside that make a directory for our app, “Perfect-App-Deployed”.

Copy into this new directory the following files:

libCHTTPParser.so
libCOpenSSL.so
libCZlib.so
libLinuxBridge.so
Perfect-App-Template

Go up a couple of levels in the Finder, and copy into the app dir in SFTP the config and webfoot directories.

Create a new file called “perfect-app-template.service”, and copy the code from https://github.com/PerfectlySoft/Perfect-Ubuntu/perfect-app-template.service

[Unit]
Description=Perfect deployed perfect-app-template
[Service]
Type=simple
WorkingDirectory=/perfect-deployed/Perfect-App-Template
ExecStart=/perfect-deployed/Perfect-App-Template/Perfect-App-Template
Restart=always
PIDFile=/var/run/perfect-app-template.pid
[Install]
WantedBy=multi-user.target

Now you have all the pieces in place, lets start it up!

Back in the browser, if you’ve closed your Lish browser console, open it back up, and log in.

Then you want to execute the following two commands to enable the service, and reload the services controller:

systemctl enable /perfect-deployed/Perfect-App-Template/perfect-app-template.service 
systemctl daemon-reload

Now, start the app!

service perfect-app-template start

Now, back in the browser, hit http://(youripaddress):8104/ and you should see “Hello, World!”

Next Steps

For examples visit GitHub.com/perfectexamples — there is an ever growing library of examples and demos there.

And of course if you’re looking for live help from our awesome community, join our Slack channel via www.perfect.ly

Thanks for being with us today, don’t forget to say Hi on Slack!

--

--

Jonathan Guthrie
Server Side Swift and More

Developer Evangelist, Musician, and Active Activist... Newmarket ON, Canada