简体   繁体   English

如何在专用服务器上运行节点js?

[英]how to run node js on dedicated server?

I am doing a chat app and integrating it on a website. 我正在做一个聊天应用程序,并将其集成到网站上。 When i execute teh command 'node index.js' on the local server everything works fine. 当我在本地服务器上执行命令'node index.js'时,一切正常。 But when i try installing node js on a dedicated server and try to execute the command 'nohup node index.js &' through ssh it gives following message. 但是,当我尝试在专用服务器上安装节点js并尝试通过ssh执行命令“ nohup node index.js&”时,会显示以下消息。

nohup: ignoring input and appending output to `nohup.out' nohup:忽略输入并将输出附加到`nohup.out'

I had followed the method mentioned in this site for installation of node js on server https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts 我已经按照本站点中提到的方法在服务器https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts上安装节点js

Can someone help me, please? 有人能帮助我吗?

You first need to install Node in a correct way. 首先,您需要以正确的方式安装Node。 I wrote a tutorial about it: How to get Node 6.7.0 on Linux (of course you can use newer versions, just change the version in the commands). 我写了一篇关于它的教程: 如何在Linux上获得Node 6.7.0 (当然,您可以使用较新的版本,只需在命令中更改版本)。

Basically it's something like this - change the version to the one you like: 基本上就是这样-将版本更改为您喜欢的版本:

# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node

I recommend building Node from source and always running make test but you can also install a binary package which is faster - just make sure you understand the issues with paths and hashbang lines if you do so - more info on that and more install options are described in my tutorial . 我建议从源代码构建Node并始终运行make test但您也可以安装速度更快的二进制程序包-只需确保您了解路径和hashbang行的问题-并说明更多信息和更多安装选项在我的教程中

Then you need to make sure that your application is started every time the server is restarted. 然后,您需要确保每次重新启动服务器时都启动您的应用程序。 I recommend using Upstart if you can. 如果可以的话,我建议您使用Upstart。

Using Upstart, save something like this in /etc/init/YOURAPP.conf : 使用Upstart将类似的内容保存在/etc/init/YOURAPP.conf

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [06]

# If the process quits unexpectadly trigger a respawn
respawn

# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile --pidfile /www/YOURAPP/run/node-upstart.pid --exec /opt/node/bin/node -- /www/YOURAPP/app/app.js >> /www/YOURAPP/log/node-upstart.log 2>&1

Just change: 只是改变:

  • YOURAPP to the name of your own app YOURAPP为您自己的应用程序的名称
  • /opt/node/bin/node to your path to node /opt/node/bin/node到您的node路径
  • /www/YOURAPP/app/app.js to the path of your Node app /www/YOURAPP/app/app.js到您的Node应用程序的路径
  • /www/YOURAPP/run to where you want your PID file /www/YOURAPP/run至您想要PID文件的位置
  • /www/YOURAPP/log to where you want your logs /www/YOURAPP/log到您想要的日志位置
  • --chuid node to --chuid OTHERUSER if you want it to run as a different user than node 如果希望--chuid node以与node不同的用户身份运行, --chuid node改为--chuid OTHERUSER

(make sure to add a user with a name from --chuid above) (确保添加名称来自上述--chuid的用户)

With your /etc/init/YOURAPP.conf in place you can safely restart your server and have your app still running, you can run: 使用/etc/init/YOURAPP.conf ,您可以安全地重新启动服务器并使应用程序仍在运行,您可以运行:

start YOURAPP
restart YOURAPP
stop YOURAPP

to start, restart and stop your app - which would also happen automatically during the system boot or shutdown. 以启动,重新启动和停止您的应用-系统启动或关闭时也会自动发生。

For more info see those answers about: 有关更多信息,请参见以下答案:

You can also use systemd for that but there are some differences as the system is much more complicated and often leads to some problems . 您也可以为此使用systemd,但是存在一些差异,因为系统更加复杂并且经常导致一些问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM