Deploying a Swift Application on VPS: Step-by-Step Guide
Server-side application development in Swift is rapidly gaining popularity thanks to the Vapor framework. However, many iOS/macOS developers face difficulties when moving a finished application to a remote server. In this guide, we will thoroughly examine how to deploy a Swift application on a VPS, configure the environment, and ensure stable operation in a production environment.
1. Preparing the VPS Server for Swift
The first step is to rent a VPS (Ubuntu 22.04 LTS is recommended) and connect to it via SSH. After logging in, update the system:
sudo apt update && sudo apt upgrade -ysudo apt install curl git clang libicu-dev libcurl4-openssl-dev libssl-dev -yTo install Swift on Linux, use the official script from Apple:
wget https://swift.org/builds/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gztar xzf swift-5.9-RELEASE-ubuntu22.04.tar.gzsudo mv swift-5.9-RELEASE-ubuntu22.04 /usr/share/swiftecho 'export PATH=/usr/share/swift/usr/bin:$PATH' >> ~/.bashrcsource ~/.bashrcswift --versionVerify that Swift is installed correctly — you should see the compiler version.
2. Building the Swift Application (Vapor) on the Server
Copy the source code of your project to the server. Using Git is the most convenient method:
git clone https://github.com/your-project/your-repository.gitcd your-repositoryBuild the project in release mode. This will create an optimized binary:
swift build -c releasecp .build/release/Run /usr/local/bin/myappIf you are using Vapor, make sure the correct port (usually 8080) is configured in the configure.swift file. Example configuration:
import Vapor
public func configure(_ app: Application) throws { app.http.server.configuration.hostname = "0.0.0.0" app.http.server.configuration.port = 8080 // Register routes try routes(app)}Check the application's functionality by running it manually:
myapp --env production serveIf the application starts and responds to requests, you can proceed to setting up the daemon.
3. Configuring systemd for Auto-Start
To ensure the application starts automatically when the server reboots, create a systemd service:
sudo nano /etc/systemd/system/swift-app.serviceInsert the following code:
[Unit]Description=Swift Vapor ApplicationAfter=network.target
[Service]Type=simpleUser=www-dataWorkingDirectory=/var/www/myappExecStart=/usr/local/bin/myapp --env production serveRestart=on-failureRestartSec=10
[Install]WantedBy=multi-user.targetSave the file and activate the service:
sudo systemctl daemon-reloadsudo systemctl enable swift-appsudo systemctl start swift-appsudo systemctl status swift-appIf the status shows active (running) — everything is working.
4. Configuring Nginx as a Reverse Proxy
For a production environment, it is recommended to use Nginx to proxy requests to the Swift application. This will provide load balancing and SSL support:
sudo apt install nginx -ysudo nano /etc/nginx/sites-available/swift-appExample configuration:
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; }
location /static/ { root /var/www/myapp/Public; expires 3