Hi there! Since this post was originally written,
nvm has gained some new tools, and some people have suggested alternative (and potentially better) approaches for modern systems. Make sure to have a look at the comments to this article, before following this guide!
Trickier than it seems.
Let's assume that you've already created an unprivileged user named
myapp. You should never run your Node.js applications as root!
Switch to the
myapp user, and do the following:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash(however, this will immediately run the nvm installer - you probably want to just download the
install.shmanually, and inspect it before running it)
- Install the latest stable Node.js version:
nvm install stable
Your package.json must specify a
start script, that describes what to execute for your application. For example:
"start": "node app.js"
Save this as
You'll want to change the
WorkingDirectory paths to reflect your application setup.
Next, save this as
/home/myapp/start.sh (adjusting the username in both the path and the script if necessary):
This script is necessary, because we can't load nvm via the service file directly.
Make sure to make it executable:
chmod +x /home/myapp/start.sh
my-application with whatever you've named your service file after, running the following as root:
systemctl enable my-application
systemctl start my-application
To verify whether your application started successfully (don't forget to
npm install your dependencies!), run:
systemctl status my-application
... which will show you the last few lines of its output, whether it's currently running, and any errors that might have occurred.