Deploying a Swift Application on VPS: Step-by-Step from A to Z

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 from A to Z

\n\n

Swift has long ceased to be a language only for iOS development. Thanks to the Vapor framework and official Linux support, you can create high-performance server-side applications in Swift and deploy them on a VPS. In this guide, we will walk through the entire process from server setup to launching the production version of your Swift application.

\n\n

1. Preparing the VPS and Installing Swift

\n\n

First, you will need a VPS with Ubuntu 22.04 LTS (or 20.04). Connect via SSH and update the system:

\n\n
ssh root@your-server-ip\napt update && apt upgrade -y
\n\n

Install the necessary dependencies for Swift:

\n\n
apt install -y curl git build-essential libssl-dev libcurl4-openssl-dev\napt install -y libxml2-dev libicu-dev libsqlite3-dev libz-dev
\n\n

Now download and install Swift for Ubuntu. Check the latest version at swift.org. At the time of writing, this is Swift 5.9:

\n\n
cd /opt\nwget https://download.swift.org/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gz\ntar xzf swift-5.9-RELEASE-ubuntu22.04.tar.gz\necho 'export PATH=/opt/swift-5.9-RELEASE-ubuntu22.04/usr/bin:$PATH' >> ~/.bashrc\nsource ~/.bashrc
\n\n

Verify the installation:

\n\n
swift --version
\n\n

You should see Swift version 5.9.

\n\n

2. Creating and Building a Swift/Vapor Application

\n\n

Install Vapor Toolbox (optional but convenient):

\n\n
git clone https://github.com/vapor/toolbox.git\ncd toolbox\nswift build -c release\ncp .build/release/vapor /usr/local/bin/
\n\n

Let's create a test Vapor application. If you already have a project, simply copy it to the server via SCP or Git.

\n\n
cd ~\nvapor new HelloServer\ncd HelloServer
\n\n

Edit Sources/App/routes.swift to add a simple endpoint:

\n\n
import Vapor\n\nfunc routes(_ app: Application) throws {\n    app.get { req async in\n        "Hello from Swift on VPS!"\n    }\n    \n    app.get("status") { req async -> HTTPStatus in\n        return .ok\n    }\n}
\n\n

Build the project in production mode:

\n\n
swift build -c release
\n\n

The binary will be located at .build/release/HelloServer.

\n\n

3. Configuring Launch and systemd Service

\n\n

To ensure the application starts automatically after a server reboot, create a systemd service. First, create a user for the application (security):

\n\n
useradd -m -s /bin/bash swiftuser\nchown -R swiftuser:swiftuser /root/HelloServer
\n\n

Create the service file:

\n\n
nano /etc/systemd/system/helloserver.service
\n\n

Insert the following content:

\n\n
[Unit]\nDescription=HelloServer Swift App\nAfter=network.target\n\n[Service]\nType=simple\nUser=swiftuser\nWorkingDirectory=/root/HelloServer\nExecStart=/root/HelloServer/.build/release/HelloServer --host 127.0.0.1 --port 8080\nRestart=on-failure\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target
\n\n

Start the service:

\n\n
systemctl daemon-reload\nsystemctl enable helloserver\nsystemctl start helloserver\nsystemctl status helloserver
\n\n

Check that the application responds locally:

\n\n
curl http://127.0.0.1:8080
\n\n

4. Configuring Nginx as a Reverse Proxy

\n\n

For a production environment, it is not recommended to expose port 8080 directly. Let's install Nginx and configure proxying with SSL (Let's Encrypt).

\n\n
apt install -y nginx certbot python3-certbot-nginx
\n\n

Create the Nginx configuration:

\n\n
nano /etc/nginx/sites-available/helloserver
\n\n

Insert:

\n\n
server {\n    listen 80;\                

Blogs

Book Recommendations