Introduction: Why Deploy Swift on a VPS?
Swift is not only a language for iOS and macOS applications. Thanks to the Vapor framework and native Linux support, you can create high-performance server-side solutions in Swift. Deploying such an application on a VPS (Virtual Private Server) opens up opportunities for creating your own APIs, websites, and microservices.
In this article, we will walk through the complete deployment cycle of a Swift application on a remote server running Ubuntu 22.04. You will learn how to prepare the environment, compile the project, set up auto-start, and configure a proxy server. This guide is suitable for both beginners and experienced developers looking to systematize their knowledge.
1. Server Preparation and Swift Installation
The first step is to rent a VPS (we recommend 2 cores, 2 GB RAM). After connecting via SSH, update the system and install dependencies.
1.1. Basic Setup
ssh root@your-server-ipapt update && apt upgrade -yapt install curl git build-essential pkg-config libssl-dev zlib1g-dev -y1.2. Installing the Swift Toolchain
For Ubuntu 22.04, use the official Swift distribution. Check the latest version at swift.org.
# Download Swift 5.9 (example)wget https://download.swift.org/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gz
# Extracttar -xzf swift-5.9-RELEASE-ubuntu22.04.tar.gzmv swift-5.9-RELEASE-ubuntu22.04 /usr/share/swift
# Add to PATHecho 'export PATH=/usr/share/swift/usr/bin:$PATH' >> ~/.bashrcsource ~/.bashrc
# Verifyswift --version2. Compilation and Application Setup
Let's consider deploying a typical Vapor application. Assume your project is already in a Git repository.
2.1. Cloning and Building
cd /var/www/git clone https://github.com/your-username/your-repository.git myappcd myapp
# Build in Release modeswift build -c releaseAfter a successful compilation, the executable file will be located at .build/release/Run. For Vapor, port 8080 is used by default.
2.2. Running and Testing
# Temporary run (Ctrl+C to stop).build/release/Run serve --hostname 0.0.0.0 --port 8080Check that it works: open http://your-server-ip:8080 in your browser. If you see a response, everything is working.
3. Setting Up systemd for Auto-Start
To make the application start automatically after a server reboot, we will create a systemd service.
3.1. Creating the Service
nano /etc/systemd/system/myapp.serviceInsert the following code (replace the paths with your own):
[Unit]Description=Swift Vapor ApplicationAfter=network.target
[Service]Type=simpleUser=www-dataGroup=www-dataWorkingDirectory=/var/www/myappExecStart=/var/www/myapp/.build/release/Run serve --hostname 127.0.0.1 --port 8080Restart=on-failureRestartSec=5
[Install]WantedBy=multi-user.target3.2. Activation and Start
systemctl daemon-reloadsystemctl enable myappsystemctl start myappsystemctl status myappIf the status is active (running), the service is working. Now the application will start automatically.
4. Setting Up Nginx and SSL (HTTPS)
The Nginx proxy server is necessary for handling static files, load balancing, and adding an SSL certificate.
4.1. Installing Nginx
apt install nginx -y4.2. Proxy Configuration
Create a configuration file for your domain or IP:
nano /etc/nginx/sites-available/myappserver { listen 80; server_name your-domain.ru;
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $ho