1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Install and configure systemd on Managed Cloud Server
This guide explains how to install and configure systemd on a Managed Cloud Server and presents the main usable commands.
Prerequisites
- Follow the systemd installation guide for the Cloud Server.
- Refer to the official documentation to explore all the possibilities offered by systemd.
- The "unit" files should be placed in:
~/.config/systemd/user/ ( /home/clients/absolute-path-id/.config/systemd/user )
(replace absolute-path-id as seen in your Manager), and permissions should be set to 0644. - The
--user
parameter should be included in each command.
Main Commands
Here is a non-exhaustive list of commands that can be used with systemd.
Force systemd to reload the unit files and consider the changes:
systemctl --user daemon-reload
Enabling a service:
systemctl --user enable --now SERVICENAME.service
Checking the status of a service:
systemctl --user status SERVICENAME.service
Configuring Node as a service with systemd
You will need to create a "Unit" file with the ".service" extension, which should be saved in the directory:
~/.config/systemd/user/
You can reuse the example below by replacing values starting with "{}"
[Unit]
Description={Service Name} # Specify a service name here. This is mandatory but does not impact the functionality
[Service]
Restart=always
Environment=NODE_VERSION={desired version} # Specify the desired Node version here. Ensure it is installed beforehand with "nvm install {desired version}"
WorkingDirectory=%h/{Node project directory} # %h corresponds to the root of hosting
ExecStart=/bin/bash -c "exec $HOME/.nvm/nvm-exec {node script launch command}" # This command depends on the project. For example, "npm run start," "npm run serve," or "node server.js" are common
[Install]
WantedBy=default.target
Additional Actions with a Unit File
systemctl --user daemon-reload
Start the service (if it's already active, nothing happens):
systemctl --user start [Unit Name]
Stop the service (if it's not active, nothing happens):
systemctl --user stop [Unit Name]
Restart the service (if it's not running, it starts):
systemctl --user restart [Unit Name]
Get information about the service; including:
- "Active," which indicates if the service is running and since when
- "CGroup" shows the process group managed by the service, allowing you to see active processes with their arguments and IDs
Beneath "CGroup" are any logs (standard output and error of the process):
systemctl --user status [Unit Name]
Enable automatic startup of the service at server boot; NB: it doesn't start the service:
systemctl --user enable [Unit Name]
Disable automatic startup of the service at server boot; NB: it doesn't stop the service:
systemctl --user disable [Unit Name]
Configuration with user inputs:
[Unit]
Description="Service Name"
[Service]
Restart=always
Environment=NODE_VERSION=16.17
WorkingDirectory=%h/sites/"site-directory-name"/
ExecStart=/bin/bash -c "exec $HOME/.nvm/nvm-exec npm run start"
[Install]
WantedBy=default.target