-
Talinn, Estonia
-
-
support@lnsolutions.ee
How to Install and Automatically Run a Node.js App with Systemd
How to Install and Automatically Run a Node.js App with Systemd
Read more on Medium

Introduction
Systemd is a powerful tool for managing services on Linux systems. By configuring systemd, you can automate the startup, shutdown, and management of your Node.js applications. In this article, we’ll guide you through the process of installing a Node.js app and setting up a systemd configuration to automatically run it.
Prerequisites
- A Linux-based operating system (e.g., Ubuntu, Debian, CentOS)
- Node.js installed on your system
- Basic knowledge of the command line
Steps:
1. Install Node.js: Ensure that Node.js is installed on your system. You can install it using the package manager of your Linux distribution or by downloading and installing it from the official Node.js website. Alternatively you can use nvm to install a specific version of Node.js not included in your Linux distro. For more information read the official github project here: https://github.com/nvm-sh/nvm
2. Install Your Node.js App: Navigate to the directory where you want to install your Node.js app and clone your app’s repository or copy your app’s files into that directory.
git clone https://github.com/your-username/your-node-app.git
cd your-node-app
3. Install Dependencies: Use npm or yarn to install the dependencies of your Node.js app.
npm install
# or
yarn install
4. Test Your App: Before setting up systemd, ensure that your Node.js app runs correctly by manually starting it.
npm start
# or
yarn start
Test your app by accessing it in a web browser or using any other appropriate method.
5. Create a Systemd Service File: Now, let’s create a systemd service file to manage our Node.js app.
sudo nano /etc/systemd/system/your-app-name.service
Replace your-app-name
with a descriptive name for your service. Add the following content to the file:
[Unit]
Description=Your Node.js App
After=network.target[Service]
ExecStart=/usr/bin/node /path/to/your/app/app.js
WorkingDirectory=/path/to/your/app
Restart=always
User=your-username
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Adjust the paths and filenames according to your app’s setup. The ExecStart
directive specifies the command to start your app. The User
directive specifies the user account under which the service should run.
6. Grant permission to another non-root user to start and manage the systemd service. When specifying the User
directive in the systemd service file, you are defining the user account under which the service runs. You can allow other non-root users to control the service by adding them to the relevant systemd user group.
Here are the steps to grant permission to another non-root user:
a. Add the non-root user to the systemd user group:
sudo usermod -aG systemd-journal your-username
b. Replace your-username
with the non-root username that you want to grant permission to.
c. Reload the systemd manager configuration:
sudo systemctl daemon-reload
d. Grant the necessary privileges to the user:
sudo systemctl enable your-app-name sudo systemctl start your-app-name
Now, the specified non-root user should have permission to start and manage the service. Note that this grants the user permission to interact with the service through systemd commands. If there are additional file or directory permissions needed for your Yarn app, you should ensure that the user has the appropriate access rights.
Also, keep in mind that adding users to the systemd-journal
group allows them to view the journal logs. If your logs are redirected to custom log files using the StandardOutput
and StandardError
directives in the systemd service file, you may need to adjust the file permissions accordingly.
7. Enable and Start the Service: After creating the systemd service file, reload the systemd daemon and start your service.
sudo systemctl daemon-reload
sudo systemctl start your-app-name
sudo systemctl enable your-app-name
8. Verify the Status: Check the status of your service to ensure that it’s running without any errors.
sudo systemctl status your-app-name
Conclusion
In this article, we’ve demonstrated how to install a Node.js app and automatically run it using systemd on a Linux system. By following these steps, you can ensure that your Node.js app starts automatically on system boot and is managed effectively by systemd. This approach helps in ensuring the reliability and availability of your application.