nodejs on port 443 with authbind
sudo nano /etc/init/myapp.conf
description "node.js server" author "author"
used to be: start on startup
until we found some mounts weren't ready yet while booting:
start on started mountall stop on shutdown
Automatically Respawn:
respawn respawn limit 99 5
script # Not sure why $HOME is needed, but we found that it is: export HOME="/usr/local/myapp/bin" cd /usr/local/myapp/bin
exec su nobody -c "/usr/bin/node /usr/local/myapp/bin/myapp.js >> /var/log/myapp/myapp.log 2>&1"
su -c 'authbind /usr/bin/node /usr/local/myapp/bin/myapp.js >> /var/log/myapp/myapp.log 2>&1' nobody
end script
post-start script # Optionally put a script here that will notifiy you node has (re)started # /root/bin/hoptoad.sh "node.js has started!" end script
EXAMPLE
script
export NODE_ENV=production
su -c 'authbind /usr/local/bin/node /data/apps/myapp/app.js 2>&1 >> /var/log/node.log' www-data
end script
var http = require('http');
var https = require('https');
var httpListener = undefined;
var protocol = undefined;
if (useSSL)
{
try
{
var sslOptions = {
key: fs.readFileSync('sslcerts/cert.key'),
ca: fs.readFileSync('sslcerts/intermediate.crt'),
cert: fs.readFileSync('sslcerts/cert.crt')
};
httpListener = https.createServer(sslOptions, processRequest);
listenPort = listenSSLPort;
protocol = "https";
}
catch(error)
{
console.log("Cannot initialize SSL listener: " + error.message);
}
}
else
{
httpListener = http.createServer(processRequest);
protocol = "http";
}
if (httpListener != undefined)
{
httpListener.on('connection', function(stream)
{
LOGINFO("Connection established. remoteAddress: " + stream.remoteAddress + ":" + stream.remotePort);
});
httpListener.listen(listenPort, listenIP);