0

Hi guys I try to start Docusaurus V2 as a service on ubuntu headless. Currently I have a script in my Docusaurus folder as a temporarly solution. It works when I start it with ./start.sh:

/var/www/citro-docs-2/start.sh

#!/bin/bash

npm run serve

I tried to create a service like that:

/etc/systemd/system/docusaurus.service

[Unit]
Description=Docusaurus Service

[Service]
ExecStart=/var/www/citro-docs-2/start.sh
WorkingDirectory=/var/www/citro-docs-2

[Install]
WantedBy=multi-user.target

But when I start it with sudo systemctl start docusaurus.service and check the status I get these errors:

● docusaurus.service - Docusaurus Service
     Loaded: loaded (/etc/systemd/system/docusaurus.service; disabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2021-08-25 11:53:20 UTC; 4s ago
    Process: 3494 ExecStart=/var/www/citro-docs-2/start.sh (code=exited, status=1/FAILURE)
   Main PID: 3494 (code=exited, status=1/FAILURE)

Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at require (node:internal/modules/cjs/helpers:94:18)
Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at Object.<anonymous> (/var/www/citro-docs-2/node_modules/@docusaurus/c>
Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at Module._compile (node:internal/modules/cjs/loader:1101:14)
Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153>
Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at Module.load (node:internal/modules/cjs/loader:981:32)
Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/r>
Aug 25 11:53:20 citro-docs2 start.sh[3521]:     at node:internal/main/run_main_module:17:47
Aug 25 11:53:20 citro-docs2 systemd[1]: docusaurus.service: Main process exited, code=exited, status=1/FAILURE
Aug 25 11:53:20 citro-docs2 systemd[1]: docusaurus.service: Failed with result 'exit-code'.

I found a solution where I can start the .js file directly: How do I run a node.js app as a background service? but I don't know if this is also possible for Docusaurus. At least I don't know which .js file I should start. It would be awesome if someone could help.

Micha
  • 3
  • 1
  • Also, sometimes, lots of new things together can be overwhelming and confusing. When this happens to me, I like to break it up into simpler things. For example, I'd start with being able to output "Hello World" from a node command. Then running a server. Then running the server as a service (aka daemon). Then running Docusaurus. Then running it as a service. – Raphael Rafatpanah Aug 25 '21 at 18:59

1 Answers1

0

Yes, it is possible to run Docusaurus as a service on Ubuntu. It's important to mention that what you're trying to do here is quite normal for Node.js apps. After all, if you don't run your Node.js app as a service, then the service would stop running as soon as you close the shell you used to start the app.

However, it seems like most folks don't run Docusaurus on Ubuntu since it's "just a static site" and static sites don't really need a full blown server to run on. For example, you could leverage GitHub Pages to run your docs site.

That being said, Docusaurus is just a Node.js app. It's similar to all other Node.js apps, and you can certainly run it in Ubuntu if you wanted to.

Take a look at the docs on PM2.

The way I would start your app as a service is:

  1. Install Node.js on your Ubuntu server (if it isn't already installed)
  2. Install pm2 globally on your server via npm install -g pm2
  3. Navigate to your app's directory and run npm install to install the dependencies of your app.
  4. Now you can run pm2 start /var/www/citro-docs-2/start.sh --name myDocsSite

Your Docusaurus app should now be running. To stop it, you can run pm2 stop myDocsSite.

Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158
  • Thanks for the answer, this is definitely a good way, I will keep that in mind. But I try to get it to run as a systemd service like this: https://stackoverflow.com/questions/4018154/how-do-i-run-a-node-js-app-as-a-background-service In the first answer they link to the main .js file, but in Docusaurus I don't really know which this is. Is there a way to find that out with an npm command or something like that? – Micha Aug 26 '21 at 06:57
  • `npm run serve` is an alias for another command that can be found in your `package.json` file. What is the value of that alias? You'll see a `"scripts"` field in your `package.json` with a field called `"serve": ""`. – Raphael Rafatpanah Aug 27 '21 at 19:05
  • However, let me scratch my entire previous answer and replace it with this: Since Docusaurus is generating static assets, then why not just install something like `nginx` on your Ubuntu server to serve them? You may already be more familiar with that anyway. – Raphael Rafatpanah Aug 27 '21 at 19:08