meteor构建app程序_在Meteor.js中构建Slack克隆(第5部分):流星部署

meteor构建app程序

This is the fifth, and last, of a five-part series on building a Slack clone using Meteor. The aim of these tutorials are not just for you to blindly follow instructions, but it's our hope that you'll understand the thought process and reasoning behind the architecture.

这是关于使用Meteor构建Slack克隆的五部分系列的第五篇也是最后一部分。 这些教程的目的不仅是让您盲目遵循说明,而且我们希望您能理解架构背后的思考过程和推理。

So far, we have been working on our local machine, chatting to ourselves. We need to deploy it on a server so other people can chat with us! So in the last article of the series, we'll focus on how deploy our awesome application for the world to see.

到目前为止,我们一直在本地计算机上工作,与自己聊天。 我们需要将其部署在服务器上,以便其他人可以与我们聊天! 因此,在本系列的最后一篇文章中,我们将重点介绍如何部署令人敬畏的应用程序,以供全世界查看。

You can host your application on Meteor's own infrastructure using meteor deploy, or on your own infrastructure.

您可以使用meteor deploy将应用程序托管在Meteor自己的基础架构上,也可以托管在自己的基础架构上。

meteor deploy (meteor deploy)

If you've tried out our demo you'd have noticed the URL is something.meteor.com - on the meteor.com domain. This is because Meteor provides their own infrastructure to us for free, allowing us to deploy quickly without having the bother with setting up servers, configuring databases, configuring email settings etc.

如果你已经尝试了我们的演示中,你会注意到网址是something.meteor.com -在meteor.com域。 这是因为Meteor免费为我们提供了自己的基础架构,使我们能够快速部署,而不必费心设置服务器,配置数据库,配置电子邮件设置等。

$ meteor deploy d4nyll-slack.meteor.com
    Deploying to d4nyll-slack.meteor.com.         
    Now serving at http://d4nyll-slack.meteor.com

Deployment requires you to have a Developer account. If you don't have one, it will prompt you to create one. If you are not logged into the Meteor tool, login using meteor login.

部署需要您具有开发者帐户。 如果您没有,则将提示您创建一个。 如果您尚未登录Meteor工具,请使用meteor login

It'll take some time while Meteor minify and upload your files. But once it's done, you can now access it on http://d4nyll-slack.meteor.com

流星缩小并上传文件需要一些时间。 但是,一旦完成,您现在可以在http://d4nyll-slack.meteor.com上进行访问

Once you've deployed on that subdomain using your Developer Account, no one else can deploy to that domain.

使用您的开发者帐户在该子域上部署后,其他任何人都无法部署到该域。

$ meteor deploy scotch-slack1.meteor.com
    Sorry, that site belongs to a different user. 
    You are currently logged in as d4nyll.        

    Either have the site owner use 'meteor authorized --add' to add you as
    an authorized developer for the site, or switch to an authorized account
    with 'meteor login'.

To see if the name is taken, just go to the URL and if you see the following screen, you're in luck!

要查看该名称是否被使用,只需转到URL,然后看到以下屏幕,表示您很幸运!

Screen saying there is no site deployed at this address

自己的域名 (Own Domain)

Meteor also allows you to deploy the application on their server, but make it reachable through a custom domain. For example, if I want to have http://slack.danyll.com to house my Slack clone.

Meteor还允许您将应用程序部署在他们的服务器上,但是可以通过自定义域访问它。 例如,如果我想拥有http://slack.danyll.com来存放我的Slack克隆。

First, I'd need to go and edit my DNS settings so slack.danyll.com points to origin.meteor.com.

首先,我需要去编辑我的DNS设置,以便slack.danyll.com指向origin.meteor.com

I'm using DigitalOcean to host my sites, and they have a handy interface to change my DNS settings, so I will be using that. You should find something similar with your own hosting provider.

我正在使用DigitalOcean托管我的站点,并且它们具有方便的界面来更改我的DNS设置,因此我将使用它。 您应该找到与自己的托管服务提供商类似的东西。

cname-record

Notice the period (.) in the 'hostname'. That is very important - it signifies the root DNS server.

请注意“主机名”中的句点( . )。 这非常重要-它表示根DNS服务器。

After that's done, your zone file should have a new entry:

完成之后,您的区域文件应具有一个新条目:

slack.danyll.com. 1800 IN CNAME origin.meteor.com.

You might need to wait a short time before trying it out as the DNS settings need some time to propagate. So in the mean time, let's deploy our application on Meteor's infrastructure, but using our own domain name.

您可能需要等待一小段时间再尝试,因为DNS设置需要一些时间才能传播。 因此,与此同时,让我们使用自己的域名在Meteor的基础架构上部署应用程序。

$ meteor deploy slack.danyll.com
    Deploying to slack.danyll.com.                
    Now serving at http://slack.danyll.com

Fantastic! If you go to http://slack.danyll.com, you can actually see it being deployed.

太棒了! 如果您访问http://slack.danyll.com,则实际上可以看到它已被部署。

更新中 (Updating)

If you've made an update you can simply deploy it again by running the same command. The database will be kept.

如果进行了更新,则只需运行同一命令即可再次部署它。 数据库将被保留。

$ meteor deploy d4nyll-slack.meteor.com

To delete your application, just pass the --delete flag.

要删除您的应用程序,只需传递--delete标志。

$ meteor deploy --delete d4nyll-slack.meteor.com

meteor deploy is part of the meteor tool commands, make sure you have that installed and are logged in to your Developer Account

meteor deploymeteor工具命令的一部分,请确保已安装并登录到您的开发者帐户

开发与生产 (Development vs Production)

Run the application on your local environment and take a look at the one deployed. They look the same, but look under the hood we find a very different story!

在您的本地环境上运行该应用程序,然后看一下已部署的应用程序。 它们看起来一样,但是在幕后我们发现了一个截然不同的故事!

缩小和串联 (Minification and Concatenation)

Open up the developer's tools and see what's inside the <head>.

打开开发人员的工具,然后查看<head>

development-vs-production

In our localhost:3000 application, the <head> is filled with many files. In fact, all the files from pacakges and our own application can be found there individually. This is because we are in development mode.

在我们的localhost:3000应用程序中, <head>充满了许多文件。 实际上,可以分别在其中找到pacakges和我们自己的应用程序中的所有文件。 这是因为我们处于开发模式

When we deploy our application using meteor deploy, the environment is automatically set to production mode. One of the effects of this is that files are minified and concatenated together. So we get only one CSS stylesheet, and one JavaScript file, and both are minified.

当我们使用meteor deploy部署应用程序时,环境会自动设置为生产模式。 这样的影响之一是将文件缩小并串联在一起。 因此,我们仅获得一个CSS样式表和一个JavaScript文件,并且两者均已缩小。

When we are deploying the application on our own infrastructure, it's important we remember to put it in production mode.

当我们在自己的基础结构上部署应用程序时,记住将其置于生产模式很重要。

记忆 (Memory)

Background processes that enables features like Hot Code Push and resolving package dependencies takes up a lot of memory, and you don't need this in production. Running on development mode when the application is live can lead to Out-Of-Memeory (OOM) errors.

启用诸如热代码推送和解决程序包相关性之类的功能的后台进程会占用大量内存,而在生产中则不需要。 当应用程序处于活动状态时,在开发模式下运行可能会导致内存不足(OOM)错误。

自己的基础设施 (Own Infrastructure)

While Meteor provides free hosting, it's not recommened to use it for live applications, since we have no control over the server or infrastructure. It's there as a free service so there are no guarantees.

虽然Meteor提供免费托管,但不建议将其用于实时应用程序,因为我们无法控制服务器或基础架构。 它是一项免费服务,因此无法保证。

That's why for production applications, we'd need to deploy on your own servers.

因此,对于生产应用程序,我们需要在您自己的服务器上进行部署。

meteor build (meteor build)

The most no-nonsense way to deploy a Meteor application is using the meteor build command. This will build from the files in the application, bundle it into a tarball (.tar.gz), which you can extract from and run.

部署Meteor应用程序的最简单的方法是使用meteor build命令。 这将从应用程序中的文件构建,并将其捆绑到一个tarball( .tar.gz )中,您可以从中提取并运行它。

Let's do that now. Inside the root directory of your Meteor application, run:

现在开始吧。 在您的Meteor应用程序的根目录中,运行:

$ meteor build .

You can now find a file named src.tar.gz inside the root application directory. This is the file we'd need to upload to our remote machine. So the next step is to set up Virtual Private Hosting (VPS).

现在,您可以在根应用程序目录中找到一个名为src.tar.gz的文件。 这是我们需要上传到远程计算机的文件。 因此,下一步是设置虚拟专用主机(VPS)。

设置VPS (Setting Up VPS)

Setting up a server and securing it is an entire topic on its own, so we won't go too in-depth into it, but just enough so you can get it running.

设置服务器和保护服务器本身是一个完整的主题,因此我们不会对其进行太深入的研究,只是足够让您可以运行它。

Again, I will be using Digital Ocean, but you can use your own hosting provider. Just to level the playing field, there are other VPS providers out there - Linode, Amazon EC2, Modulus (which is a PaaS, like Heroku), and we will also take a look at Galaxy - "the managed cloud platform for deploying Meteor apps."

同样,我将使用Digital Ocean ,但是您可以使用自己的托管服务提供商。 只是为了公平起见,还有其他VPS提供商-LinodeAmazon EC2Modulus (这是PaaS ,例如Heroku),我们还将看看Galaxy-“用于部署Meteor应用程序的托管云平台”。

Let's create a new server (which are called 'Droplets' in Digital Ocean).

让我们创建一个新服务器(在Digital Ocean中称为“ Droplets”)。

new-droplet

We have included our SSH key in here so we can access it using SSH from my machine. If you don't have one, generate one now (press for everything)

我们在此处包含了SSH密钥,因此我们可以从我的计算机上使用SSH对其进行访问。 如果您没有,请立即生成一个(按表示所有内容)

$ ssh-keygen -t rsa

You can now find your key at ~/.ssh/id_rsa.pub. Copy it in its entirety into the box.

现在,您可以在~/.ssh/id_rsa.pub找到密钥。 将其完整复制到框中。

Click 'Create Droplet' to continue.

点击“创建液滴”继续。

server-details

From this screen, we know that our server can be reached via the IP address 128.199.170.132. Since it's our first time on this freshly installed server, we will ssh into it as root.

从这个屏幕上,我们知道可以通过IP地址128.199.170.132来访问我们的服务器。 由于这是我们第一次在此全新安装的服务器上运行,因此我们将以root用户身份ssh到该服务器。

$ ssh root@128.199.170.132
    The authenticity of host '128.199.170.132 (128.199.170.132)' can't be established.
    ECDSA key fingerprint is 96:46:56:e5:33:71:45:12:f3:bd:b1:0e:65:e8:fd:03.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '128.199.170.132' (ECDSA) to the list of known hosts.
    Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-52-generic x86_64)

     * Documentation:  https://help.ubuntu.com/

      System information as of Sun Jun  7 09:10:16 EDT 2015

      System load: 0.0               Memory usage: 9%   Processes:       51
      Usage of /:  7.5% of 19.56GB   Swap usage:   0%   Users logged in: 0

      Graph this data and manage this system at:

https://landscape.canonical.com/

    root@Production:~#
创建新用户 (Creating New User)

For security reasons, we don't use our root user. Instead we create a new user with root priviledges.

出于安全原因,我们不使用root用户。 相反,我们创建一个具有root特权的新用户。

# adduser d4nyll

After filling in some details about the user, assign it to the sudo group

填写有关用户的一些详细信息后,将其分配给sudo

# gpasswd -a d4nyll sudo
    Adding user d4nyll to group sudo

Now if we exit, we can login again as the user d4nyll.

现在,如果exit ,我们可以再次以d4nyll用户d4nyll登录。

$ ssh d4nyll@128.199.170.132

From here on, if you get an permission-related error while running the commands, try prepending the command with sudo, which will run the command as the root user.

从这里开始,如果在运行命令时遇到与权限相关的错误,请尝试在命令前加上sudo ,它将以root用户身份运行命令。

依存关系 (Dependencies)

We have our application code ready; we have our server set up. Next we need to deploy it onto our server. To follow along, you'd need to have to get access to a server. I will be using Ubuntu 14.04 in this tutorial.

我们已经准备好应用程序代码; 我们已经设置了服务器。 接下来,我们需要将其部署到我们的服务器上。 要继续进行下去,您需要访问服务器。 在本教程中,我将使用Ubuntu 14.04。

Ubuntu 15.04 was released but since "MongoDB only provides packages for long-term support Ubuntu releases", it's best we stick to 14.04.

Ubuntu 15.04已发布,但是由于“ MongoDB仅提供长期支持Ubuntu版本的软件包”,因此最好坚持使用14.04。

There are tools that we need to set up to serve our application. Here's the stack we will be using:

我们需要设置一些工具来服务于我们的应用程序。 这是我们将要使用的堆栈:

  • nginx as a reverse proxy web server which passes requests to our application

    nginx作为反向代理Web服务器,它将请求传递给我们的应用程序
  • mongodb

    mongodb
  • pm2 - A production process manager that keeps an eye on our application, and restart it if it crashes. There are alternatives - forever, supervisor and Upstart for Ubuntu, which you can explore in your own time.

    pm2生产过程管理器,负责监视我们的应用程序,如果崩溃,请重新启动它。 您可以在自己的时间内探索替代品-foreverforeversupervisor和Ubuntu的Upstart
安装依赖项 (Installing Dependencies)

Now let's install all the software we need. First, nginx

现在,让我们安装所需的所有软件。 首先, nginx

$ apt-get install nginx

Next up is Mongo:

接下来是Mongo:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
    $ echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
    $ sudo apt-get update
    $ sudo apt-get install -y mongodb-org

You can find more in-depth explanation of the purpose of each step on this official Guide.

您可以在本官方指南中找到有关每个步骤目的的更深入的说明。

Alternatively, you can use apt-get install mongodb-server to install MongoDB, but using the official releases are generally more up-to-date. You cannot run both the Ubuntu-package and the official versions concurrently, so I'd advice with sticking with the official version.

另外,您可以使用apt-get install mongodb-server来安装MongoDB,但是使用官方发行版通常是最新的。 您不能同时运行Ubuntu软件包和正式版本,因此建议您坚持使用正式版本。

And of course - Node

当然-节点

$ curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
    $ sudo apt-get install -y nodejs

关于反向代理的注意事项 (Note on Reverse Proxy)

We will be using nginx as a reverse proxy. This means our Meteor application will be running on port 3000, nginx will take in requests and pass it to the Meteor application; our application then replies with a response, which will be sent to the client through nginx.

我们将使用nginx作为反向代理 。 这意味着我们的Meteor应用程序将在端口3000上运行, nginx将接收请求并将其传递给Meteor应用程序; 然后,我们的应用程序将回复一个响应,该响应将通过nginx发送给客户端。

The reason why it's called a reverse proxy is because the client doesn't know that it's not in fact nginx that is providing the responde, but in fact our Meteor application. For all they know, nginx is serving the files to them; when in fact, all nginx is doing is relay the message.

之所以称为反向代理,是因为客户端不知道提供响应对象的实际上不是nginx ,而是我们的Meteor应用程序。 就他们所知, nginx正在将文件提供给他们。 实际上, nginx所做的只是中继消息。

Contrast this with a forward proxy, or what most of you will think of when the term proxy is used. In a forward proxy, the client access the server via another server, and the server has no idea who the final recipient is.

将此与正向代理进行对比,或者当您使用术语“ 代理 ”时,大多数人会想到什么。 在转发代理中,客户端通过另一台服务器访问服务器,而服务器不知道最终接收者是谁。

运行我们的应用程序 (Running Our Application)

MongoDB should already be running. If it's not run

MongoDB应该已经在运行。 如果没有运行

$ sudo service mongod start

$ sudo服务mongod启动

First, we need to upload our tarball onto the server, we will use good old sftp for this.

首先,我们需要将tarball上传到服务器,为此我们将使用良好的旧sftp

$ sftp -P 3825 d4nyll@128.199.170.132 
    Connected to 128.199.170.132.
    sftp> put src.tar.gz 
    Uploading src.tar.gz to /home/d4nyll/src.tar.gz
    src.tar.gz                             100% 2330KB 166.4KB/s   00:14

Then move the src.tar.gz file to the location where we put our application files. For me (and it is very much a personal preference), I put my files under /srv/www/.

然后将src.tar.gz文件移动到我们放置应用程序文件的位置。 对我来说(这是个人喜好),我将文件放在/srv/www/

$ mv src.tar.gz /srv/www/
    $ cd /srv/www/

Unpack the files:

解压缩文件:

$ tar -zvxf src.tar.gz
    $ ls -al
    total 2344
    drwxr-xr-x 3 root   root      4096 Jun  7 11:30 .
    drwxr-xr-x 3 root   root      4096 Jun  7 11:21 ..
    drwxr-xr-x 4 d4nyll d4nyll    4096 Jun  7 10:53 bundle
    -rw-rw-r-- 1 d4nyll d4nyll 2385695 Jun  7 11:24 src.tar.gz

We have generated a bundle directory containing all the files we need to run our application. Feel free to rename it however you want, especially when you have many applications on the same server. We can now delete our tarball and see what's inside the bundle.

我们已经生成了一个bundle目录,其中包含运行应用程序所需的所有文件。 可以随意重命名,尤其是在同一服务器上有多个应用程序时。 现在,我们可以删除tar包,查看其中的内容。

$ rm src.tar.gz
    $ cd bundle
    $ ls -al
    total 28
    drwxr-xr-x 4 d4nyll d4nyll 4096 Jun  7 10:53 .
    drwxr-xr-x 3 root   root   4096 Jun  7 11:34 ..
    -r--r--r-- 1 d4nyll d4nyll  485 Jun  7 10:53 main.js
    drwxr-xr-x 4 d4nyll d4nyll 4096 Jun  7 10:53 programs
    -r--r--r-- 1 d4nyll d4nyll  543 Jun  7 10:53 README
    drwxr-xr-x 2 d4nyll d4nyll 4096 Jun  7 10:53 server
    -r--r--r-- 1 d4nyll d4nyll  392 Jun  7 10:53 star.json

The important file here is the main.js file, which we can run using

这里的重要文件是main.js文件,我们可以使用它运行

$ node main.js

But when we do, it comes up with the following error.

但是,当我们这样做时,会出现以下错误。

module.js:338
        throw err;
              ^
    Error: Cannot find module 'fibers'
        at Function.Module._resolveFilename (module.js:336:15)
        at Function.Module._load (module.js:278:25)
        at Module.require (module.js:365:17)
        at require (module.js:384:17)
        at Object.<anonymous> (/srv/www/bundle/programs/server/boot.js:1:75)
        at Module._compile (module.js:460:26)
        at Object.Module._extensions..js (module.js:478:10)
        at Module.load (module.js:355:32)
        at Function.Module._load (module.js:310:12)
        at Module.require (module.js:365:17)

We actually need to go into the /programs/server/ and install the packages there. This requires g++ and make, which you can install along with other useful tools as part of the build-essential package.

实际上,我们需要进入/programs/server/并在其中安装软件包。 这需要g++make ,您可以将它们与其他有用的工具一起作为build-essential软件包的一部分安装。

$ cd programs/server/
    $ apt-get install build-essential
    $ npm install

Now if we run node main.js again, we get another error:

现在,如果再次运行node main.jsnode main.js收到另一个错误:

/srv/www/bundle/programs/server/node_modules/fibers/future.js:245
                            throw(ex);
                                  ^
    Error: Must pass options.rootUrl or set ROOT_URL in the server environment
        at Object.Meteor.absoluteUrl (packages/meteor/url_common.js:21:1)
        at Object.WebAppInternals.generateBoilerplate (packages/webapp/webapp_server.js:543:1)
        at Object.main (packages/webapp/webapp_server.js:739:1)
        at /srv/www/bundle/programs/server/boot.js:255:27
环境变量 (Environment Variables)

This is because there are environment variables we need to set up - namely ROOT_URL and MONGO_URL. (If you're using the email package, you'd also need to set up a MAIL_URL environment variable)

这是因为需要设置一些环境变量,即ROOT_URLMONGO_URL 。 (如果您使用的是email软件包,则还需要设置MAIL_URL环境变量)

ROOT_URL

ROOT_URL

The ROOT_URL environment variable is used by some packages, and should be set to the URL that the client will enter to reach your application. In our case, it would be http://128.199.170.132. If you have your own domain name, it'd be http://mydomain.com.

某些软件包使用ROOT_URL环境变量,并且应将其设置为客户端将输入以访问您的应用程序的URL。 在我们的情况下,它将是http://128.199.170.132 。 如果您拥有自己的域名,则为http://mydomain.com

MONGO_URL

MONGO_URL

The MONGO_URL environment variable is a little more self-explanatory, and needs to be the address for your MongoDB database.

MONGO_URL环境变量的MONGO_URL ,需要用作您的MongoDB数据库的地址。

Logged in as the user who will be running our Meteor application, edit / create a ~/.bash_profile file and export these environment variables:

以将要运行我们的Meteor应用程序的用户身份登录,编辑/创建~/.bash_profile文件并export以下环境变量:

~/.bash_profile

~/.bash_profile

export ROOT_URL=http://128.199.170.132
    export MONGO_URL=mongodb://localhost:27017/myapp

If you now run echo $ROOT_URL you won't see the newly exported variables because these settings weren't there when this terminal session started. To make these settings come into effect for the current session, run

如果现在运行echo $ROOT_URL ,则将看不到新导出的变量,因为在此终端会话启动时这些设置不存在。 要使这些设置对当前会话生效,请运行

$ source ~/.bash_profile

And now the environment variables are in effect.

现在环境变量生效了。

Now let's try running our application again:

现在,让我们尝试再次运行我们的应用程序:

$ node /srv/www/bundle/main.js

This time no errors.

这次没有错误。

保持应用程序运行 (Keeping Your Application Running)

As mentioned earlier, we will be using pm2 to monitor our application and keep it running. Let's install it globally (using the -g flag), run

如前所述,我们将使用pm2监视我们的应用程序并保持其运行。 让我们在全局安装它(使用-g标志),运行

$ npm install -g pm2

And now, instead of running node main.js, we simply run

现在,我们无需运行node main.js ,而只是运行

$ pm2 start main.js

Run pm2 list to see a list of applications, their uptime, and the number of times it's been restarted.

运行pm2 list以查看应用程序列表,它们的正常运行时间以及重新启动它的次数。

┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
    │ App name │ id │ mode │ pid  │ status │ restart │ uptime │ memory      │ watching │
    ├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
    │ main     │ 0  │ fork │ 8268 │ online │ 0       │ 8m     │ 48.844 MB   │ disabled │
    └──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘

You can also get the real-time status of the application, specifically the memory usage, by running pm2 monit.

您还可以通过运行pm2 monit来获取应用程序的实时状态,特别是内存使用情况。

With pm2, you'd need to environment variales inside a file instead. Convention calls it process.json.

使用pm2 ,您需要在文件内部环境变量。 习惯上称它为process.json

{
      "apps": [
        {
          "name": "app-name",
          "script": "./main.js",
          "log_date_format": "YYYY-MM-DD",
          "exec_mode": "fork_mode",
          "env": {
            "PORT": 3000,
            "MONGO_URL": "mongodb://127.0.0.1:27017/meteor",
            "ROOT_URL": "http://128.199.170.132/",
            "BIND_IP": "127.0.0.1"
          }
        }
      ]
    }

And then run pm2 start process.json

然后运行pm2 start process.json

保持pm2运行 (Keeping pm2 running)

pm2 can only keep our application running if it is itself running. We need to make sure pm2 is automatically restarted after a server restart. To do this, pm2 actually provides the pm2 startup command, which in turns output a command you need to run to set it up.

pm2仅在应用程序本身运行时才能保持运行。 我们需要确保pm2在服务器重启后自动重启。 为此, pm2实际上提供了pm2 startup命令,该命令反过来输出您需要运行以设置它的命令。

$ pm2 startup
    [PM2] Spawning PM2 daemon
    [PM2] PM2 Successfully daemonized
    [PM2] You have to run this command as root. Execute the following command:
          sudo env PATH=$PATH:/usr/bin pm2 startup linux -u d4nyll

    $ sudo env PATH=$PATH:/usr/bin pm2 startup linux -u d4nyll
    [PM2] Generating system init script in /etc/init.d/pm2-init.sh
    [PM2] Making script booting at startup...
    [PM2] -linux- Using the command:
          su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
     Adding system startup for /etc/init.d/pm2-init.sh ...
       /etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
    [PM2] Done.

And now pm2 and our Meteor application will be kept alive.

现在, pm2 我们的Meteor应用程序将保持活动状态。

Web服务器设置 (Web Server Set-up)

We have our application and it's running. The last thing we need to do is to point our web server at it. Remember we are using nginx as a reverse proxy, which means it's not server the files itself, but rather simply acting as a middle-man between the client and our Meteor application.

我们有我们的应用程序,并且正在运行。 我们需要做的最后一件事是将我们的Web服务器指向它。 请记住,我们将nginx用作反向代理,这意味着它不是服务器本身的文件,而只是充当客户端和流星应用程序之间的中间人。

We must configure nginx first.

我们必须先配置nginx

组态 (Configuration)

Configuration files for services on a Linux machine are usually stored inside the /etc/ directory; nginx is no exception.

Linux机器上服务的配置文件通常存储在/etc/目录中。 nginx也不例外。

If you are unfamiliar with conventional Ubuntu or Linux file directories, take at look at this blog post I wrote last year.

如果您不熟悉常规的Ubuntu或Linux文件目录,请看一下我去年写的这篇博客文章

$ cd /etc/nginx/
    $ ll
    total 72
    drwxr-xr-x  5 root root 4096 Jun  7 09:48 ./
    drwxr-xr-x 92 root root 4096 Jun  7 09:56 ../
    drwxr-xr-x  2 root root 4096 Feb 11 11:26 conf.d/
    -rw-r--r--  1 root root  911 Mar  4  2014 fastcgi_params
    -rw-r--r--  1 root root 2258 Mar  4  2014 koi-utf
    -rw-r--r--  1 root root 1805 Mar  4  2014 koi-win
    -rw-r--r--  1 root root 2085 Mar  4  2014 mime.types
    -rw-r--r--  1 root root 5287 Mar  4  2014 naxsi_core.rules
    -rw-r--r--  1 root root  287 Mar  4  2014 naxsi.rules
    -rw-r--r--  1 root root  222 Mar  4  2014 naxsi-ui.conf.1.4.1
    -rw-r--r--  1 root root 1601 Mar  4  2014 nginx.conf
    -rw-r--r--  1 root root  180 Mar  4  2014 proxy_params
    -rw-r--r--  1 root root  465 Mar  4  2014 scgi_params
    drwxr-xr-x  2 root root 4096 Jun  7 09:48 sites-available/
    drwxr-xr-x  2 root root 4096 Jun  7 09:48 sites-enabled/
    -rw-r--r--  1 root root  532 Mar  4  2014 uwsgi_params
    -rw-r--r--  1 root root 3071 Mar  4  2014 win-utf

nginx can serve multiple applications on the same server in different virtual hosts, these different applications can be reached via different domain names. So we should create a new virtual host for each application we are running on our server.

nginx可以在不同虚拟主机上的同一服务器上为多个应用程序提供服务,可以通过不同的域名访问这些不同的应用程序。 因此,我们应该为服务器上运行的每个应用程序创建一个新的虚拟主机。

Open up nginx.conf, which is the main configuration file for nginx. At the bottom of the http {} block, you will find these lines:

打开nginx.conf ,这是nginx的主要配置文件。 在http {}块的底部,您将找到以下几行:

##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

This means nginx will look inside these directories for configuration for each virtual host.

这意味着nginx将在这些目录中查找每个虚拟主机的配置。

We will use the /etc/nginx/sites-enabled/ to house all our virtual host configuration files.

我们将使用/etc/nginx/sites-enabled/来容纳我们所有的虚拟主机配置文件。

Actually, we will create all our virtual host configuration files inside the sites-available directory and then symbolic link them to the sites-enabled directory. This means we can have many configurations available, but only enable the onces we want to be public.

实际上,我们将在sites-available目录内创建所有虚拟主机配置文件,然后将它们符号链接到sites-enabledsites-enabled目录。 这意味着我们可以有许多可用的配置,但只能启用我们希望公开的配置。

$ cd /etc/nginx/sites-available
    $ ls -al
    total 12
    drwxr-xr-x 2 root root 4096 Jun  7 09:48 .
    drwxr-xr-x 5 root root 4096 Jun  7 09:48 ..
    -rw-r--r-- 1 root root 2593 Mar  4  2014 default

You'll find there is a default configuration file prepared for us already. Let's copy that and modify it.

您会发现已经为我们准备了一个default配置文件。 让我们复制并修改它。

$ cp default myapp

If we take away all the comments, we are left with this:

如果我们删除所有评论,则剩下的是:

server {
      listen 80 default_server;
      listen [::]:80 default_server;

      root /var/www/html;
      index index.html index.htm;

      server_name _;

      location / {
        try_files $uri $uri/ =404;
      }
    }

The listen lines tells us what port the server is listening out for. The default_server tells us this is the virtual host that requests will default to if the domain name doesn't match any defined in the server_name property in any configuration files.

listen行告诉我们服务器正在侦听哪个端口。 default_server告诉我们这是虚拟主机,如果域名与任何配置文件中的server_name属性中定义的任何域名都不匹配,则请求将默认为该虚拟主机。

The root tells nginx where to look for files, and index index.html index.htm defines which file it should look for if no filename is specified, and the location block simply say "Try to serve the requests as a file, then as a directory, and if none is found, return with status code 404"

root告诉nginx在哪里寻找文件,而index index.html index.htm定义了如果未指定文件名则应该寻找哪个文件,并且location块只是说“尝试将请求作为文件提供,然后作为目录,如果找不到,则返回状态码404

But that doesn't matter to us, since we don't want nginx to serve files, we want it to be a reverse proxy. So replace the configuration with below:

但这对我们来说并不重要,因为我们不希望nginx提供文件,所以我们希望它成为反向代理。 因此,将配置替换为以下内容:

server {
      listen 80 default_server;
      listen [::]:80 default_server;

      location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://127.0.0.1:3000/;
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }

At the moment, if you go to http://128.199.170.132, you'll see the NGINX Welcome page.

目前,如果您转到http://128.199.170.132,您将看到NGINX欢迎页面。

nginx-landing

That's because in our /etc/nginx/sites-available directory, the default configuration file is used. We need to remove that and symbolic link to our new configuration file.

这是因为在我们的/etc/nginx/sites-available目录中,使用了default配置文件。 我们需要删除指向我们新配置文件的符号链接。

$ cd /etc/nginx/sites-available
    $ rm default
    $ ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/myapp

Now, all we have to do is restart nginx and you will see our app running.

现在,我们要做的就是重启nginx ,您将看到我们的应用程序正在运行。

$ service nginx restart

Setting up your own server is error-prone, so if you run into any errors, test your nginx configuration file using nginx -t. It will help you determine whether the problem is with nginx or something else.

设置您自己的服务器容易出错,因此,如果遇到任何错误,请使用nginx -t测试您的nginx配置文件。 这将帮助您确定问题是否出在nginx或其他方面。

meteor-up (meteor-up)

In this article, we've dealt with deployment the hard way, because then you get to appreciate the process involved. But once you understand the process, using a tool like meteor-up will make your workflow much quicker.

在本文中,我们采用了艰苦的方式来处理部署,因为您将了解其中涉及的过程。 但是,一旦您了解了这一过程,就可以使用meteor-up的工具来加快工作流程。

This article is long enough already, so here's a quick overview:

本文已经足够长了,因此这里是一个快速概述:

Install meteor-up on your local / development environment.

在您的本地/开发环境上安装meteor-up

$ npm install -g mup

Inside the application directory, run

在应用程序目录中,运行

$ mup init

This creates two files - mup.json and settings.json.

这将创建两个文件mup.jsonsettings.json

Open mup.json and change the settings to fit your project and server configuration. Things are commented and quite self-explanatory. If you're using a fresh server, leave most of the settings alone and just update the servers, app and env blocks.

打开mup.json并更改设置以适合您的项目和服务器配置。 事物被评论并且很不言自明。 如果您使用的是新鲜的服务器,保留大部分设置单独的,只是更新serversappenv块。

"servers": [
        {
          "host": "hostname",
          "username": "d4nyll",
          "pem": "~/.ssh/id_rsa"
        }
      ],

I am using SSH to connect to the server here. With meteor-up, your private key cannot be password-protected.

我正在使用SSH连接到这里的服务器。 随着meteor-up ,您的私钥无法受到密码保护。

Set app to . because the mup.json file is already inside our application root directory. Set the env variables as described before.

app设置为. 因为mup.json文件已经在我们应用程序的根目录中。 设置env如前所述变量。

Lastly, run mup setup followed by mup deploy. To update your application, just run mup deploy again.

最后,运行mup setup然后运行mup deploy 。 要更新您的应用程序,只需再次运行mup deploy

星系 (Galaxy)

As with every open-source framework funded by investors, there will be a time when they roll out some commercial product to complement the open-source work.

就像每个由投资者资助的开源框架一样,有时他们会推出一些商业产品以补充开源工作。

Galaxy is Meteor's commercial product.

银河系流星的商业产品。

Modulus already provides a PaaS for Meteor, and has garnered some good reviews. But the MDG is coming up with the commercial platform which is tailored-made for Meteor alone.

Modulus已为流星提供PaaS ,并获得了一些好评。 但是MDG提出了仅针对Meteor量身定制商业平台。

You can keep up-to-date with updates on the Meteor Roadmap

您可以在流星路线图上保持最新

ngrok (ngrok)

If you don't want to actually deploy your application, but just want to provide your co-workers or client with a demo for a short period of time, then you don't need to take up a meteor.com subdomain, or bother with deployment at all. Instead, check out ngrok!

如果您不想实际部署应用程序,而只想在短时间内为您的同事或客户提供演示,则无需占用meteor.com子域,也meteor.com费心完全可以部署。 相反,请签出ngrok

ngrok basically opens up a tunnel from your machine and allow access through a subdomain of ngrok.io.

ngrok基本上从您的计算机打开一个隧道,并允许通过ngrok.io的子域进行ngrok.io

So all you have to do is run Meteor as normal (on port 3000), and run the command,

因此,您所要做的就是正常运行Meteor(在端口3000上),然后运行命令,

ngrok http -subdomain=d4nyll 3000

And now you can access your site through d4nyll.ngrok.io. Neat.

现在,您可以通过d4nyll.ngrok.io访问您的网站。 整齐。

结论 (Conclusion)

As you'd appreciate, deploying your Meteor application isn't so easy. Deployment and server configuration is an art of its own, and there are too many different distributions, variations and minor details for me to cover all of them. Tools like meteor-up deals with many of these complexities for you.

如您所愿,部署Meteor应用程序并非易事。 部署和服务器配置本身就是一门艺术,对我来说,太多的不同发行版,变体和次要细节无法涵盖所有​​内容。 诸如meteor-up工具可以为您解决许多复杂问题。

After you have your server set up, you'd still need to consider creating backups of the database, caching, logging, monitoring, load-balancing, seting up firewalls, security and encryption using SSL plus many more.

设置服务器后,您仍然需要考虑创建数据库备份,缓存,日志记录,监视,负载平衡,设置防火墙,使用SSL以及其他功能的安全性和加密。

This is just a very basic guide so you know all the components that are required to run a Meteor application. Now that you have an understanding of the process, you might understand better what is happening behind the scenes when using tools like meteor-up.

这只是一个非常基本的指南,因此您知道运行Meteor应用程序所需的所有组件。 既然您已经了解了该过程,那么在使用诸如meteor-up类的工具时,您可能会更好地了解幕后发生的事情。

翻译自: https://scotch.io/tutorials/building-a-slack-clone-in-meteor-js-part-5-deployment

meteor构建app程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值