Strapi start with systemd?

Hi. I can start Strapi with npm run develop and npm run start without problems. However, when I try to start Strapi as systemd Service, I run into the following error message:

[15210]: > fs@0.1.0 start /home/username/strapi/fs
[15210]: > strapi start
[15210]: /home/username/strapi/fs/node_modules/fs-extra/lib/mkdirs/make-dir.js:85
[15210]: } catch {
[15210]: ^
[15210]: SyntaxError: Unexpected token {
[15210]: at createScript (vm.js:80:10)
[15210]: at Object.runInThisContext (vm.js:139:10)
[15210]: at Module._compile (module.js:617:28)
[15210]: at Object.Module._extensions…js (module.js:664:10)
[15210]: at Module.load (module.js:566:32)
[15210]: at tryModuleLoad (module.js:506:12)
[15210]: at Function.Module._load (module.js:498:3)
[15210]: at Module.require (module.js:597:17)
[15210]: at require (internal/module.js:11:18)
[15210]: at Object. (/home/username/strapi/fs/node_modules/fs-extra/lib/mkdirs/index.js:3:44)
[15210]: npm ERR! code ELIFECYCLE
[15210]: npm ERR! errno 1
[15210]: npm ERR! fs@0.1.0 start: strapi start
[15210]: npm ERR! Exit status 1
[15210]: npm ERR!
[15210]: npm ERR! Failed at the fs@0.1.0 start script.
[15210]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

This is my service file:

[Unit]
Description=Strapi systemd service

[Service]
Type=simple
WorkingDirectory=/home/username/strapi/fs
User=999
Environment=“NODE_ENV=production”
ExecStart=/home/username/.nvm/versions/node/v12.12.0/bin/npm run start
Restart=always

[Install]
WantedBy=multi-user.target
~

What am I missing?

Use PM2 with it’s own systemd startup system: PM2 - Startup Script

PM2 is designed to be a service manager for Node applications

Thanks for your reply. I am aware of the PM2 solution, however, PM2 has been discussed in regard of potential licensing issues (GNU APGL 3.0). So, I am still wondering why the standard systemd way in which I start all my Node.JS applications does not work with Strapi. Does anyone know the technical reason?

Unless you are modifying the pm2 source code, usage of it should not matter there. (I’m guessing your company has some kind of blanket ban on usage of AGPLv3 which is a bit of a “burn everything” approach that I generally disagree with but that’s a subjective opinion not a legal one)

To be honest I’ve never tried to run strapi with a dedicated systemd script, and I’ve never actually seen anyone else try including many large enterprises that are our customers in various sectors.

Typically the 3 go-tos for node services are:

  • PM2 (easily 80%)
  • Docker or some kind of container
  • forever: forever - npm

Okay, thank you very much!

I would say here on what @DMehaffy said i would suggest Docker to run it in a container as then it should not matter what system you are running it on :slight_smile:

While I’m not a massive fan of docker or containers personally (subjective and bias opinion). This is a good point.

1 Like

Here is a Dockerfile that can be used

FROM node:14-alpine
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY ./package.json ./
COPY ./yarn.lock ./
ENV PATH /opt/node_modules/.bin:$PATH
RUN yarn config set network-timeout 600000 -g
RUN yarn install
WORKDIR /opt/app
COPY ./ .
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]

Built the image

docker build -t my-strapi .

You can then use it like docker run -dp 1337:1337 my-strapi
-d means detached so run in the background and p is to give the port that we want to use. you can switch it to 80:1337 to map it to port 80 if you want this.

Nice! Many thanks

You’re welcome.
In simple terms is what it does.
Uses node-14 alpine for production as a small image.
Setting the node_env to production unless this is passed in.
Setting a workdir inside the container, then copying the package.json and yarn.lock (if you are not using yarn you can change yarn to npm etc. i just prefer use it for package stability and versions.

The image itself won’t do much btw. As it will just build it, but you can use a nginx image to take what you just built copy over the build folder and place it inside nginx etc.

Under PM2 5.1.1, we are experiencing restarts of Strapi 3.6.8 under moderate load. Are there some best practices for PM2 that we should be looking at?

I’d need more information as to why it’s restarting, any logs or something? (we lightly cover usage with pm2 here: Process Manager - Strapi Developer Documentation )

Was reading this as I needed a systemd file to start STRAPI on my raspberry pi. This file is currently working, but I also needed to run my strapi api from a bash script, which included getting node set to the proper version. Y’all might not need that. Both scripts are included:
Systemd file:
[Unit]
Description=STRAPI Blog API [or whatever you wish]

[Service]
type=notify
WorkingDirectory=/path/to/your/strapi/app
ExecStart=/path/to/your/strapi/app/runblog [runblog is the bash start script]
SyslogIdentifier=STRAPIB [or,whatever you wish]
User=yourusername
Restart=always
RestartSec=9
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target

I copied and modified the above file from one I use to start a dotnet API. Items in [brackets] are just notes, not part of file. Use your username. I do suspect that mine might be working because it runs inside my home folder structure where I have node installed.

Bash startup script:
#!/bin/bash

export NVM_DIR="$HOME/.nvm"
[ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh”
[ -s “$NVM_DIR/bash_completion” ] && . “$NVM_DIR/bash_completion”
source ~/.bashrc
sleep 2
nvm use 14
node --version
npm run develop

House this script inside the folder where your app resides. Again, the initial part is because my pi (and laptop) has a habit of not staying on the right version. I wasn’t happy I had to downgrade node and npm. Syslog was reporting the errors that node wasn’t high enough (my pi is at 10, usually).
Use at your own discretion and risk. Your results may vary. But, it is working on my Raspberry Pi running the most recent Buster.