setting NODE_ENV for node.js + expressjs application as a daemon under ubuntu
Asked Answered
R

5

28

i got the daemon working alright with these instructions: http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/

but because this starts the application in DEVELOPMENT mode, the log file gets spammed with socket.io debug logs.

i tried setting the NODE_ENV to production in the upstart-conf-file but had no success.

script
    export HOME="/root"
    export NODE_ENV=production

    exec /usr/local/bin/node /where/yourprogram.js >> /var/log/node.log 2>&1
end script

didn't work.

Rasure answered 11/8, 2011 at 8:11 Comment(0)
E
27

Try

exec NODE_ENV=production /usr/local/bin/node /where/yourprogram.js >> /var/log/node.log 2>&1

In my setup I'm sudoing as a lesser user, so it's

exec sudo -u some-user NODE_ENV=production /usr/local/bin/node /where/yourprogram.js >> /var/log/node.log 2>&1

and since it's spawning off another user it probably has another environment. I'm a newbie here, but it works for me.

Ensign answered 18/8, 2011 at 23:56 Comment(3)
The first option does not work and throws the following error: /proc/self/fd/9: 3: exec: NODE_ENV=production: not foundClywd
That might be because I'm relying on bash syntax. Check which shell you are using and correct the command accordingly.Ensign
This is what worked for me in the end script export NODE_ENV=production exec /usr/bin/node /someapp/server.js >> /var/log/someapp.log 2>&1 end scriptClywd
H
18

Here's a simpler upstart script you can use. Upstart now supports everything you need to do directly without script sections or too much embedded shell syntax. This includes environment variables (env), working directory (chdir), user/group (setuid, setgid), log handling (console log), etc. Your log files will be handled and rotated into /var/log/upstart/your_app.log

description "start and stop the example express.js/node.js server"
author "John Doe <[email protected]>"

start on filesystem and started networking
respawn
console log
chdir /opt/your_app
setuid your_app_user
setgid your_app_user
env PATH=./node_modules/.bin:./node/bin:/usr/bin
env NODE_ENV=production
exec app/server.js
Hickson answered 11/8, 2011 at 15:34 Comment(0)
S
15

If you are using node.js in production, I recommend you use forever.js to daemonize your program https://github.com/nodejitsu/forever

Install using npm: [sudo] npm install forever -g

export NODE_ENV=production and run forever start app.js You can also specify where to put error and stdout logs.

Schaub answered 11/8, 2011 at 9:55 Comment(4)
For something more advanced, you can also check out: github.com/learnboost/clusterSchaub
Just to clarify on @Schaub last comment, Cluster doesn't solve the "keep it running" problem like Upstart with Respawn, Upstart + Monit, or Forever.Teratism
IMHO Upstart is better approach under Ubuntu than foreverRosannarosanne
Notice if, for any reason, you have to launch forever command as sudo, you have to set the NODE_ENV environment variable in the same line: sudo NODE_ENV=production forever start app.jsPaulitapaulk
I
4

to set NODE_ENV in heroku use:

heroku config:set NODE_ENV="production"
Italia answered 17/6, 2014 at 23:8 Comment(0)
S
2

Ubuntu/Upstart are listed in the question, but I got here while looking for answers for a FreeBSD/system shell daemon.

The line below started the app in "development" environment:

exec node path/to/start/script.js

The line below started the app in "production" environment:

NODE_ENV=production exec node path/to/start/script.js

It took me a while to figure this out, so I thought I'd share.

Strenuous answered 10/12, 2015 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.