2

我找到了一台旧 PC,我想将它用作专用的 Node.js 测试机。

基本上我想在win机器上编写我的应用程序,然后通过samba将它们复制到节点文件夹并通过ssh启动它们。稍后,我会添加一个新贵脚本并使用 samba 将其复制到服务器,这样当我重新启动时,应用程序每次都会自动启动。

  1. 为了在专用 Ubuntu 服务器上的网络上正确运行 Node.js 应用程序,我需要安装什么?这是我想出的清单,如果我错了,请纠正我。还有别的事吗?
    • SSH
    • samba (ftp 或 sftp 应该是要走的路,但由于它是一个封闭的内部网络,我必须从各种操作系统的 samba 访问它是不考虑安全问题的共享文件的最简单方法..大多数时候我使用简单的文本编辑)
    • “基本的ubuntu服务器”文件?
    • “灯” (?)
    • 节点.js
    • 节点包管理器。
  2. 如何在 Ubuntu 服务器上安装最新的 Node.js、npm 和 init 文件。我看到没有简单的sudo apt-get install nodejs npm.
  3. 我需要什么样的脚本来启动我的应用程序以及我将它们放在哪里(更喜欢本机脚本)?

编辑

经过一些测试,我现在处于一个好点,这就是我所做的:

  1. 我从最小的 CD 安装了 ubuntu
  2. 在选择软件包时,我只选择了 ssh 和 samba
  3. 更新系统
  4. 安装运行 node.js 所需的依赖项
  5. 从 git 安装最新的节点
  6. 在我的情况下设置 samba 我为脚本创建了文件夹 /var/nodejs
  7. 将您的 testApp.js 放在 nodejs 文件夹中
  8. 从 ssh 启动你的 testApp.js。*它不会工作

3-更新系统

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

4-依赖

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

5-安装节点

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

6 设置桑巴舞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

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-现在一切都应该运行......但是:

您只能以管理员身份运行 nodejs,在启动命令前附加“sudo”,否则作为普通用户您无权访问 1000 以下的大多数端口。

A. 如何在不使用 sudo 的情况下在端口 80 上启动我的应用程序?

显然,如果您使用command sudo node /var/nodejs/testApp.js 关闭终端启动应用程序,则应用程序将停止。

为此,我们使用初始化脚本。

经过一番阅读,我发现upstart本地安装在 ubuntu 服务器中,它可能是启动应用程序的最佳方式。

B. 我知道你需要用你的 appname 和 .conf 扩展名将脚本放入 /etc/init/ 中。但是它是如何工作的?

4

5 回答 5

13

我需要安装什么才能在专用 ubuntu 服务器上的网络上正确运行 node.js 应用程序?

你只需要安装nodejs。nodejs 可以在任何端口上运行,因此您不需要 Apache 或其他任何东西。

我如何在 ubuntu 服务器上安装最新的 nodejs、npm 和 init 文件

尝试按照本指南中概述的步骤操作:http: //howtonode.org/how-to-install-nodejs。使用 Ubuntu 的说明。

当我重新启动时,应用程序每次都会自动启动

一种方法是编写一个将在启动时运行的小脚本。该脚本将包含以下指令:

nodejs /path/to/app/app.js

查看有关如何在启动时运行脚本的 SO 答案:https ://stackoverflow.com/questions/3036/files-and-scripts-that-execute-on-boot

于 2013-06-14T16:17:59.360 回答
3

根据您的问题,您听起来和我一样懒惰和不耐烦,因此使用 PPA 而不是从源头构建。只需按照node.js ubuntu 指示

事实上我很懒惰,我拒绝输入端口号,因此我用 nginx 代理我的所有 node.js 应用程序。(这也是最好的方法,也是我能告诉的唯一方法让多台服务器在端口 80 上“监听”)。[Nginx 的安装指南。] 启动 nginx 后,请按照 Chris Lea 的指南http://wiki.nginx.org/Install)获取代理。

顺便说一句,如果您安装了 apache,请确保清除它sudo apt-get purge apache*。这很可能会破坏您的 php 应用程序,但这就是您运行 node 的原因吗?只需谷歌如何使用 nignx 运行 php。

现在是暴发户和监控。只需按照本指南注意:指南有错字,请仔细阅读评论。

至于 samaba,你自己在那里。

TL;博士

答案A:指导

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

编辑 1

Upstart 是 Ubuntu 用于启动后台进程的本机工具。在这里阅读所有相关信息。

#!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

现在使用以下命令启动和停止您的进程service

sudo service node-app start

如果需要,切换start停止或状态。

如果您不使用监视器,只需删除 pid 行。我真的建议使用 monit,因为您可以将其配置为在您的进程终止或日志文件中发生错误时向您发送电子邮件警报。

于 2013-07-09T22:01:26.380 回答
0

我会做什么:

  • 安装 ubuntu

  • 安装 apache 和 SVN,通过 http:// 访问 svn repos

  • 为每个项目创建一个 svn

  • 创建一个 svn 提交挂钩以将脚本自动部署到文件夹

/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


#!/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
  • 安装nodejs
  • 使用 npm install -g nodemon 安装 nodemon (我想不到 - 请查看手册)
  • 在 /etc/init 中为节点 js 脚本创建一个新贵文件

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

  • 现在您所要做的就是处理代码并提交

  • 当您提交代码并重新启动 nodejs 脚本时

  • 玩得开心编码!

我认为不需要 samba,除非您想通过 samba 在服务器上进行开发。如果您想这样做,只需跳过 svn 部分并安装 samba,但设置 nodemon upstart 脚本 - 它会为您节省很多麻烦的 os ssh-ing。

我以这种方式工作是因为它允许我在本地编写和测试,然后快速轻松地将代码提交到开发/测试/生产服务器。

要解决端口 80 问题,只需设置防火墙规则以将传入的 tcp/80 重定向到 tcp/8080(例如)并在该端口上侦听 nodejs 脚本。更多信息在这里:https ://serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user

ps 我没有为每个项目添加完整的分步说明,因为那里有很多指南会比我做得更好

于 2013-07-09T21:33:51.330 回答
0

您可以使用 forvere 模块在后台启动 nodejs 应用程序。有关完整信息,请参阅此http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever

我只是用 nodejs 运行一个 Ubuntu 专用服务器,它工作没有问题

于 2014-01-02T18:43:36.917 回答
0

在 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
于 2013-08-29T10:22:09.273 回答