Systemd is the default system and service manager on most Linux distributions. It is usually installed by default so no installation is needed on supported Swift distributions.
Each Vapor app on your server should have its own service file. For an example
Hello project, the configuration file would be located at
/etc/systemd/system/hello.service. This file should look like the following:
[Unit] Description=Hello Requires=network.target After=network.target [Service] Type=simple User=vapor Group=vapor Restart=always RestartSec=3 WorkingDirectory=/home/vapor/hello ExecStart=/home/vapor/hello/.build/release/Run serve --env production StandardOutput=syslog StandardError=syslog SyslogIdentifier=vapor-hello [Install] WantedBy=multi-user.target
As specified in our configuration file the
Hello project is located in the home folder for the user
vapor. Make sure
WorkingDirectory points to the root directory of your project where the
Package.swift file is.
--env production flag will disable verbose logging.
Otherwise, quoting the values is optional but recommended.
You can export variables in two ways via systemd. Either by creating an environment file with all the variables set in it:
Or you can add them directly to the service file under
Exported variables can be used in Vapor using
let port = Environment.get("PORT")
You can now load, enable, start, stop and restart your app by running the following as root.
systemctl daemon-reload systemctl enable hello systemctl start hello systemctl stop hello systemctl restart hello