简体   繁体   English

如何设置私有专用Node.js Ubuntu服务器?

[英]How to setup a private dedicated Node.js Ubuntu Server?

I found an old PC and i want to use it as a dedicated Node.js test machine. 我找到了一台旧PC,我想将它用作专用的Node.js测试机。

Basically i wanna write my apps on a win machine then copy them over samba to the node folder and launch them via ssh. 基本上我想在win机器上编写我的应用程序然后将它们通过samba复制到节点文件夹并通过ssh启动它们。 Later, I would add an upstart script and copy it with samba to the server so that when i reboot the app starts automatically every time. 稍后,我将添加一个upstart脚本并将其与samba一起复制到服务器,这样当我重新启动时,应用程序每次都会自动启动。

  1. What do I need to install in order to properly run Node.js apps on my network on a dedicated Ubuntu server? 为了在专用的Ubuntu服务器上正确运行网络上的Node.js应用程序,我需要安装什么? Here is the list I came up with, please correct me if I'm wrong. 这是我提出的清单,如果我错了请纠正我。 Is there anything else? 还有别的事吗?
    • ssh SSH
    • samba (ftp or sftp should be the way to go but as it's a closed internal network and i have to access it from various os's samba is the simplest way to share files not considering security issues..most of the time i use a simple text editor) samba(ftp或sftp应该是要走的路,但因为它是一个封闭的内部网络,我必须从各种os的samba访问它是共享文件的最简单的方法,不考虑安全问题..大多数时候我使用简单的文本编辑)
    • "basic ubuntu server" files? “基本的ubuntu服务器”文件?
    • "LAMP" (?) “灯”(?)
    • node.js 的node.js
    • node package manager. 节点包管理器。
  2. how do i install the latest Node.js, npm, and the init files on Ubuntu server. 如何在Ubuntu服务器上安装最新的Node.js,npm和init文件。 I saw that there was no simple sudo apt-get install nodejs npm . 我看到没有简单的sudo apt-get install nodejs npm
  3. What kind of script do I need to launch my apps and where do i put them (prefer native scripts)? 我需要什么样的脚本来启动我的应用程序以及我在哪里放置它们(更喜欢本机脚本)?

EDIT 编辑

After some testing i'm at a good point now, and here is what i did: 经过一些测试后,我现在处于一个好点,这就是我所做的:

  1. I installed ubuntu from a minimal CD 我用最小的CD安装了ubuntu
  2. when it comes to choose the packages i selected ONLY ssh & samba 当我选择仅选择ssh和samba的包时
  3. update the system 更新系统
  4. install the dependencies that u need to run node.js 安装运行node.js所需的依赖项
  5. install latest node from git 从git安装最新节点
  6. setup samba in my case i created the folder /var/nodejs for the scripts 在我的情况下设置samba我为脚本创建了文件夹/ var / nodejs
  7. put your testApp.js in the nodejs folder 将testApp.js放在nodejs文件夹中
  8. start your testApp.js from ssh. 从ssh启动testApp.js。 *it won't work *它不起作用

3-update the system 3 - 更新系统

sudo apt-get update && sudo apt-get upgrade

4-dependancies 4-依赖关系

sudo apt-get install g++ curl libssl-dev apache2-utils git-core make

5-install node 5安装节点

git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

6-setup samba sudo nano /etc/samba/smb.conf 6-setup samba sudo nano /etc/samba/smb.conf

[nodejs]
comment = nodejs
workgroup = WG
security = USER
path = /var/nodejs
server string =Node JS
browsable = yes
read only = no
writeable = yes
create mask = 0777

7-testApp.js 7- testApp.js

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node.js\n');
}).listen(80, "192.168.0.1");
console.log('Server running at http://192.168.0.1:80/');

8-Now everything should run...but: 8-现在一切都应该运行......但是:

You can run nodejs only as administrator appending "sudo" in front of the launch command else as a normal user u don't have access to most of the ports under 1000. 您可以仅在管理员在启动命令前添加“sudo”时运行nodejs,否则作为普通用户,您无法访问1000以下的大多数端口。

A. How can i lauch my app on port 80 without using sudo? A.如何在不使用sudo的情况下在端口80上启用我的应用程序?

And obviously if u launch you app with the command sudo node /var/nodejs/testApp.js if u close the terminal the app will stop. 显然,如果你使用command sudo node /var/nodejs/testApp.js启动app,如果你关闭终端,应用程序将停止。

For that we use a init script. 为此,我们使用init脚本。

After some reading i found that upstart is natively installed in ubuntu server and it's probably the best way to launch your apps. 经过一番阅读后,我发现upstart本身安装在ubuntu服务器中,这可能是启动应用程序的最佳方式。

B. I know u need to put the script into /etc/init/ with your appname and .conf extension.but how does that work? B.我知道你需要把你的appname和.conf扩展名放到/ etc / init /中。但是这怎么办?

what do i need to install to properly run node.js apps on my network on a dedicated ubuntu server? 我需要安装什么来在我的网络上在专用的ubuntu服务器上正确运行node.js应用程序?

You just need to install nodejs. 您只需要安装nodejs。 nodejs can run on any port, so you don't need Apache or anything else. nodejs可以在任何端口上运行,因此您不需要Apache或其他任何东西。

how do i install the latest nodejs,npm,and the init files on ubuntu server 如何在ubuntu服务器上安装最新的nodejs,npm和init文件

Try to follow the steps outlined in this guide: http://howtonode.org/how-to-install-nodejs . 请尝试按照本指南中列出的步骤操作: http//howtonode.org/how-to-install-nodejs Use the instructions for Ubuntu. 使用Ubuntu的说明。

when i reboot the app starts automatically every time 当我重新启动应用程序每次自动启动

One way to do this is to write a small script that will run on boot. 一种方法是编写一个将在引导时运行的小脚本。 The script would contain the instruction: 该脚本将包含以下指令:

nodejs /path/to/app/app.js

Check out this SO answer on how to run a script on boot: https://stackoverflow.com/questions/3036/files-and-scripts-that-execute-on-boot 查看有关如何在启动时运行脚本的SO答案: https//stackoverflow.com/questions/3036/files-and-scripts-that-execute-on-boot

By your question, you sound about as lazy and impatient as I am, therefore use PPAs instead of building from the source. 根据你的问题,你听起来像我一样懒惰和不耐烦,因此使用PPA而不是从源代码构建。 Just follow the node.js ubuntu directions . 只需按照node.js ubuntu方向操作即可

In-fact I'm so lazy, I refuse to type in port numbers, hence I proxy all my node.js applications with nginx. 实际上我很懒,我拒绝输入端口号,因此我用nginx代理我所有的node.js应用程序。 (This is also the best way, and only way I can tell to have multiple servers "listening" on port 80). (这也是最好的方法,也是唯一能让多个服务器在端口80上“监听”的方式。 [Nginx's install guide.] Once you get nginx up, follow Chris Lea's guide .( http://wiki.nginx.org/Install ) for the proxy. [Nginx的安装指南。]一旦你获得nginx,请按照Chris Lea的指南 。( http://wiki.nginx.org/Install )获取代理。

BTW if you installed apache, make sure you purge it sudo apt-get purge apache* . 顺便说一句,如果你安装了apache,请确保你清除它sudo apt-get purge apache* This will most likely break your php apps, but that's why you're running node right? 这很可能会打破你的php应用程序,但这就是你正在运行节点的原因吗? Just google how to run php with nignx. 只是google如何使用nignx运行php。

Now for upstart & monit. 现在为新贵和monit。 Just follow this guide . 请遵循本指南 NOTE: The guide has a typo so read the comments carefully. 注意:指南有拼写错误,请仔细阅读说明。

As for samaba, you're on your own there. 至于samaba,你自己就在那里。

TL;DR TL; DR

Answer A: guide 答案A: 指南

Answer B: sudo cp my-node-app.conf /etc/init; sudo service my-node-app start 答案B: sudo cp my-node-app.conf /etc/init; sudo service my-node-app start sudo cp my-node-app.conf /etc/init; sudo service my-node-app start

Edit 1 编辑1

Upstart is Ubuntu's native utiltiy for starting background processes. Upstart是Ubuntu用于启动后台进程的本机工具。 Read all about it here . 在这里阅读所有相关信息。

#!upstart
description "node-app"
author      "me"

env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

respawn
start on runlevel [23]

script
    #set enviroment vars here
    export NODE_ENV=production
    #Uncommit if you need a pid file for monit
    #echo $$ > /var/run/node-app.pid
    exec /usr/bin/node /path/to/app.js 2>&1 >> /path/to/log/file/app.log
end script

#Logs start and stop time timestamps to the file
pre-start script
  echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /path/to/log/file/app.log
end script

pre-stop script
  #Uncomment if you need a pid file for monit
  #rm /var/run/yourprogram.pid
  echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /path/to/log/file/app.log
end script

Now start and stop your process by using service : 现在使用service启动和停止您的流程:

sudo service node-app start

Switch start with stop or status if needed. 如果需要,切换start停止或状态。

If you are not using monit, just remove the pid lines. 如果您不使用monit,只需删除pid行。 I really recommend using monit because you can configure it to give you email alert if your process dies or an error occurs in the log file. 我真的建议使用monit,因为您可以将其配置为在您的进程终止或日志文件中发生错误时向您发送电子邮件警报。

What i would do: 我会做什么:

  • install ubuntu 安装ubuntu

  • install apache and SVN, with svn repos accessible via http:// 安装apache和SVN,通过http://访问svn repos

  • create a svn for each project 为每个项目创建一个svn

  • create a svn commit hook to autodeploy scripts to a folder 创建一个svn提交挂钩,以将脚本自动部署到文件夹

/svn//hooks/post-commit / SVN //钩/提交后


REPOS="$1"
REV="$2"

cd /var/target/path
svn cleanup /var/target/path/
svn checkout -q --force file:///svn/ /var/target/path
svn cleanup /var/target/path/
exit 0

/svn//hooks/post_commit.sh /svn//hooks/post_commit.sh


#!/bin/bash

REPOS="$1"
REV="$2"

# Files and directories can be distinguished, as directory paths are displayed with a trailing "/" character.

LOOK=/usr/bin/svnlook
SVN=/svn/
DEV=/var/target/path/

@mkdir /var/tmp/svn
cd /var/tmp/svn
  for changes in `$LOOK changed $REPOS | awk '{print $1 "=" $2;}'`;
  do
        len=${#changes}
        idx=`expr index "$changes" =`;
        directory=${changes:$idx};
        action=${changes:0:$idx-1};
        if [ ${changes:len-1} = '/' ]
        then
            case "$action" in
                "A" ) \
                    mkdir --mode=775 -p $DEV/$directory;
                    chown nobody:nobody $DEV/$directory;
                    chmod 775 $DEV/$directory;
                    ;;
                "D" ) \
                    rmdir $DEV/$directory;
                    ;;
            esac
        else
            case "$action" in
                "A"|"U"|"UU" ) \
                    $SVN export --force --non-interactive -r HEAD -q file://$REPOS/$directory;
                    BASE=`basename $directory`;
                    DIR=`dirname $directory`;
                    chown nobody:nobody $BASE;
                    chmod 775 $BASE;
                    mkdir --mode=775 -p $DEV/$DIR;
                    cp -f --preserve=ownership $BASE $DEV/$DIR;
                    unlink $BASE;
                    ;;
                "D" ) \
                    rm -f $DEV/$directory;
                    ;;
            esac
        fi
  done
echo Updated dev subdomain
exit 1
  • install nodejs 安装nodejs
  • install nodemon with npm install -g nodemon (out of my head - please check the manual) 使用npm install -g nodemon安装nodemon(我的头脑 - 请查看手册)
  • create an upstart file for the node js script in /etc/init 在/ etc / init中为节点js脚本创建一个upstart文件

description "nodejs with nodemonn"
author "etc"

start on startup

respawn

script
   cd /var/target/project/dir/
   exec nodemon /var/target/project/dir/main.js
end script

  • now all you have to do is work on the code and commit 现在你所要做的就是处理代码和提交

  • when you commit the code is updated and the nodejs script is restarted 当您提交代码时,更新代码并重新启动nodejs脚本

  • have fun coding ! 有乐趣编码!

I don't think samba is required, unless you want to develop on the server via samba. 我不认为samba是必需的,除非你想通过samba在服务器上开发。 If you want to do that just skip the svn part and install samba, but setup nodemon upstart scripts - it will save you alot of hassle os ssh-ing. 如果你想这样做只是跳过svn部分并安装samba,但是设置nodemon upstart脚本 - 它将为你节省很多麻烦的ssh-ing。

I work this way because it allows me to write and test locally and then comit code to dev/test/prod servers quick and easy. 我这样工作是因为它允许我在本地编写和测试,然后快速简便地将代码编写到dev / test / prod服务器。

To solve the port 80 issue, just setup a firewall rule to redirect incoming tcp/80 to tcp/8080 (for instance) and listen in your nodejs script on that port. 要解决端口80问题,只需设置防火墙规则以将传入的tcp / 80重定向到tcp / 8080(例如)并侦听该端口上的nodejs脚本。 More information here: https://serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user 更多信息请访问: https//serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user

ps i did not add a complete step-by-step instruction for each item because there are plenty of guides out there that would a much better job than i can ps我没有为每个项目添加完整的逐步说明,因为那里有很多指南比我能做得好得多

on Ubuntu: startup booting Nodejs + socket.io by ubuntu user: 在Ubuntu上:ubuntu用户启动Nodejs + socket.io启动:

more /etc/init/noded.conf

# Ubuntu upstart file at /etc/init/noded.conf

description "noded.conf" 

author      "Nguyen Thanh Binh"

start on runlevel [2345]

stop on runlevel [06]

respawn

script

su - ubuntu -c "NODE_ENV=test exec sudo /usr/bin/node /home/ubuntu/server.js" >> /home/ubuntu/log.log &


end script

You can use forvere module to start a nodejs application in the background. 您可以使用forvere模块在后台启动nodejs应用程序。 for full info see this http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever . 有关完整信息,请参阅http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever

and I am just running an Ubuntu dedicated server with nodejs and it's working without a problem 我正在运行一个带有nodejs的Ubuntu专用服务器,它的工作没有问题

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

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