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

沒有留言:

PowerShell 無法執行腳本

查看目前的執行原則 Get-ExecutionPolicy 預設原則為Restricted,也就是受到限制的意思,所以造成你無法執行Powershell的腳本。 PowerShell 執行原則共有七個: AllSigned: 可執行已簽署的腳本 Bypass: 不會封鎖任何項目,...