Search code examples
node.jsreactjslinuxservicedocusaurus

How can I start Docusaurus as a service on Linux (Ubuntu)


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.


Solution

  • 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.