nl-mean程序下载_将MEAN应用程序部署到Amazon EC2(第2部分)

nl-mean程序下载

介绍 (Introduction)

Welcome back. In Part I of our tutorial, we took steps to deploy a MEAN application to EC2. In the process, we created a running EC2 instance, configured our security settings, made a connection, and set our application to run.

欢迎回来。 在本教程的第一部分中,我们采取了将MEAN应用程序部署到EC2的步骤。 在此过程中,我们创建了一个正在运行的EC2实例,配置了安全设置,建立了连接,并将应​​用程序设置为运行。

In this part of the tutorial, we'll be modifying our MEAN application to utilize an instance of MongoDB running on our EC2 server.

在本部分的教程中,我们将修改MEAN应用程序,以利用在EC2服务器上运行的MongoDB实例。

我们为什么要这样做? (Why should we do this?)

Live To-Do App

As we discussed at the conclusion of Part I, our current To-Do application is relying on an external instance of MongoDB. Specifically, we're employing a Database-as-a-service (DaaS) offered by Modulus.io to provide a complete database that our node application can connect to.

正如我们在第一部分结尾所讨论的那样,我们当前的To-Do应用程序依赖于MongoDB的外部实例。 具体来说,我们正在使用Modulus.io提供的数据库即服务(DaaS),以提供我们的节点应用程序可以连接到的完整数据库。

DaaS's (like Modulus.io, MongoLab, and Compose.io) offer a convenient way to use MongoDB without having to handle installation and troubleshooting. As an added advantage, they often provide friendly GUIs for administering and querying their databases.

DaaS(如Modulus.io, MongoLabCompose.io )提供了使用MongoDB的便捷方法,而无需处理安装和故障排除。 另外一个好处是,它们通常提供友好的GUI来管理和查询其数据库。

DaaS

That said, these services come with tradeoffs.

也就是说,这些服务需要权衡。

For one, their monthly cost tends to be significantly higher than if you setup MongoDB yourself on your own server. As an example, an M1 Cluster with 40 GB of storage would cost you about $180 per month using MongoLab. In contrast, a similar MongoDB instance in EC2 would cost only $36 per month if you set it up yourself.

一方面,与您自己在服务器上设置MongoDB相比,它们的每月费用往往会高得多。 例如,使用MongoLab,一个拥有40 GB存储空间的M1集群每月将花费您大约180美元 。 相反,如果您自己进行设置,则EC2中类似的MongoDB实例每月只需花费$ 36

Additionally, using DaaS's often limit your ability to fully customize all aspects of your database. (If, for instance, you were creating a healthcare application and needed a HIPPA compliant system, a ready-made solution typically wouldn't make the cut.)

此外,使用DaaS常常会限制您完全自定义数据库各个方面的能力。 (例如,如果您正在创建医疗保健应用程序并且需要符合HIPPA的系统,那么现成的解决方案通常不会成功 。)

Just consider the pros and cons. But for today's tutorial, we're going to ditch the DaaS in favor of our own EC2 instance of MongoDB.

只考虑利弊。 但是在今天的教程中,我们将放弃DaaS,转而使用我们自己的MongoDB EC2实例。

回到业务 (Getting Back to Business)

Because this tutorial is focused on deployment, you will need to have completed Part I in order to follow along. Take a look and come back when you're done. For those returning, make sure you have your passwords and account details handy.

由于本教程的重点是部署,因此您需要完成第I部分才能继续学习。 看一看,等完成后再回来。 对于那些返回的人,请确保您拥有方便的密码和帐户详细信息。

Once you're ready, let's connect to our EC2 instance again using PuTTY.

准备就绪后,让我们再次使用PuTTY连接到我们的EC2实例。

PuTTY

When prompted, enter: bitnami to login.

出现提示时,输入: bitnami登录。

Login Bitnami

登录到MongoDB (Logging into MongoDB)

To begin working with Mongo, we'll need to login as an administrator.

要开始使用Mongo,我们需要以管理员身份登录。

Here, we'll be making use of the green password we obtained in Part I. (Remember, that password is only displayed once. If you failed to record it earlier, you'll need to create a new EC2 instance and repeat Part I again. Sorry!).

在这里,我们将使用在第I部分中获得的绿色密码。(请记住,该密码仅显示一次。如果您之前无法记录该密码,则需要创建一个新的EC2实例并重复第I部分。再次抱歉!)。

ExamplePassword

While in the EC2 terminal, use mongo admin to enter MongoDB. (p.s. Remember to include sudo or you may go crazy wondering why MongoDB refuses to permit you):

在EC2终端中,使用mongo admin输入MongoDB。 (ps请记住包括sudo否则您可能会感到奇怪,为什么MongoDB拒绝允许您这样做):

sudo mongo admin --username root --password {YOUR GREEN PASSWORD}

Once you've logged in, the first action we'll take is to create a new user with administrative privileges. We'll create this account, so you have access to a username and password you can actually remember.

登录后,我们将要执行的第一步是创建一个具有管理特权的新用户。 我们将创建此帐户,以便您可以访问实际上可以记住的用户名和密码。

> show dbs 
> use admin 
> db.createUser 
> db.createUser(
    {
      user: "YOUR USERNAME",
      pwd: "YOUR PASSWORD",
      roles: [ "root" ]
    }
)

If all goes well, you should be notified that the user was successfully added.

如果一切顺利,应该通知您该用户已成功添加。

Now, log out of mongodb by entering ctrl+c and try logging back in using your new admin username and password.

现在,通过输入ctrl+c注销mongodb,然后尝试使用新的管理员用户名和密码重新登录。

sudo mongo admin --username {YOUR USERNAME} --password {YOUR PASSWORD}

Login Successful

Confirm that your account has been granted complete privileges by querying the databases and adding in a dummy record. Here we'll add a record: "This is a test" into the "test" collection of "dummyDB".

通过查询数据库并添加虚拟记录来确认您的帐户已被授予完全特权。 在这里,我们将添加一条记录:“这是一个测试”到“ dummyDB”的“测试”集合中。

> show dbs 
> use dummyDB 
> db.test.insert({ item: "This is a test"})

If we query for the record we should see it appear.

如果我们查询记录,我们应该看到它出现。

> db.test.find().pretty()

Queried Record

Perfect. There's our dummy record -- proving that our admin account is fully functional. Now its time to connect our app.

完善。 有我们的虚拟记录-证明我们的管理员帐户可以正常运行。 现在是时候连接我们的应用程序了。

启用访问 (Enabling Access)

Before we can copy and paste our MongoDB URL into our MEAN application, we need to make sure that its fully accessible. This means two things. First, it means ensuring the MongoDB port is accessible to the outside world. And secondly, it means configuring MongoDB so it knows which IP addresses to allow database access.

在将MongoDB URL复制并粘贴到MEAN应用程序之前,我们需要确保其完全可访问。 这意味着两件事。 首先,这意味着确保外界可以访问MongoDB端口。 其次,这意味着配置MongoDB,以便它知道允许数据库访问的IP地址。

We aren't going to dive too deeply into "ideal" security settings in this tutorial. How loosely or how tightly you need your database access to be is dependent on your specific application. And since we're just deploying a simple demo, we're going to keep things simple.

在本教程中,我们不会深入探讨“理想”的安全设置。 需要数据库访问的宽松程度或紧密程度取决于特定的应用程序。 由于我们只是部署了一个简单的演示,因此我们将使事情保持简单。

So let's begin. For a first step, let's make sure the MongoDB port is accessible. We cheated and already did this in Part I, but I think its worth re-examining. By default, the port used by MongoDB is 27017. (The Bitnami image we used in Part I also uses this default as well.)

因此,让我们开始吧。 首先,请确保可访问MongoDB端口。 我们作弊并且已经在第一部分中做了,但是我认为值得重新研究。 默认情况下,MongoDB使用的端口是27017。(我们在第一部分中使用的Bitnami映像也使用了该默认值。)

Now, open your Amazon EC2 instance in the Amazon Web Console and head to the Security Groups section. If you completed the steps in Part I correctly, you should see that we have a Custom TCP Rule which allows inbound access to port 27017 for all IP addresses (0.0.0.0).

现在,在Amazon Web Console中打开您的Amazon EC2实例,然后转到“安全组”部分。 如果您正确完成了第一部分中的步骤,则应该看到我们有一个自定义TCP规则,该规则允许对所有IP地址( 0.0.0.0 )的端口27017进行入站访问。

AWS Security Groups

Great! Step 1 complete.

大! 步骤1完成。

Now, we need to ensure that all IP addresses can run commands in MongoDB itself. This allowance is helpful especially during development, in which, many developers may need to access the database from multiple machines. To do this, open up your PuTTY window again and cd back into the main directory. From here, head into Stack -> MongoDB.

现在,我们需要确保所有IP地址都可以在MongoDB本身中运行命令。 此津贴特别有用,在开发过程中,其中许多开发人员可能需要从多台计算机访问数据库。 为此,请再次打开您的PuTTY窗口,然后将其cd返回主目录。 从这里进入Stack -> MongoDB

cd 
cd stack
ls
cd mongodb
ls

MongoDB Directory

If you look into this directory, you'll see a number of files and folders associated with MongoDB. In our case, we will be opening the mongodb.conf file and making a few edits using the vi editor.

如果查看此目录,将看到许多与MongoDB关联的文件和文件夹。 在本例中,我们将打开mongodb.conf文件,并使用vi编辑器进行一些编辑。

sudo vi mongodb.conf

Once you've opened the file, you'll see a list of config settings. Notice, for instance, where it specifies the port is 27017.

打开文件后,您将看到配置设置列表。 注意,例如,它指定的端口是27017

In our case, we'll actually be changing the bind_ip value to equal 0.0.0.0. This will allow all IP addresses access to our MongoDB instance. While still in the editor, type i to enter insert mode. Change the value of the bind_ip. Then type :w to save and :q to exit the editor.

在本例中,我们实际上将将bind_ip值更改为等于0.0.0.0 。 这将允许所有IP地址访问我们的MongoDB实例。 仍在编辑器中时,键入i进入插入模式。 更改bind_ip的值。 然后输入:w保存,并输入:q退出编辑器。

This is how your config file would have looked before...

这就是您的配置文件之前的样子...

Bind IP Before Change

And after.

之后。

Bind IP After Change

Once complete, navigate up one level back to the stack folder. Then run the bash script in ctlscript.sh to restart mongodb. This will ensure your changes to the MongoDB have taken effect.

完成后,向上一级导航到stack文件夹。 然后在ctlscript.sh运行bash脚本以重新启动mongodb。 这将确保您对MongoDB所做的更改已生效。

cd ..
ls
sudo bash ctlscript.sh restart mongodb

Restarting MongoDB

At this point, our MongoDB database is ready for use by our MEAN application. But, unfortunately, we have a few more steps to get things working for remote development and testing.

至此,我们的MEAN应用程序已准备好使用我们的MongoDB数据库。 但是 ,不幸的是,我们还有其他一些步骤可以使事情用于远程开发和测试。

MongoDB 3.0的必要解决方法 (Necessary Workaround for MongoDB 3.0)

To describe why this next step is necessary, open up a local terminal and try to access your EC2 MongoDB database instance. Use the below command as a guide.

为了说明为什么需要执行此下一步,请打开本地终端并尝试访问您的EC2 MongoDB数据库实例。 使用以下命令作为指导。

mongo {EC2 DOMAIN}/admin -u  {YOUR USERNAME} -p {YOUR PASSWORD}

At this point, what you'll likely see is the aggravating response: Error: 18... "auth failed". Try as you might in repeating your password or creating new users -- this error won't go away.

在这一点上,您可能会看到严重的响应: Error: 18... "auth failed" 。 您可以尝试重复输入密码或创建新用户-该错误不会消失。

Auth Failed

The reason for this error has less to do with your faulty memory and more to do with the recent upgrade to MongoDB. With version 3.0, came a new authentication mechanism for MongoDB. In Mongo 2.0, a database could be accessed via a remote shell or terminal so long as an admin username and password was supplied. This was convenient for upstart developers trying to quickly set things up. With the latest change, the authentication mechanism requires a bit more work as many third-party tools (including Bitnami's default image) have yet to switch to the new drivers.

发生此错误的原因与内存错误有关,而与最近升级到MongoDB有关。 在3.0版中,出现了针对MongoDB的新身份验证机制 。 在Mongo 2.0中,只要提供了管理员用户名和密码,就可以通过远程Shell或终端访问数据库。 这对于试图快速进行设置的新贵开发人员很方便。 随着最新的变化,身份验证机制需要更多的工作,因为许多第三方工具(包括Bitnami的默认映像)尚未切换到新的驱动程序。

Fortunately, the MongoDB team has a relatively simple workaround, in which we will revert our MongoDB setting to the old authentication mechanism.

幸运的是,MongoDB团队有一个相对简单的解决方法 ,在该方法中,我们将把我们的MongoDB设置还原为旧的身份验证机制。

To do this, first open up your PuTTY instance to EC2 and navigate to the mongodb.conf file again.

为此,首先将PuTTY实例打开到EC2,然后再次导航到mongodb.conf文件。

cd stack
cd mongodb
ls
sudo vi mongodb.conf

Now, look for where it says: auth = true. Temporarily, comment this value out, and uncomment the parameter that says noauth = true. Save your changes (:w) and exit the vi editor (:q). Your config file should now look like the image below.

现在,查找其内容: auth = true 。 暂时将此值注释掉,然后取消注释表示noauth = true的参数。 保存更改( :w )并退出vi编辑器( :q )。 您的配置文件现在应如下图所示。

NoAuth=true

With this change to config settings, we could actually access MongoDB remotely without even having to authenticate. But that's clearly not a solution we want. Instead, we are going to create a new administrator that utilizes the old-form of MongoDB credentialing.

通过更改配置设置,我们实际上可以远程访问MongoDB,而无需进行身份验证。 但这显然不是我们想要的解决方案。 相反,我们将创建一个使用旧版MongoDB凭据的新管理员。

Now restart MongoDB again by running the restart script in ctlscript.sh.

现在,通过运行ctlscript.sh的重启脚本来再次重启MongoDB。

cd ..
ls
sudo bash ctlscript.sh restart mongodb

Finally, login to MongoDB and run the following to revert the authentication mechanism.

最后,登录MongoDB并运行以下命令以还原身份验证机制。

> use admin
> var schema = db.system.version.findOne({"_id": "authSchema"}) 
> schema.currentVersion = 3 
> db.system.version.save(schema)

Your output should look like the below.

Reverting Authentication

您的输出应如下所示。

Now create a new admin profile. This administrator will work with the reverted authentication mechanism and work for remote connections. To restrict access for this remote admin, I am going to limit its access to the dummyDB we created earlier.

现在创建一个新的管理员配置文件。 该管理员将使用还原的身份验证机制并用于远程连接。 为了限制对此远程管理员的访问,我将限制其对我们之前创建的dummyDB访问。

> show dbs 
> use dummyDB
> db.createUser({
        user: "NEW USERNAME",
        pwd: "NEW PASSWORD",
        roles:[
                {
                        "role" : "readWrite",
                        "db": "dummyDB"
                }
        ]
})

Once created, go back again to the mongodb.conf file, comment out noauth=true, and uncomment auth=true. (Essentially re-activate the authentication requirement). Your config file should now look like this:

创建后,再次返回mongodb.conf文件,注释掉noauth=true ,并取消注释auth=true 。 (本质上是重新激活身份验证要求)。 您的配置文件现在应如下所示:

Auth=True

Restart MongoDB again using the ctlscript.sh and then try connecting to your Mongo instance via local terminal again.

使用ctlscript.sh再次重新启动MongoDB,然后尝试再次通过本地终端连接到您的Mongo实例。

mongo {EC2 DOMAIN}/dummyDB-u {New Username} -p {password}

Connected Remotely

Voila! Connection successful!

瞧! 连接成功!

将EC2 DB集成到我们的应用程序中 (Incorporating the EC2 DB into our App)

Okay. We have one final step before we can re-deploy our code. We just need to incorporate the new MongoDB URI into our MEAN application. Open up your forked node-todo app from before.

好的。 在重新部署代码之前,我们还有最后一步。 我们只需要将新的MongoDB URI合并到我们的MEAN应用程序中即可。 从以前打开您的派生node-todo应用程序。

Navigate to the database.js file found in the config folder.

导航到config文件夹中的database.js文件。

Comment out the existing URL and replace it with the URL associated with your EC2 instance of MongoDB. It should follow the format below:

注释掉现有URL,并将其替换为与您的MongoDB EC2实例关联的URL。 它应遵循以下格式:

'mongodb://{NEW USERNAME}:{NEW PASSWORD}@{EC2 URL}:{PORT}/dummyDB'

As a reference, here's how my url looked:

作为参考,这是我的网址的外观:

'mongodb://todoAholic:scotchio@ec2-52-0-14-185.compute-1.amazonaws.com:27017/dummyDB'

Finally, run your code locally using node server.js and navigate to localhost:8080. If all went well the app should work as before. The big change is that you're now using your EC2 instance of MongoDB. Huzzah!

最后,使用node server.js在本地运行代码,然后导航到localhost:8080 。 如果一切顺利,该应用程序将像以前一样运行。 最大的变化是您现在正在使用MongoDB的EC2实例。 晕!

To-Do with Mongo EC2

Add a few to-do items in the web application. Then open up your PuTTY window, login to MongoDB, and query the dummyDB database to confirm that the todo items match.

在Web应用程序中添加一些待办事项。 然后打开您的PuTTY窗口,登录到MongoDB,并查询dummyDB数据库以确认待办事项是否匹配。

> use dummyDB
> show collections
> db.todos.find().pretty()

Do we have a match?

我们有比赛吗?

Matching MongoDB  Records

YES! Victory at last.

是! 最后胜利了。

推,拉和调用一天 (Pushing, Pulling, and Calling it a Day)

We've been through a lot in these past two tutorials. All that's left for us to do is to push our node-todo code into Github and pull the updates onto our Amazon EC2 instance.

在过去的两个教程中,我们经历了很多。 我们要做的就是将node-todo代码推送到Github中,并将更新拉到我们的Amazon EC2实例上。

From your local dev environment commit and push your changes to Github.

在您本地的开发环境中,提交并将更改推送到Github。

git commit -m "Changed the DB URL to use EC2"
git push

Then from the node-todo folder of your EC2 instance, run

然后从您的EC2实例的node-todo文件夹中运行

sudo git pull

Pulled code

Finally, run forever to launch your MEAN application.

最后,永久运行以启动MEAN应用程序。

forever stopall
forever start server.js

And with that, it's time to check out our finished work.

至此,是时候检查一下我们完成的工作了。

http://ec2-52-0-14-185.compute-1.amazonaws.com:8080/

http://ec2-52-0-14-185.compute-1.amazonaws.com:8080/

Finished App

总结思想 (Closing thoughts)

After a long and hard slog we've finally completed our EC2 deployment. In this two part tutorial we went through the process of initializing an EC2 instance, setting up Node and MongoDB, configuring security settings, and connecting our app to a running instance of MongoDB located on our EC2 server.

经过漫长而艰苦的努力,我们终于完成了EC2部署。 在这分为两部分的教程中,我们完成了初始化EC2实例,设置Node和MongoDB,配置安全设置以及将我们的应用程序连接到EC2服务器上正在运行的MongoDB实例的过程。

As you can see, the process was arduous. But with the power of EC2 behind you, it becomes possible to create and deploy, low-cost MEAN applications for the entire world to see. Good luck building something great!

如您所见,该过程很艰巨。 但是,有了EC2的强大功能,就有可能创建和部署低成本的MEAN应用程序,供全世界使用。 祝您好运!

翻译自: https://scotch.io/tutorials/deploying-a-mean-app-to-amazon-ec2-part-2

nl-mean程序下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值