2018年9月20日 星期四

讓 nodejs 跑 service

編輯 /etc/systemd/system/nodejs.service
[Unit]
Description=nodejs server

[Service]
ExecStart=/usr/bin/node /var/nodejs/index.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/nodejs

[Install]
WantedBy=multi-user.target

編輯測試程式 /var/nodejs/index.js 內容
var http = require('http');

var hostname = '10.10.10.10';
var port = 3849;

http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(port, hostname, function () {
console.log('Server running at http://%s:%s/', hostname, port);
});

重新載入所有service
systemctl daemon-reload

啟動 nodejs service
systemctl start nodejs

用lynx連結 http://10.10.10.10:3849,網頁顯示 Hello World 表示成功

設定自動啟動 nodejs
systemctl enable nodejs

沒有留言:

OUTLOOK 無法加密連線 POP3S SMTPS

最近郵件主機加上了憑證,公司一堆老舊電腦卻無法連線, 原先以為是主機設定的問題搞了很久, 最終還是要谷歌大師出馬, 參考這篇 文章 就解決了