Skip to content

Latest commit

 

History

History
107 lines (81 loc) · 2.5 KB

use-pm2.md

File metadata and controls

107 lines (81 loc) · 2.5 KB

Run CSS with pm2

We want the CSS to start automatically when server restarts, or when CSS crashes. We'll use pm2 to do that.

Install pm2

sudo npm install -g pm2

Then, make sure it will start when server restarts

# run as [user]
pm2 startup

This command will output another command that you should execute. In my case it looks like this:

## don't do this!
## generate your own with `pm2 startup`! (run it as [user])
## run as [user] or root
# sudo env PATH=$PATH:/snap/node/7581/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u [user] --hp /home/[user]

Run the server with pm2

Create script to run with pm2

  1. Create a file at ~/www/[projectname]/run.sh:

    # run as [user]
    touch ~/www/[projectname]/run.sh
  2. Open it with vim or nano, and paste your favourite way to run the CSS, for example:

    #!/bin/bash
    
    # Source the nvm initialization script
    # this is a little tweak to make nvm available in the context of pm2
    source /home/solidtest/.nvm/nvm.sh
    
    # put your favourite way to run CSS here, for example
    nvm exec 18 npx -y @solid/community-server@7 -f ./data/ -c ./config.json -p [port] -b https://[your.domain]
  3. Make the script executable:

    # run as [user]
    chmod +x ~/www/[projectname]/run.sh
  4. Test it

    # run as [user]
    # go to project folder
    cd ~/www/[projectname]
    # cd ~/www/[projectname]/CommunitySolidServer # if you run it from git repo
    # execute
    . ./run.sh

Use pm2 to run the server

  1. Start CSS:
    # run as [user]
    cd ~/www/[projectname]
    # cd ~/www/[projectname]/CommunitySolidServer # if you run it from git repo
    pm2 start run.sh --name [for-example-your.domain]
  2. Check that the server runs
  3. Persist the settings
    # run as [user]
    pm2 save
  4. Try to reboot your machine and see if CSS starts automatically

Some useful commands to manage pm2

# each user manages its own pm2 instance

# see list of pm2 processes
pm2 list
# you'll see a list including name and id of each process

# see log of a particular process
pm2 log [name-or-id]

# start, restart, and stop process
pm2 start [name-or-id]
pm2 restart [name-or-id]
pm2 stop [name-or-id]

# see details about a process
pm2 describe [name-or-id]

# see nice overview of running processes
pm2 monit

Next: Set up mashlib as default app