Deploying a Swift Application on VPS: Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Deploying a Swift Application on VPS: Step-by-Step Guide

Developing server-side applications in Swift using the Vapor framework is gaining popularity. However, after writing the code, the question arises: how to host a Swift application on a remote server? In this article, we will walk through the complete process of deploying a Swift application on a VPS (Ubuntu 22.04) — from installing the necessary components to setting up a reverse proxy and running it in production mode.



1. Server Preparation and Swift Installation

First, connect to the VPS via SSH and update the package database:

sudo apt update && sudo apt upgrade -y

Swift requires dependencies for building. Install them:

sudo apt install clang libicu-dev libcurl4-openssl-dev libssl-dev python3 pkg-config -y

Then download and install Swift for Ubuntu. At the time of writing, the current version is 5.9. Download the archive:

wget https://download.swift.org/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gz

Extract it and move it to /usr/share:

tar xzf swift-5.9-RELEASE-ubuntu22.04.tar.gzsudo mv swift-5.9-RELEASE-ubuntu22.04 /usr/share/swift

Add Swift to PATH. Open ~/.bashrc:

nano ~/.bashrc

Add the line to the end of the file:

export PATH="/usr/share/swift/usr/bin:$PATH"

Apply the changes:

source ~/.bashrc

Verify the installation:

swift --version

Swift version 5.9 should be displayed.



2. Building the Swift Application (Vapor) on the Server

Clone the application repository or copy the files via SCP. For example, create a test Vapor project:

mkdir ~/myapp && cd ~/myappswift package init --type executable

Add Vapor to Package.swift (example of a minimal server):

// swift-tools-version:5.9import PackageDescription

let package = Package( name: "myapp", platforms: [.macOS(.v13)], dependencies: [ .package(url: "https://github.com/vapor/vapor.git", from: "4.89.0") ], targets: [ .executableTarget(name: "myapp", dependencies: [ .product(name: "Vapor", package: "vapor") ]) ])

Create the file Sources/myapp/main.swift:

import Vapor

let app = try Application(.detect())app.get { req in return "Hello, Swift on VPS!"}try app.run()

Build the project in release mode:

swift build -c release

After building, the binary will be located in .build/release/myapp. Copy it to a convenient location:

cp .build/release/myapp /usr/local/bin/myapp


3. Configuring Nginx as a Reverse Proxy

The Swift application will listen on port 8080, and Nginx will redirect traffic from port 80 (or 443 for HTTPS). Install Nginx:

sudo apt install nginx -y

Create a configuration file for our application:

sudo nano /etc/nginx/sites-available/myapp

File contents:

server {    listen 80;    server_name your-domain.com;

location / { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }}

Activate the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl restart nginx

Don't forget to configure the DNS A record for your domain, pointing to the VPS IP address.

Blogs

Book Recommendations