[操作疑难] 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'.

test.js
  1. const http = require('http');

  2. const hostname = '0.0.0.0';
  3. const port = process.env.NODE_PORT || 3000;

  4. const server = http.createServer((req, res) => {
  5.   res.writeHead(200, { 'Content-Type': 'text/plain' });
  6.   res.write("Hello, world.");
  7.   res.end();
  8. });

  9. server.listen(port, hostname, () => {
  10.   console.log("Server running at http://" + hostname + ":" + port + "/");
  11. });
复制代码
test.service
  1. [Unit]
  2. After=network.target

  3. # 服务名称
  4. Description=My Node.js App

  5. [Service]
  6. Type=simple

  7. # 设定环境变数
  8. Environment=NODE_PORT=3000 NODE_ENV=production

  9. # 执行服务的使用者
  10. User=root

  11. # 启动服务指令
  12. ExecStart=/usr/bin/node /var/www/node4/test.js

  13. # 不正常停止时重新启动
  14. Restart=on-failure

  15. [Install]
  16. WantedBy=multi-user.target
复制代码
附件: 您需要登录才可以下载或查看附件。没有帐号?註册

本帖最后由 javacomhk 于 2022-5-19 03:49 编辑

Avoid running nodejs service as root for security reasons.

change it to something like
User=ubuntu

TOP

原来 permission 问题, copy node 去 /usr/local/bin/ 就ok, thanks.

TOP

原来 permission 问题, copy node 去 /usr/local/bin/ 就ok, thanks.
gamezz 发表于 2022-5-19 12:37 PM


乱咁搬啲 program 只会衍生更多问题
建议你用 pm2 管理
pm2 start test.js
然后行 pm2 startup 佢会帮你生成 startup script

TOP

回覆 4# louislam

之前系用pm2,只系试下另一个方法系咪好d

TOP