Skip to content

Instantly share code, notes, and snippets.

@rennex
Last active July 18, 2023 11:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rennex/f379b8a53c8f2791d5838b4622ed6dd8 to your computer and use it in GitHub Desktop.
Save rennex/f379b8a53c8f2791d5838b4622ed6dd8 to your computer and use it in GitHub Desktop.
Example of a systemd service file to start a Sinatra app.
# /etc/systemd/system/foo.service
[Unit]
Description=Foo service
Wants=nginx.service
Requires=postgresql.service
After=postgresql.service
[Service]
Type=simple
User=foo
Group=foo
ExecStart=/srv/foo/app/serve
Restart=always
TimeoutSec=10
[Install]
WantedBy=multi-user.target
# /etc/nginx/sites-enabled/foo
# example nginx config, which proxies /foo to this app
server {
# ...basic stuff omitted...
location / {
try_files $uri $uri/ =404;
}
location /foo {
# this nginxroot directory has "foo" symlinked to /srv/foo/app/public
# to avoid having to rewrite that prefix out here :)
root /srv/foo/nginxroot;
try_files $uri @foosinatra;
}
location @foosinatra {
include /etc/nginx/sinatra_params;
proxy_pass http://localhost:1234;
}
}
#!/bin/sh
cd $(dirname $(readlink -f "$0"))
rackup --env production -o 127.0.0.1 -p 1234 >>log/production.log 2>&1
# /etc/nginx/sinatra_params
# extra headers that seem to be needed for Sinatra apps, especially when nginx uses https
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host $http_host;
proxy_set_header Connection "";
proxy_http_version 1.1;
@rennex
Copy link
Author

rennex commented Jun 16, 2016

And I guess I'll have to put extra info here. foo.service goes to /etc/systemd/system/foo.service (at least in Ubuntu 16.04). Run systemctl daemon-reload, systemctl start foo. systemctl enable foo to make it autostart on boot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment