1# Running Soft Serve as a Systemd Service
 2
 3Most Linux OSes use Systemd as an init system and service management. You can
 4use Systemd to manage Soft Serve as a service on your host machine.
 5
 6Our Soft Serve deb/rpm packages come with Systemd service files pre-packaged.
 7You can install `soft-serve` from our Apt/Yum repositories. Follow the
 8[installation instructions](https://github.com/charmbracelet/soft-serve#installation) for
 9more information.
10
11## Writing a Systemd Service File
12
13> **Note** you can skip this section if you are using our deb/rpm packages or
14> installed Soft Serve from our Apt/Yum repositories.
15
16Start by writing a Systemd service file to define how your Soft Serve server
17should start.
18
19First, we need to specify where the data should live for our server. Here I
20will be choosing `/var/local/lib/soft-serve` to store the server's data. Soft
21Serve will look for this path in the `SOFT_SERVE_DATA_PATH` environment
22variable.
23
24Make sure this directory exists before proceeding.
25
26```sh
27sudo mkdir -p /var/local/lib/soft-serve
28```
29
30We will also create a `/etc/soft-serve.conf` file for any extra server settings that we want to override.
31
32```conf
33# Config defined here will override the config in /var/local/lib/soft-serve/config.yaml
34# Keys defined in `SOFT_SERVE_INITIAL_ADMIN_KEYS` will be merged with
35# the `initial_admin_keys` from /var/local/lib/soft-serve/config.yaml.
36#
37#SOFT_SERVE_GIT_LISTEN_ADDR=:9418
38#SOFT_SERVE_HTTP_LISTEN_ADDR=:23232
39#SOFT_SERVE_SSH_LISTEN_ADDR=:23231
40#SOFT_SERVE_SSH_KEY_PATH=ssh/soft_serve_host_ed25519
41#SOFT_SERVE_INITIAL_ADMIN_KEYS='ssh-ed25519 AAAAC3NzaC1lZDI1...'
42```
43
44> **Note** Soft Serve stores its server configuration and settings in
45> `config.yaml` under its _data path_ directory specified using
46> `SOFT_SERVE_DATA_PATH` environment variable.
47
48Now, let's write a new `/etc/systemd/system/soft-serve.service` Systemd service file:
49
50```conf
51[Unit]
52Description=Soft Serve git server 🍦
53Documentation=https://github.com/charmbracelet/soft-serve
54Requires=network-online.target
55After=network-online.target
56
57[Service]
58Type=simple
59Restart=always
60RestartSec=1
61ExecStart=/usr/bin/soft serve
62Environment=SOFT_SERVE_DATA_PATH=/var/local/lib/soft-serve
63EnvironmentFile=-/etc/soft-serve.conf
64WorkingDirectory=/var/local/lib/soft-serve
65
66[Install]
67WantedBy=multi-user.target
68```
69
70Great, we now have a Systemd service file for Soft Serve. The settings defined
71here may vary depending on your specific setup. This assumes that you want to
72run Soft Serve as `root`. For more information on Systemd service files, refer
73to
74[systemd.service](https://www.freedesktop.org/software/systemd/man/systemd.service.html)
75
76## Start Soft Serve on boot
77
78Now that we have our Soft Serve Systemd service file in-place, let's go ahead
79and enable and start Soft Serve to run on-boot.
80
81```sh
82# Reload systemd daemon
83sudo systemctl daemon-reload
84# Enable Soft Serve to start on-boot
85sudo systemctl enable soft-serve.service
86# Start Soft Serve now!!
87sudo systemctl start soft-serve.service
88```
89
90You can monitor the server logs using `journalctl -u soft-serve.service`. Use
91`-f` to _tail_ and follow the logs as they get written.
92
93***
94
95Part of [Charm](https://charm.sh).
96
97<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>
98
99Charm热爱开源 • Charm loves open source