作者: gamezz 時間: 2022-5-19 10:41 標題: oracle cloud centos 8 使用 Systemd 建立 Node.js rest server service
本帖最後由 gamezz 於 2022-5-19 10:45 編輯
我跟住這教學在oracle cloud centos 8 使用 Systemd 建立 Node.js rest server service:
https://blog.gtwang.org/linux/no ... d-service-tutorial/
可是 status 顯示 test.service: Start request repeated too quickly.
test.service: Failed with result 'exit-code'.
[attach]2320924[/attach]
test.js
- const http = require('http');
- const hostname = '0.0.0.0';
- const port = process.env.NODE_PORT || 3000;
- const server = http.createServer((req, res) => {
- res.writeHead(200, { 'Content-Type': 'text/plain' });
- res.write("Hello, world.");
- res.end();
- });
- server.listen(port, hostname, () => {
- console.log("Server running at http://" + hostname + ":" + port + "/");
- });
- [Unit]
- After=network.target
- # 服務名稱
- Description=My Node.js App
- [Service]
- Type=simple
- # 設定環境變數
- Environment=NODE_PORT=3000 NODE_ENV=production
- # 執行服務的使用者
- User=root
- # 啟動服務指令
- ExecStart=/usr/bin/node /var/www/node4/test.js
- # 不正常停止時重新啟動
- Restart=on-failure
- [Install]
- WantedBy=multi-user.target
作者: javacomhk 時間: 2022-5-19 11:48
本帖最後由 javacomhk 於 2022-5-19 03:49 編輯
Avoid running nodejs service as root for security reasons.
change it to something like
User=ubuntu
作者: gamezz 時間: 2022-5-19 12:37
原來 permission 問題, copy node 去 /usr/local/bin/ 就ok, thanks.
作者: louislam 時間: 2022-5-19 21:25
亂咁搬啲 program 只會衍生更多問題
建議你用 pm2 管理
pm2 start test.js
然後行 pm2 startup 佢會幫你生成 startup script
作者: gamezz 時間: 2022-5-20 22:31
回覆 4# louislam
之前係用pm2,只係試下另一個方法係咪好d

