At some point I'm going to write up all the crap I've done to host node.js on Linode easily. It's really kind of a pain if you're coming into it fairly cold. I mean, I've been using *NIX since the 90s, but when you really are the only schmoe responsible for everything, it's a new experience.

There are three main things I needed to learn to code and release node:

  1. How to set up firewalls -- ufw and iptables.
  2. How to execute node in an extended test mode that didn't shut down when I logged out.
    • Both for testing and for production
  3. How to fix case sensitivity
    • Okay, I'm a dope. As you may remember, this one got me, though I have better fix now.

I'm going to hit number two right now, just for fun.

Use forever

Here's a good resource, but let's hit the high points:

$ [sudo] npm install forever
$ forever start simple-server.js
$ forever list
    [0] simple-server.js [ 24611, 24596 ]
$ forever stop 0

See how the forever stop 0 matches the [0] from forever list? Then you've got it.

... and one not on the list...

$ forever stopall

Success.

Why is that useful? Two reasons. First, I have a testing service set up that mirrors the "real" site so that I can stage changes both to my website and to the express server. That's at another port, and it's good to be able to start that code from the command line with something better than node app.js (or nodejs app.js as the case may be. If things act funky, try a which node and start sniffing).

But I might want that staging server to be up indefinitely. I probably don't really need it to stay up if the VM blows up, but I want it to stay even when I move from tower to laptop and back. Forever does this nicely.

Secondly, to "really" keep things running, you want to create something for systemd, which apparently replaced upstart in Ubuntu to some controversy a while back, that also ends up calling forever.

Fastest intro to systemd EVAH

$ cd /etc/systemd/system/
$ [sudo?] vim myservice.service

Now put something in the service file like this:

[Service]
ExecStart=/usr/local/bin/forever /home/yourLogin/path/to/your/app.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node-sample
#User=srv-node-sample
#Group=srv-node-sample
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Now [esc]:wq (for you vi[m] users).

That seems to be alls you need. Notice that the ExecStart part has forever (and the full path to forever in it).

Here's the rest of the magic:

$ sudo systemctl start myservice.service
$ sudo systemctl status myservice.service
โ— myservice.service
   Loaded: loaded (/etc/systemd/system/myservice.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2016-11-30 12:16:47 EST; 1 weeks 5 days ago
   Main PID: 30575 (node)
      CGroup: /system.slice/exweb.service
              โ”œโ”€12345 node /usr/local/bin/forever /home/yourLogin/path/to/your/app.js
              โ””โ”€12346 /usr/bin/nodejs /home/yourLogin/path/to/your/app.js

$ sudo systemctl stop myservice.service

And now you're booting up your node server indefinitely or as a service in case of reboot.

I'll try to remember to follow-up on the other two. The case sensitivity one is the more interesting problem; it's borderline fun. ;^)

Labels: , , ,