有很多next、nuxt、yarn的nodejs程序,都是什么npm start或者yarn develop的启动模式,如何搞成systemd的服务呢,这里面麻烦的就是systemd的服务需要使用全路径。

那方法如下:

一、安装nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
二、安装nodejs和yarn
source ~/.nvm/nvm.sh

nvm ls-remote

nvm install v20.19.5

node -v

npm install -g yarn

npm list -g
三、弄strapi.service服务
echo $NVM_DIR     # 确定 nvm 的安装目录
/root/.nvm

which node        # 确定 Node.js 的路径
/root/.nvm/versions/node/v20.19.5/bin/node

which yarn        # 确定 Yarn 的路径
/root/.nvm/versions/node/v20.19.5/bin/yarn

cat < EOF > /etc/systemd/system/strapi.service 
[Unit]
Description=Strapi App
After=network.target

[Service]
# 使用用户账户运行服务,替换为实际的用户名
User=root
Group=root

# 指定工作目录,替换为项目的实际路径
WorkingDirectory=/data/strapi

# 加载 nvm 并运行 yarn 或 node
Environment="NVM_DIR=/root/.nvm"
ExecStart=/bin/bash -c ". $NVM_DIR/nvm.sh && yarn develop"

Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

systemctl enable --now strapi