Skip to content

Commit

Permalink
feature: update readme
Browse files Browse the repository at this point in the history
feature: add install script copy all templates
feature: run node app by package.json "main" entry
feature: npm auto install
feature: pm2 init node app by name set on package.json file
  • Loading branch information
klich3 committed Nov 29, 2021
1 parent 876be87 commit 8096819
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
46 changes: 41 additions & 5 deletions NodeJS.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,45 @@ ip=$3
home=$4
docroot=$5

mkdir "$home/$user/web/$domain/nodeapp"
chown -R $user:$user "$home/$user/web/$domain/nodeapp"
rm "$home/$user/web/$domain/nodeapp/app.sock"
runuser -l $user -c "pm2 start $home/$user/web/$domain/nodeapp/app.js"
#default script name
mainScript="app.js"
nodeDir="$home/$user/web/$domain/nodeapp"

mkdir $nodeDir
chown -R $user:$user $nodeDir

#auto install dependences
if [ ! -d "$nodeDir/node_modules" ]; then
echo "No modules found."
cd $nodeDir && npm i
fi

#get init script form package.json
package="$nodeDir/package.json"

if [ -e $package ]
then
mainScript=$(cat $package \
| grep main \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g' \
| sed 's/ *$//g')

scriptName=$(cat $package \
| grep name \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g' \
| sed 's/ *$//g')
fi

rm "$nodeDir/app.sock"
runuser -l $user -c "pm2 del $scriptName"

#remove blank spaces
pmPath=$(echo "$nodeDir/$mainScript" | tr -d ' ')
runuser -l $user -c "pm2 start $pmPath --name $scriptName"

sleep 5
chmod 777 "$home/$user/web/$domain/nodeapp/app.sock"
chmod 777 "$nodeDir/app.sock"
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ Run Node.JS web apps automatically using NGINX reverse proxy, UNIX sockets and P
## Usage.

- PM2 must be installed.
- The app must be installed in `/home/user/web/domain/nodeapp/`
- The app entry point must be `/home/user/web/domain/nodeapp/app.js`
- The app must be copied in `/home/user/web/<domain>/nodeapp/`
- The app entry point must be specify in `package.json` file, with `"main":"<you_init_script.js>"` if your file are not have this entry, script use default one `/home/user/web/domain/nodeapp/app.js`
- The app must listen on a UNIX socket in `/home/user/web/domain/nodeapp/app.sock`
- Upload `NodeJS.tpl`, `NodeJS.stpl`, `NodeJS.sh` to `/usr/local/vesta/data/templates/web/nginx/`
- Upload `NodeJS.tpl`, `NodeJS.stpl`, `NodeJS.sh` to `/usr/local/vesta/data/templates/web/nginx/` or run `install.sh`
- In the control panel, select NodeJS from Proxy Template

![VestaCP](https://logico.com.ar/img/2019/04/21/vestacp_proxy_setup.png)
![VestaCP](https://logico.com.ar/img/2019/04/21/vestacp_proxy_setup.png)

### Sample

`admin <domain> 127.0.0.1 /home`
`/usr/local/vesta/data/templates/web/nginx/NodeJS.sh admin default 127.0.0.1 /home`

#### Sudo commands

* Remove all instances: `runuser -l <user> -c "pm2 del all"` for admin `runuser -l admin -c "pm2 del all"`
* List all instances: `runuser -l <user> -c "pm2 list"` for admin `runuser -l admin -c "pm2 list"`


#### Documentation

Pm2: [https://pm2.keymetrics.io/docs/usage/quick-start/]
3 changes: 3 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

cp -R ./NodeJS.* /usr/local/vesta/data/templates/web/nginx/

0 comments on commit 8096819

Please sign in to comment.