For Amazon Linux 2023, the recommended way to run and manage a Node.js app in production is using systemd — the native process manager and service controller built into modern Linux distributions (including Amazon Linux 2023).
Why systemd?
✅ Preinstalled and supported on Amazon Linux 2023
✅ Automatically starts on boot
✅ Logs to journalctl
✅ No need to install PM2 or forever
Here’s how to set it up for your Node.js project:
✅ Step-by-Step: Run a Node.js App with systemd on Amazon Linux 2023
1. Install Node.js (if not already installed)
If you haven’t installed Node.js yet:
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs
Verify:
node -v
npm -v
2. Create a systemd service file
Let’s say your app is located at:
/home/ec2-user/myapp/app.js
Create a new service:
sudo nano /etc/systemd/system/myapp.service
Paste the following (replace paths/user accordingly):
[Unit]
Description=My Node.js App
After=network.target
[Service]
ExecStart=/usr/bin/node /home/ec2-user/myapp/app.js
WorkingDirectory=/home/ec2-user/myapp
Restart=always
RestartSec=5
Environment=NODE_ENV=production
User=ec2-user
Group=ec2-user
[Install]
WantedBy=multi-user.target
3. Reload systemd and enable your service
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
4. Check if your app is running
sudo systemctl status myapp
🔍 View logs:
journalctl -u myapp -f
(Press Ctrl+C to exit)
5. Manage the app
Restart: sudo systemctl restart myapp
Stop: sudo systemctl stop myapp
View logs: journalctl -u myapp
✅ Done! Your Node.js app will now auto-start on boot and stay running with systemd.