安装服务器的nod​​e.js的Web服务应用程序 [英] Install a server for a node.js web service application

查看:335
本文介绍了安装服务器的nod​​e.js的Web服务应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了与node.js的,它允许通过REST API来获取一些数据上运行的应用程序。

I've built an application that run with node.js, which permit to retrieve some data through a REST API.

我希望把它放到网上个人计算机(Windows),但我不知道如何安装服务器,我需要让我的应用程序可在网上。什么

I want to put it online on a personal computer (Windows), but I have no idea how to install a server and what I need to make my application available online.

有人可以解释我的步骤做呢?我知道有一些在线服务存在像Heroku的,但我想自己做。

Can someone explain me the steps to do it ? I know that some online services exists like Heroku but I want to do it by myself.

感谢您

推荐答案

这个问题看起来很小,但它实际上是巨大的。我开始写这是一个基本的指导,它结束了真的是相当漫长的答案,所以我把它分解成碎片。总之,希望这有助于!

This question looks small, but it's actually huge. I started writing this as a basic guide, and it ended up really being quite a lengthy answer, so I split it into pieces. Overall hope this helps!

您不想从你的个人电脑成为一个网站,因为任何时候你的电脑处于关闭状态,该网站将下降。你不想要那种与您的计算机的责任,这么多的人选择从本质上是公司的唯一目的就是获得一个总是在简单的电脑你空间/带宽租用服务器空间的时间。这些通常被称为 VPS (虚拟专用服务器)。

You don't want to serve a website from your personal computer, because any time your computer is off, the website will be down. You don't want that kind of responsibility with your computer, so much of the time people choose to essentially rent server space from companies that's sole purpose is to get you space/bandwidth on a simple computer that is always on. These are often called VPS's (virtual private servers).

所以我建议的第一步就是抓住一个VPS自己。 数字海洋是一个伟大的服务,你可以从$ 5 /月获得了稳定的服务器,我会建议你从那里。还有其他一些公司,你可以得到VPS的距离,如果你preFER虽然一束束,可能是最流行alterntive是的Linode

So the first step I'd recommend is to grab a VPS for yourself. Digital Ocean is a great service that you can get a solid server from for $5/month, I would recommend starting there. There are bunches of other companies you can get VPS's from though if you prefer, probably the most popular alterntive being linode.

一旦你拥有属于自己的VPS,使用ssh登录到它。通常它会是这个样子:

Once you've got yourself a VPS, log in to it using ssh. Usually it will look something like this:

ssh root@000.000.0000

...在末尾数为您的服务器的IP地址。大多数VPS的是Linux的一些味道,所以是熟悉Linux命令行界面是非常重要的。一旦你都在你的服务器设置,你会想要做一些事情。这是我平时做,依次是:

...with the number at the end being the IP address of your server. Most VPS's are some flavor of linux, so being familiar with the linux command line interface is important. Once you're all set in your server, you'll want to do a few things. This is what I usually do, in order:

对于我来说,VIM是通过命令行来编辑文件最简单的方法。这当然可能不是每个人的情况 - 有些人preFER emacs的的,有些纳米,这是一个简单得多。如果您有兴趣了解vim的,有围绕净教程负荷。如果进入VIM是不是你的东西,我建议使用纳米,而不是无论我说出来,从这里开始。

For me, vim is the easiest way to edit files through the command line. This certainly might not be the case for everyone - some people prefer emacs, and some nano, which is a lot simpler. If you are interested in learning about vim, there are loads of tutorials around the 'net. If getting into vim isn't your thing, I'd recommend using nano instead wherever I mention it from here on.

要得到它安装,我们可以使用,这是天赋,在Ubuntu的软件包管理器,LINUX的味道,因为它是一个我将在这个答案使用流行的一种用于服务器,并且是数字海洋的默认。只要运行 apt-get的更新,以确保包是最新的,那么易于得到安装的vim 将在VIM。

To get it installed, we can use apt, which is aptitude, the package manager on ubuntu, the flavor of linux I'll use in this answer since it's a popular one for servers, and is the default for digital ocean. Just run apt-get update to make sure packages are up to date, then apt-get install vim to put in vim.

添加您的SSH密钥,以的〜/ .ssh / authorized_keys中,这样你就不需要密码即可登录。如果你不熟悉的ssh键的,他们基本都是一对,您可以使用,以避免需要每次都用密码授权加密密钥。通过添加你的公钥到的〜/ .ssh / authorized_keys中文件,你基本上告诉服务器:这是我的电脑,所以你不必问我密码登录。 Github上对如何生成密钥的很大的指导。一旦做到这一点,你可以用vim打开文件,进入插入模式,并粘贴在从本地计算机的公钥。保存并退出,你设置。

Add your ssh key to ~/.ssh/authorized_keys so that you don't need a password to log in. If you are unfamiliar with ssh keys, they are basically a pair of cryptographic keys that you can use to avoid needing to authorize with a password every time. By adding your public key to the ~/.ssh/authorized_keys file, you are essentially telling the server "this is my computer, so you don't need to ask me for a password to log in". Github has a great guide on how to generate keys. Once this is done, you can open the file with vim, get into insert mode, and paste the public key in from your local machine. Save and quit and you're set.

如果你想运行节点的应用程序,你当然需要有节点!在Linux上安装节点是有点不同,因为节点安装我敢肯定,你在本地使用的是图形化的,在这里你只需要在命令行中。幸运的是,这不是更加困难<一个href=\"https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager#ubuntu-mint-elementary-os\">this指令集中,你可以照做。请确保你不只是做默认的apt-get安装的NodeJS ,因为这将安装旧版本。就拿几个步骤第二款后增加PPA,并得到一个新的版本。

If you are trying to run a node app, you will of course need to have node! Installing node on linux is a bit different because the node installer I'm sure you used locally is graphical, where here you only have the command line. Luckily, it's not much more difficult with this set of instructions, which you can follow exactly. Make sure you do not just do the default apt-get install nodejs, as this will install an old version. Take the couple steps after the second paragraph to add ppa and get a newer version.

好了,你有一个具有节点,理论上可以运行你的应用程序的机器。这是一个好消息。现在,我们需要真正得到应用在机器上。还有,你可以做到这一点的一些方法。如果你已经红宝石本地安装,您可以使用 Capistrano的,一个流行的部署解决方案。轻量化的方法,我常常preFER是部署的,虽然我不认为这会在Windows上运行。您也可以只使用github上或者到位桶 - 把你的应用程序来远程回购,然后从你的VPS克隆下来(确保的apt-get安装的git 并设置你的用户名第一 - 如果它是一个私人回购你可能已经产生,并添加一键搞定访问把它拉下来)。然而,你能做到它,得到传输的文件。

Ok, so you have a machine that has node and theoretically could run your app. This is good news. Now we need to actually get the app onto the machine. There are a few ways you can do this. If you have ruby installed locally, you can use capistrano, a popular deployment solution. A lighter weight approach that I often prefer is deploy, although I don't think that will work on windows. You can also just use github or bitbucket - push your app to a remote repo then clone it down from your VPS (make sure to apt-get install git and set up your username first - if it's a private repo you'll probably have generate and add a key to get access to pull it down). However you manage to do it, get the files transferred.

在您的VPS, CD 到哪里你的应用程序被投入并运行它。确保一切工作正常,并创下的http:// YOUR_IP:PORT ,只是你的IP地址后,你的应用是在冒号后运行的端口号。你应该能够看到你的应用程序。如果不检查回到码头,它可能已经坠毁。有时你会发现吸虫当你设置它在不同的系统。如果应用程序使用的数据库,你可能需要得到这个配置了。你可以谷歌的ubuntu安装数据库名,并找到一些教程 - 数字海洋有这些类型的教程自己的pretty固体库

On your VPS, cd into wherever your app was put and run it. Make sure everything is working ok, and hit http://YOUR_IP:PORT, just your ip address followed by a port number that your app is running on after the colon. You should be able to see your app. If not check back to the terminal, it may have crashed. Sometimes you can find flukes when you are setting it up on a different system. If your app uses a database, you might need to get this configured too. You can google "ubuntu setup database name" and find some tutorials -- digital ocean has a pretty solid library of these types of tutorials themselves.

Nginx的是服务于一台机器上多个应用程序,并处理域名和这样一个伟大的方式。我写关于如何建立一个nginx的文章,你可以检查出学习的基础知识,并得到它安装。一旦做到这一点,你可以连接你的应用程序使用 proxy_pass 。而不是 try_files ,这是文章做什么服务器静态文件,只是在一个 proxy_pass 语句删除端口您的应用程序,而不是运行,并会nginx的指挥交通的权利,通过你的应用程序。下面是一个例子,如果你有你的应用程序运行在端口 1234 键,您的域名是 example.com

Nginx is a great way to serve multiple apps on one machine, and to handle domain names and such. I wrote an article on how to set up nginx that you can check out to learn the basics and get it installed. Once this is done, you can link up your app with a proxy_pass. Rather than try_files, which is what the article does to server static files, just drop in a proxy_pass statement to the port your app is running on instead, and nginx will direct traffic right through to your app. Here's an example, if you had your app running on port 1234 and your domain name was example.com

server {
  server_name example.com;

  location / {
    proxy_pass http://localhost:1234;
  }
}

这只是采取交通进入箱子从 example.com ,并把它传递给你的应用程序,这是真棒。

This will just take traffic coming into the box from example.com and pass it to your app, which is awesome.

我要假设你不想要求人们使用一个IP地址来访问你的应用程序,你想一个域名。去抓住一个来自哪里,一旦你有这个需要编辑的DNS记录。我发现它是最容易使用 dnsimple 这一点,因为并非每个域名注册商拥有雄厚的DNS记录的处理,你可以将所有DNS管理在一个地方。现在,只需把你的域的根A记录,将其指向你的VPS的IP地址。给它一个几分钟的记录propigate后,一炮到该域应该直接去你的服务器 - 梦幻

I have to assume you don't want to require people to use an IP address to access your app, and you want a domain name. Go grab one from wherever, and once you have this you need to edit the DNS records. I've found that it's easiest to use dnsimple for this, as not every domain registrar has solid dns record handling, and you can keep all your dns management in one place. Now, just put an A record on the root of your domain, pointing it to your VPS's IP address. After giving it a couple minutes for the records to propigate, a hit to that domain should go directly to your server - fantastic.

现在是检查通过,并确保您的应用程序运行正常,并且您的nginx的配置是正确的(并且已重新加载nginx的)的时间。请确保您的配置,服务器名镜像设置你的VPS指向域。确保端口在 proxy_pass 是一样的您的应用程序上运行。一旦这个已经证实,转到域,如果你这样做是正确的,你的应用程序就上来了。喔!

Now is the time to check through and make sure that your app is running properly and that your nginx configuration is correct (and that you have reloaded nginx). Make sure that in your configuration, the server_name mirrors the domain you set to point at your VPS. Make sure the port in the proxy_pass is the same as your app is running on. Once this has been confirmed, go to the domain, and if you did it right, your app will come up. Whoo!

大,所以我们得到了我们的应用程序运行,它是在线在互联网上,供市民欣赏。只是时间坐下来,让大家投钱你,一个普遍的现象,每当你得到一个发货的网站。但不要放倒太快,因为我们需要的最后一件事是确保这个应用程序保持并继续运行,即使出了问题,或者您登出VPS的,所以你并不总是必须保持终端窗口打开运行的应用程序。对于这一点,我们可以用一些人所说的生产服务器 - 专做服务器,以确保您的应用程序在后台运行,并保持运行所有的时间。幸运的是,节点有一些这些开源的,我最喜欢的是 PM2 。看看这个页面,阅读入门指导,您的计算机上安装 PM2 ,并运行你的应用程序。这个过程可能会是这样的:

Great, so we got our app running and it's online on the internet for the public to enjoy. Just about time to sit back and let everyone throw money at you, a common occurance whenever you get a site shipped. But don't recline too quickly, because the last thing we need is to make sure this app stays up and continues running even if something goes wrong, or you log out of your VPS, so you don't always have to keep a terminal window open running the app. For this, we can use what some call production servers -- servers made specifically to ensure that your app runs in the background and stays running all the time. Luckily, node has a few of these open source, my favorite being pm2. Check out this page, read the getting started instructions, install pm2 on your machine, and run your app. The process might look something like this:

npm install pm2 -g
cd path_to_my_app
pm2 start app.js

因为我们是把它同一个端口上,你的nginx的配置应该保持不变,并且您的应用程序仍然应该,如果你访问该域名。

Since you ran it on the same port, your nginx configuration should remain the same, and your app should still be up if you visit the domain.

唷,这是一个漫长的过程。比你预期的可能更多 - 为什么像Heroku的存在是有道理的。所以这真的是值得的,运行和维护自己的网站?我要说的是,我主持的网站和应用我运行这样的每一个。这里的原因:

Phew, that was a lengthy process. Probably more than you expected - makes sense why something like heroku exists. So is this really worth it, running and maintaining the site yourself? I'd argue yes, and I host every one of the sites and apps I run like this. Here's why:


  • 学习:我了解事物如何运作这种方式,在sysops得到更好吨

  • 成本:您可以承载像一个单个$ 5个数字海洋盒20个站点。托管是便士。

  • 控制:Heroku的有时会出现故障,它吮吸,因为所有你能做的就是等待他们找回来了。如果我的网站出现故障,这是我的错,我可以找出原因并解决它。

  • learning: I learn tons about how things work this way, and get much better at sysops.
  • cost: You can host like 20 sites on a single $5 digital ocean box. hosting is pennies.
  • control: Heroku sometimes goes down and it sucks because all you can do is wait for them to get it back up. If my site goes down, it's my fault and I can find out why and fix it.

我敢肯定,这答案是比你以往预期到这里,但希望这有助于!从开发前往sysops是一个旅程,有时可以得到很无奈,但我保证,一旦你对事情的好手感,它感觉非常好,确实有助于你的技能很多。

I'm sure this answer was more than you ever expected to get here, but hope this helps! Getting from dev to sysops is a journey and sometimes can get really frustrating, but I promise once you have a good handle on things, it feel great and really helps your skills a lot.

最后,我要指出,这是毫无疑问的自以为是的指南。有吨的其他工具等方式来做这些事情 - 工作流程,我这里只是我preFER做事情的方式。通过一切手段随意鼓捣和适应工作流,以您的需求,一旦你拥有了它你的腰带!也有很多的,可以在这里添加有关设置不同的数据库,提高你的部署/重新流动,多一点throughly保护您的框等细节。很想听到任何反馈,如果您或其他人有兴趣增加任何这些作品。

Finally, I want to note that this is without a doubt an opinionated guide. There are tons of other tools and other ways to do these things -- the workflow I have here is just the way I prefer to do things. By all means feel free to tinker and suit the workflow to your needs once you have it under your belt! There are also lots of other details that could be added in here about setting up different databases, improving your deploy/restart flow, and securing your box a little more throughly. Would love to hear any feedback and add any of these pieces in if you or others are interested.

这篇关于安装服务器的nod​​e.js的Web服务应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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