The instructions for controlling Kong Gateway through systemd require root to be used to start kong as a root user. We would like to use a non-root user, e.g. a “kong” user.
How to set up kong to autostart kong with systemd with a non root user
How do I set up Kong Gateway to autostart with systemd as a non-root user?
Copy Kong’s kong-enterprise-edition.service unit file to /home/kong/.config/systemd/user/, remove the hardcoded User= and Group= lines, and change WantedBy=multi-user.target to WantedBy=default.target. Enable lingering for the kong user with loginctl enable-linger $USER and set XDG_RUNTIME_DIR so systemctl --user commands can start and enable the service at boot.
Steps
In RHEL and CentOS 8 you can configure systemd to control the Kong Gateway as a “kong” user by following these steps:
- Log into the VM where you want to configure the Gateway to start as the “kong” user.
- Create a folder called
/home/kong/.config/systemd/user/. - Copy
/lib/systemd/system/kong-enterprise-edition.service(this is the file’s actual shipped location in the current Kong Gateway Enterprise package/image - it is not found under/etc/kong/) to/home/kong/.config/systemd/user/. - Edit
/home/kong/.config/systemd/user/kong-enterprise-edition.serviceand remove theUser=andGroup=lines - Kong’s shipped unit file hardcodesUser=root, and a copied user-level unit will fail to start unless those lines are stripped out. -
In the same file, replace:
[Install] WantedBy=multi-user.targetwith
[Install] WantedBy=default.target -
Run the following to make sure the Kong Gateway process can continue to run when the kong user has logged off:
loginctl enable-linger $USER -
Add the following to
.bash_profile, and source~/.bash_profileafterwards:export XDG_RUNTIME_DIR=/run/user/$(id -u) -
You can now start kong using systemctl, and enable autostart at system boot with these two commands:
systemctl --user start kong-enterprise-edition systemctl --user enable kong-enterprise-edition