Running a Node.js app as a systemd service allows your app to start automatically when your system boots up. It’s a great way to ensure that your app is always running, even after a server reboot or if something goes wrong. In this post, I’ll walk you through the process of setting up your Node.js app as a systemd service.
I’ll explain everything in simple terms, step by step, so even if you’re new to system administration or Node.js, you can follow along. I’ll also share my personal experience of setting up Node.js apps this way.
What is systemd?
First things first, let’s understand what systemd is. It’s a system and service manager for Linux. Basically, it’s the part of your operating system that starts, stops, and manages services (like a Node.js app) and other system resources. If you’re familiar with Windows, you might compare systemd to the Task Manager, but it’s way more powerful and runs in the background.
When we turn your Node.js app into a systemd service, you’re telling systemd to handle starting, stopping, and managing your app. So, you won’t need to manually start it every time the server restarts. It’s a big time-saver and a must-have for production environments.
Why Run Your Node.js App as a systemd Service?
Before diving into the steps, let’s talk about why you might want to do this:
- Automatic Start on Boot: Your app will start automatically every time your server reboots.
- Easy Management: You can start, stop, and check the status of your app with simple commands.
- Monitoring: systemd makes it easy to monitor your app’s logs and performance.
You might be thinking, “But isn’t it just as easy to run node app.js
manually?” Well, sure, but what happens if your server restarts? Or what if the app crashes? With systemd, you don’t have to worry about these things.
Step 1: Prepare Your Node.js App
Before setting up your app as a systemd service, make sure your Node.js app is ready to go. Here’s what you need:
- A working Node.js app: Ensure your app runs fine using
node app.js
or your usual start command. - Package manager: You should have npm (Node Package Manager) installed to handle dependencies.
For example, if you’re using Express, your package.json
should list all your app’s dependencies.
Step 2: Create a systemd Service File
Now, it’s time to tell systemd how to manage your Node.js app. We do this by creating a service file. A service file is just a configuration file where you define how your app should run, where it’s located, and how it should behave.
Here’s how to create the service file:
- Create the service file: Go to the
/etc/systemd/system/
directory. You need root access to create a service file here.Open a terminal and type the following command to create the service file:
sudo nano /etc/systemd/system/myapp.service
Replace
myapp
with your app’s name. - Add the following content to the service file:
[Unit] Description=My 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 Group=your-group Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=production KillMode=process TimeoutSec=10 [Install] WantedBy=multi-user.target
Let’s break this down:
- Description: A short description of your app.
- ExecStart: The command to start your Node.js app. Replace
/path/to/your/app/app.js
with the actual path to your app. - WorkingDirectory: The directory where your app lives.
- Restart: This tells systemd to restart your app if it crashes. “always” means it will restart no matter what.
- User and Group: The user and group under which your app should run.
- Environment: You can set environment variables, like the
NODE_ENV
, to specify if it should run in production or development mode. - KillMode: Tells systemd how to stop your app. “process” ensures it stops the Node.js process correctly.
- TimeoutSec: The number of seconds systemd will wait before considering the service to be “failed.”
Once you’ve added this information, save the file and exit. In nano, you do this by pressing Ctrl + O
to save and Ctrl + X
to exit.
Step 3: Reload systemd
Now that you’ve created your service file, you need to reload systemd to make it aware of the new service.
In your terminal, run:
sudo systemctl daemon-reload
This command reloads systemd’s configuration, making your app’s service available for use.
Step 4: Start and Enable the Service
Let’s start the service and make it run automatically when the system boots:
- Start the service:
sudo systemctl start myapp.service
- Check if the service is running:
You can check the status of your service with:
sudo systemctl status myapp.service
You should see something like this:
● myapp.service - My Node.js App Loaded: loaded (/etc/systemd/system/myapp.service; disabled; vendor preset: enabled) Active: active (running) since Fri 2025-05-07 12:00:00 UTC; 5 minutes ago Main PID: 12345 (node) CGroup: /system.slice/myapp.service └─12345 /usr/bin/node /path/to/your/app/app.js
If you see “active (running),” your app is up and running as a service!
- Enable the service to start on boot:
Run this command so that your Node.js app starts automatically every time your server reboots:
sudo systemctl enable myapp.service
Step 5: Managing Your Node.js Service
Now that your app is running as a systemd service, you can manage it easily. Here are some commands you can use:
- Stop the service:
sudo systemctl stop myapp.service
- Restart the service:
sudo systemctl restart myapp.service
- View the logs:
You can see your app’s logs using
journalctl
:sudo journalctl -u myapp.service
This will show the log output of your Node.js app. It’s super helpful for debugging.
Step 6: Troubleshooting
If your app doesn’t seem to be working, here are a few things to check:
- Permissions: Make sure the user and group you specified in the service file have the correct permissions to access your app and its files.
- App Errors: Check the logs using
journalctl
for any errors in your app. - Service File Mistakes: Double-check the paths in the service file, especially the
ExecStart
andWorkingDirectory
.
Conclusion
Setting up a Node.js app as a systemd service is a game-changer. It saves you from the hassle of manually starting your app every time your server reboots, and it gives you easy control over the app’s lifecycle. Plus, it’s a great way to keep your app running smoothly without constant supervision.
As a bonus, this method helps you focus on building your app instead of worrying about whether it’s running or not. If you ever want to change things up, you can modify the systemd service file to fit your needs. After all, a little tweak here and there can make a big difference!
By the way, if you’re tired of restarting your app manually, I hope this guide gives you a little more freedom to focus on writing code instead of managing services.