Using Passenger with Swift
To use Passenger with Swift, you'll need to use one of the many web frameworks written in Swift.
In this example we'll be using Vapor, because it is an uncomplicated library and does not require a lot of boilerplate code.
Step 1) Install Swift
Swift is available as source for linux and from macOS' cli tools (and Xcode). Unfortunately using swift on linux can be a bit difficult to setup.
Ubuntu:
apt install -y wget gnupg lsb-release libtinfo5 libcurl4 libncurses5
wget -q https://repo.vapor.codes/apt/keyring.gpg -O- | apt-key add -
echo "deb https://repo.vapor.codes/apt $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/vapor.list
apt-get update
apt install -y vapor
Non Ubuntu Linux Distros
Currently using Swift on linux-distros other than Ubuntu is not well supported, when the situation improves we will update these docs.
macOS:
In order to use Homebrew to install Vapor on macOS, you'll need to install the command line tools, as well as Homebrew itself.
If you are physically present at the mac, you can run the following simplified commands to install everything you need:
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install vapor/tap/vapor
The following commands will install everything for you, entirely from the cli, so you can run them over ssh if need be.
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
softwareupdate -i $(softwareupdate -l | grep "\*.*Command Line" | grep $(sw_vers -productVersion) | awk -F"*" '{print $2}' | sed -e 's/^ *//' | tr -d '\n') --verbose
rm /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install vapor/tap/vapor
Step 3) Create your project
Create the file and directories you'll use for this project and cd
there.
vapor new Hello
cd Hello
Step 4) Hello world
Edit the Sources/App/routes.swift
file to contain only the following lines:
import Vapor
public func routes(_ router: Router) throws {
router.get { req in
return "Hello, world!"
}
}
And add the following lines to the a
file, in the configure(config:env:services:)
function:
/// Register server config with default port
let resolvedPort = Int(Environment.get("PORT") ?? (env.isRelease ? "80" : "8080"))
let serverConfig = try NIOServerConfig.default(port: resolvedPort!)
services.register(serverConfig)
Step 5) Build and run
Run the following commands to run the web app, which will serve the web app on port 5000, then verify that your app is being served by sending a request to the web app from another terminal.
vapor fetch
vapor build
$(swift build --show-bin-path)/Run serve --hostname 0.0.0.0 --port 5000
# in another terminal
curl http://localhost:5000/
Step 6) Add Passenger
You will need Passenger installed, for instructions on how to install Passenger click here.
Next we will want a release build of our web app, which you can make with vapor build --release
, then you can move the executable (found with ls $(swift build --configuration release --show-bin-path)/Run
) into a more convenient location on your disk, such as ~/Sites/
. Once you are happy with where things are, you can start your web app in Passenger by cd
ing to the dir with the executable and running the following command: passenger start --app-start-command 'env PORT=$PORT ./Run serve --env prod --hostname 0.0.0.0'
.
You can verify that everything is working with curl http://localhost:3000
. To stop Passenger simply press Ctrl+C
in the terminal where you started it.
The commands to run from this section are collected here:
vapor build --release
passenger start --app-start-command "env PORT=\$PORT $(swift build --configuration release --show-bin-path)/Run serve --env prod --hostname 0.0.0.0"`.
# in another terminal
curl http://localhost:3000