Deploying a Swift Application on VPS: Step-by-Step from A to Z
\n\nSwift 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\n1. Preparing the VPS and Installing Swift
\n\nFirst, you will need a VPS with Ubuntu 22.04 LTS (or 20.04). Connect via SSH and update the system:
\n\nssh root@your-server-ip\napt update && apt upgrade -y\n\nInstall the necessary dependencies for Swift:
\n\napt install -y curl git build-essential libssl-dev libcurl4-openssl-dev\napt install -y libxml2-dev libicu-dev libsqlite3-dev libz-dev\n\nNow download and install Swift for Ubuntu. Check the latest version at swift.org. At the time of writing, this is Swift 5.9:
\n\ncd /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\nVerify the installation:
\n\nswift --version\n\nYou should see Swift version 5.9.
\n\n2. Creating and Building a Swift/Vapor Application
\n\nInstall Vapor Toolbox (optional but convenient):
\n\ngit clone https://github.com/vapor/toolbox.git\ncd toolbox\nswift build -c release\ncp .build/release/vapor /usr/local/bin/\n\nLet's create a test Vapor application. If you already have a project, simply copy it to the server via SCP or Git.
\n\ncd ~\nvapor new HelloServer\ncd HelloServer\n\nEdit Sources/App/routes.swift to add a simple endpoint:
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\nBuild the project in production mode:
\n\nswift build -c release\n\nThe binary will be located at .build/release/HelloServer.
3. Configuring Launch and systemd Service
\n\nTo ensure the application starts automatically after a server reboot, create a systemd service. First, create a user for the application (security):
\n\nuseradd -m -s /bin/bash swiftuser\nchown -R swiftuser:swiftuser /root/HelloServer\n\nCreate the service file:
\n\nnano /etc/systemd/system/helloserver.service\n\nInsert 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\nStart the service:
\n\nsystemctl daemon-reload\nsystemctl enable helloserver\nsystemctl start helloserver\nsystemctl status helloserver\n\nCheck that the application responds locally:
\n\ncurl http://127.0.0.1:8080\n\n4. Configuring Nginx as a Reverse Proxy
\n\nFor 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\napt install -y nginx certbot python3-certbot-nginx\n\nCreate the Nginx configuration:
\n\nnano /etc/nginx/sites-available/helloserver\n\nInsert:
\n\nserver {\n listen 80;\