diff --git a/NodeJS.sh b/NodeJS.sh index 572dd43..6d19dfc 100755 --- a/NodeJS.sh +++ b/NodeJS.sh @@ -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" \ No newline at end of file diff --git a/README.md b/README.md index 3a23601..1d6d7dd 100644 --- a/README.md +++ b/README.md @@ -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//nodeapp/` +- The app entry point must be specify in `package.json` file, with `"main":""` 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) \ No newline at end of file +![VestaCP](https://logico.com.ar/img/2019/04/21/vestacp_proxy_setup.png) + +### Sample + +`admin 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 -c "pm2 del all"` for admin `runuser -l admin -c "pm2 del all"` +* List all instances: `runuser -l -c "pm2 list"` for admin `runuser -l admin -c "pm2 list"` + + +#### Documentation + +Pm2: [https://pm2.keymetrics.io/docs/usage/quick-start/] \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..15c4d4e --- /dev/null +++ b/install.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +cp -R ./NodeJS.* /usr/local/vesta/data/templates/web/nginx/ \ No newline at end of file