1. What is systemctl
?
Think of systemctl
as your app’s personal assistant. It’s a command-line tool that works with systemd
, the service manager found in most modern Linux distributions. With systemctl
, you can:
- Start, stop, and restart services.
- Enable services to start automatically when the server boots.
- Check the status of services to troubleshoot issues.
It’s a powerful tool that integrates deeply with the operating system, making it a reliable choice for managing your Node.js app.
2. Why Choose systemctl
for Node.js?
You might have heard of PM2, a popular process manager for Node.js. While PM2 is great, systemctl
has some unique advantages:
- Built-In Reliability: No need to install extra tools—
systemctl
comes with your Linux OS. - Automatic Restarts: If your app crashes or the server reboots,
systemctl
can bring it back to life automatically. - Centralized Logs: All logs are managed by
journalctl
, so you don’t need additional logging tools. - Security: You can run your app as a non-root user, reducing security risks.
NOTE: If your application is small and does not require many advanced features, you can consider using systemctl to manage your service as a lightweight solution.
3. What You’ll Need
Before we dive in, make sure you have the following:
-
Node.js Installed
Run this command to check:node -v
If it’s not installed, you can use
nvm
or download it from the Node.js website. -
Your Node.js App
Have your app ready to go. If you don’t have one yet, don’t worry—we’ll create a simple example.
4. Setting Up Your Node.js App
Let’s start with a basic Node.js app. Create a file called app.js
and add the following code:
// filepath: /app/app.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World from Node.js!\n');
});
const PORT = 5000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
This app starts a simple HTTP server that listens on port 5000 and responds with "Hello, World from Node.js!".
5. Creating a Systemd Service File
Now, let’s tell systemctl
how to manage your app. Here’s how:
-
Navigate to the systemd directory:
cd /etc/systemd/system/
-
Create a service file for your app:
vim node-app.service
-
Add this configuration:
[Unit] Description=Node.js Application After=network.target [Service] ExecStart=node app.js Restart=always User=nobody Group=nogroup Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=production WorkingDirectory=/app # replace it with your project directory [Install] WantedBy=multi-user.target
ExecStart
: The command to start your app.Restart=always
: Ensures your app restarts if it crashes.User
andGroup
: Runs the app as a non-root user for security.Environment
: Sets environment variables.WorkingDirectory
: The directory where your app is located.
-
Save and exit the file.
6. Starting and Enabling the Service
Let’s get your app up and running:
-
Reload systemd to recognize the new service:
systemctl daemon-reload
-
Start the service:
systemctl start node-app
-
Enable it to start automatically on boot:
systemctl enable node-app
-
Check the status to ensure everything’s working:
systemctl status node-app
7. Viewing Logs
Need to troubleshoot or monitor your app? Use journalctl
to view logs:
journalctl -u node-app -f
The -f
option lets you follow the logs in real time, so you can see new entries as they appear.
8. Managing the Service
Here are some handy commands to manage your app:
-
Stop the service:
systemctl stop node-app
-
Restart the service:
systemctl restart node-app
-
Disable the service from starting on boot:
systemctl disable node-app
9. Why systemctl
Over PM2?
Here’s why I recommend systemctl
:
- Built-In Tool: No need to install anything extra—it’s already on your server.
- Centralized Logs: All logs are in one place with
journalctl
. - Unified Management: Manage all your server’s services with the same tool.
- Security: Run your app as a non-root user for better security.
10. Wrapping Up
And that’s it! You’ve set up your Node.js app with systemctl
, ensuring it runs reliably and securely. Whether you’re managing a small project or a large-scale application, systemctl
is a powerful tool to have in your arsenal. Happy coding!