Deploying a Swift Application on VPS: Step-by-Step Guide for Advanced Users
You have created an excellent server-side application in Swift using the Vapor framework. Now it needs to be launched in production. Deploying a Swift application on a VPS requires understanding the specifics of how Swift works on Linux, configuring the server environment, and using the right tools to ensure stability and performance. In this advanced guide, we will cover the complete cycle: from compilation for Linux to setting up CI/CD, working with Docker and Supervisor.
1. Server Preparation and Compiling Swift for Linux
The first thing to understand: Swift on macOS and Linux are different ecosystems. For deployment on a VPS (Ubuntu 22.04 LTS), cross-compilation or building directly on the server is required. It is recommended to build on the server to avoid library issues.
1.1 Installing Swift on Ubuntu
Connect to the server via SSH and install the necessary dependencies:
sudo apt updatesudo apt install -y curl git clang libicu-dev libcurl4-openssl-dev libssl-dev \ libsqlite3-dev libxml2-dev pkg-config uuid-dev
# Download the latest Swift Toolchainwget https://download.swift.org/swift-5.9.2-release/ubuntu2204/swift-5.9.2-RELEASE/swift-5.9.2-RELEASE-ubuntu22.04.tar.gztar -xzf swift-5.9.2-RELEASE-ubuntu22.04.tar.gzsudo mv swift-5.9.2-RELEASE-ubuntu22.04 /usr/share/swift
# Add to PATHecho 'export PATH=/usr/share/swift/usr/bin:$PATH' >> ~/.bashrcsource ~/.bashrcswift --version1.2 Cloning and Building the Project
Clone the repository and build the release version of the application:
git clone https://github.com/your-username/your-swift-app.gitcd your-swift-appswift build --configuration releaseAfter a successful build, the binary will be located at .build/release/YourApp. Test running it manually:
./.build/release/YourApp serve --hostname 0.0.0.0 --port 80802. Configuring the Production Environment: Nginx and Supervisor
Running a Swift application directly on port 80 is bad practice. Use a combination of Nginx (reverse proxy) + Supervisor (process manager).
2.1 Nginx Configuration
Install Nginx and create a configuration to proxy requests to your Swift application:
sudo apt install nginxsudo nano /etc/nginx/sites-available/yourappFile contents:
server { listen 80; server_name yourdomain.com www.yourdomain.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; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
# Configuration for large files (if needed) client_max_body_size 50m;}Activate the configuration and check:
sudo ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl restart nginx2.2 Configuring Supervisor for Automatic Restart
Supervisor will monitor the process and restart it if it crashes. Install and create the config:
sudo apt install supervisorsudo nano /etc/supervisor/conf.d/yourapp.confConfiguration contents:
[program:yourapp]command=/home/ubuntu/your-swift-app/.build/release/YourApp serve --hostname 0.0.0.0 --port 8080directory=/home/ubuntu/your-swift-appuser=ubuntuautostart=trueautorestart=truestartretries=3stderr_logfile=/var/log/yourapp.err.logstdout_logfile=/var/log/yourapp.out.logenvironment=ENVIRONMENT="producti