远程服务器的NodeJS与部署永远 [英] Remote nodejs server deployment with forever

查看:671
本文介绍了远程服务器的NodeJS与部署永远的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过后提交挂钩,这将调用远程机器执行实际的部署上的脚本部署节点JS在远程计算机上的服务器。

我现在面临的问题是,如果我正常工作的机器上运行远程脚本,但是当我试图用ssh命令来执行相同的脚本,服务器无法启动。

这是我的职位commit钩子如下:

 #!/斌/庆典#Connect AWS的机器上并运行脚本部署
SSH -i〜/文档/ AA-KP-inst1.pem Ubuntu的@<远程IP> SH /home/app/deploy.sh#Done
退出0

pretty简单。而这也正是deploy.sh如下:

 #!/斌/庆典#Navigate到服务器目录
CD /家庭/应用/ personal_website /服务器#停止正在运行的服务器(S)
永远停止-s 0>> forever.log#Pull最新code
未设置GIT_DIR
混帐-C /家庭/应用/ personal_website /服务器拉--quiet#Restart服务器
永远的开始-a -l forever.log -o out.log -e err.log server.js#结束
退出0

我看到了通过直接运行deploy.sh并通过SSH运行它启动的进程不同。

下面就是 PS-EF | grep的节点看起来,当我在远程机器上运行deploy.sh直接,​​如:

  Ubuntu的14058 1 3 04:26? 00:00:00在/ usr /斌/的NodeJS / usr / lib目录/ node_modules /永远/斌/显示器server.js
Ubuntu的14064 14058 2 04:26? 00:00:00在/ usr /斌/ /home/app/personal_website/server/server.js的NodeJS
Ubuntu的14071 10791 0 04:27分/ 0 00:00:00 grep的--color = auto的节点

这是我收到的时候我从SSH运行:

  Ubuntu的13435 1月14日04:19? 00:00:00在/ usr /斌/的NodeJS / usr / lib目录/ node_modules /永远/斌/显示器server.js
Ubuntu的13444 10791 0 4时19分/ 0零点00分00秒的grep --color = auto的节点

在这可能是导致此任何想法?为什么从SSH运行时仅监视进程启动?

至于问评论:
ENV 从机直接:

  XDG_SESSION_ID = 31
TERM = xterm的-256color
SHELL = /斌/庆典
SSH_CLIENT = 97.83.204.67 58752 22
SSH_TTY =的/ dev / PTS / 2
USER = Ubuntu的
MAIL =在/ var /邮件/ Ubuntu的
PATH=/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PWD = /家庭
LANG =的en_US.UTF-8
NODE_PATH = / usr / lib目录/的NodeJS:/ usr / lib目录/ node_modules:在/ usr /共享/ JavaScript的
SHLVL = 1
HOME = /家庭/ Ubuntu的
LOGNAME = Ubuntu的
SSH_CONNECTION = 97.83.204.67 58752 172.31.7.96 22
LESSOPEN = |在/ usr /斌/ lesspipe%S
XDG_RUNTIME_DIR = /运行/用户/ 1000
LESSCLOSE =的/ usr / bin中/ lesspipe%s%S
_ =的/ usr / bin中/ printenv
OLDPWD = /家庭/ EXPS

ENV 从SSH:

  XDG_SESSION_ID = 32
SHELL = /斌/庆典
SSH_CLIENT = 97.83.204.67 58966 22
USER = Ubuntu的
MAIL =在/ var /邮件/ Ubuntu的
PATH =的/ usr / local / sbin中:在/ usr / local / bin目录:/ usr / sbin目录:在/ usr / bin中:/ sbin目录:/ bin中:在/ usr /游戏:在/ usr /本地/游戏
PWD = /家庭/ Ubuntu的
LANG =的en_US.UTF-8
SHLVL = 1
HOME = /家庭/ Ubuntu的
LOGNAME = Ubuntu的
SSH_CONNECTION = 97.83.204.67 58966 172.31.7.96 22
XDG_RUNTIME_DIR = /运行/用户/ 1000
_ =的/ usr / bin中/ printenv


解决方案

在这种情况下,环境变量是本地电话,并通过ssh的通话之间略有不同。

href=\"http://stackoverflow.com/users/3669252/aa333\">运算aa333 确认<一的in该评论


  

假如运行脚本的其余部分之前导出 NODE_PATH


这允许节点命令永远的通过SSH会话运行。


更一般地,对于一个钩,我总是建议:

添加您的混帐拉前

 取消设置GIT_DIR

如果你不取消设置 GIT_DIR ,那么,当一个钩子执行时,它仍然会运行,因为它是在你按下混帐回购协议(而不是之一,你改变目录)。

请参阅例如:

作为第二个链接,我会,可以肯定,做:

 混帐--git-DIR = /家庭/应用/ personal_website /服务器/ git的 - 共同努力树= /家庭/应用/ personal_website /服务器拉--quiet

或者,因为git的1.8.5

 混帐-C /家庭/应用/ personal_website /服务器拉--quiet

I'm trying to deploy a node js server on a remote machine by a post-commit hook, which would call a script on the remote machine that does the actual deployment.

The problem I'm facing is if I run the remote script on the machine it works fine, but when I try to execute the same script by an ssh command the server doesn't start.

This is what my post commit hook looks like:

#!/bin/bash

#Connect to AWS machine and run deploy script
ssh -i ~/Documents/aa-kp-inst1.pem ubuntu@<remote-ip> "sh /home/app/deploy.sh"

#Done
exit 0

Pretty straightforward. And this is what deploy.sh looks like:

#!/bin/bash

#Navigate to server directory
cd /home/app/personal_website/server

#Stop currently running server(s)
forever stop -s 0 >> forever.log

#Pull latest code
unset GIT_DIR
git -C /home/app/personal_website/server pull --quiet

#Restart server
forever start -a -l forever.log -o out.log -e err.log server.js

#End
exit 0

I see a difference in the processes started by running deploy.sh directly and by running it through ssh.

Here's what ps-ef | grep node looks like when I run deploy.sh directly on the remote machine:

ubuntu   14058     1  3 04:26 ?        00:00:00 /usr/bin/nodejs      /usr/lib/node_modules/forever/bin/monitor server.js
ubuntu   14064 14058  2 04:26 ?        00:00:00 /usr/bin/nodejs /home/app/personal_website/server/server.js
ubuntu   14071 10791  0 04:27 pts/0    00:00:00 grep --color=auto node

And this what I get when I run it from ssh:

ubuntu   13435     1 14 04:19 ?        00:00:00 /usr/bin/nodejs /usr/lib/node_modules/forever/bin/monitor server.js
ubuntu   13444 10791  0 04:19 pts/0    00:00:00 grep --color=auto node

Any idea on what could be causing this? Why does only the monitor process start when run from ssh?

As asked in a comment: env from the machine directly:

XDG_SESSION_ID=31
TERM=xterm-256color
SHELL=/bin/bash
SSH_CLIENT=97.83.204.67 58752 22
SSH_TTY=/dev/pts/2
USER=ubuntu
MAIL=/var/mail/ubuntu
PATH=/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PWD=/home
LANG=en_US.UTF-8
NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
SHLVL=1
HOME=/home/ubuntu
LOGNAME=ubuntu
SSH_CONNECTION=97.83.204.67 58752 172.31.7.96 22
LESSOPEN=| /usr/bin/lesspipe %s
XDG_RUNTIME_DIR=/run/user/1000
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/printenv
OLDPWD=/home/exps

env from ssh:

XDG_SESSION_ID=32
SHELL=/bin/bash
SSH_CLIENT=97.83.204.67 58966 22
USER=ubuntu
MAIL=/var/mail/ubuntu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PWD=/home/ubuntu
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/ubuntu
LOGNAME=ubuntu
SSH_CONNECTION=97.83.204.67 58966 172.31.7.96 22
XDG_RUNTIME_DIR=/run/user/1000
_=/usr/bin/printenv

解决方案

In this case, the environment variable was slightly different between a local call and a call through ssh.

The Op aa333 confirms in the comments:

Had to export NODE_PATH before running the rest of the script.

That allows for the node command forever to run through the ssh session.


More generally, for a hook, I always recommend:

Add before your git pull:

unset GIT_DIR

If you don't unset GIT_DIR, then, when executed in a hook, it will still operate as it was in the git repo where you push (and not the one where you change directory to).

See for instance:

As in the second link, I would, to be sure, do a:

git --git-dir=/home/app/personal_website/server/.git --work-tree=/home/app/personal_website/server pull --quiet

Or, since git 1.8.5:

git -C /home/app/personal_website/server pull --quiet

这篇关于远程服务器的NodeJS与部署永远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆