未加星标

Tutorial: Creating and managing a Node.js server on AWS, part 2

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Inpart 1 we started a server, responding to HTTP requests on port 3000. In this tutorial we will look at 4 more concepts

Serving HTTP traffic on the standard port, 80 Keeping the Node.js process running Deploying code into the server Serving some HTML Serving HTTP traffic on the standard port,80

Paste this URL into a URL bar in a new tab.

http://imgur.com:80/

You will notice the 80 gets dropped in the URL bar. That’s because port 80 is the default port for HTTP traffic.

HTTPS traffic uses port 443 . Try Hacker News.

https://news.ycombinator.com:443/

Because these ports are often public, you need special privileges to run processes using them. Also, it is not great to run Node.js on port 80 or 443 directly because you may want to open up a few different applications on these ports. With a router you will be able to send traffic from port 80 or 443 to any program you wish, depending on the headers of the incoming HTTP request.

There are few great choices for a router, but I find that nginx is generally the best tool for most things. It’s great for building your first ever app, or when you need to scale up to millions of visitors.

Before beginning, start your server if it is stopped and SSH into it ( as shown in the last tutorial ). Once logged in, we can install nginx .

Ubuntu comes with it’s own package manager, apt-get . Using apt-get , we can install nginx in one command.

sudo apt-get install nginx

Most linux distributions come with a package manager, so Google your version if apt-get doesn’t work. apt-get runs nginx automatically after install so you should now have it running on port 80 , check by entering your public DNS URL into a browser.


Tutorial: Creating and managing a Node.js server on AWS, part 2
Default nginxpage

If this doesn’t work, you might need to start it manually.

sudo /etc/init.d/nginx start

If you are still can’t reach the server then check out Stack Overflow or post a comment here.

We need to configure nginx to route port 80 traffic to port 3000 . nginx has config placed in the /etc/nginx/sites-available folder where there is already a default config which serves the nginx welcome page we saw earlier.

You can take a look at this config using cat.

available

How are nginx configs set up? Configs are stored in plain text files in sites-available with any name. Linking them into the sites-enabled folder will cause them to be read and used when nginx starts. All of the configs are combined together by nginx .

Let’s first remove the default config from sites-enabled , we will leave it in sites-available for reference.

enabled

Create a config file in sites-available and name it whatever you like.

available

The following is the config we are going to use.

server {
listen 80;
server_name tutorial;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:3000;
}
}

This will forward all HTTP traffic from port 80 to port 3000 .

Link the config file in sites enabled (this will make it seem like the file is actually copied in sites-enabled ).

sudo ln -s /etc/nginx/sites-available/tutorial /etc/nginx/sites-enabled/tutorial

Read more about symbolic links here if they are unfamiliar.

Restart nginx for the new config to take effect.

sudo service nginx restart

If you did not stop/start the server since the last tutorial then you may still have your node application running.

# list background jobs jobs

If your application shows up then no need to run it again. If it is not running then you need to start it.

node tutorial/index.js

Once the server is running, press ctrl+z , then resume it as a background task.

bg %1

Now visit your server’s public DNS URL, using port 80 .


Tutorial: Creating and managing a Node.js server on AWS, part 2

The server is running on port 80 !

Keeping the Node.js processrunning

It’s quite tedious using ctrl+z to pause a process, and then running it in the background. Also, doing it this way will not allow the Node.js process to restart when you restart your server after an update or crash.

Before moving forward, stop your running node process

# Nukes all Node processes killall -9 node

To keep these processes running we are going to use a great NPM package called PM2 . While in an SSH session, install PM2 globally.

npm i -g pm2

To start your server, simply use pm2 to execute index.js .

pm2 start tutorial/index.js

To make sure that your PM2 restarts when your server restarts

pm2 startup

This will print out a line of code you need to run depending on the server you are using. Run the code it outputs.

Finally, save the current running processes so they are run when PM2 restarts.

pm2 save

That’s it! You can log out/in to SSH, even restart your server and it will continue to run on port 80.

To list all processes use

pm2 ls

Your process will have a pretty generic name, something like index , which would be hard to differentiate if you had a few other microservices running on the server too. Let’s stop the process, remove it and start it back up with a nicer name.

# Use the number listed in pm2 ls # to stop the daemon pm2 stop index # Remove it from the list pm2 delete index # Start it again, but give it a # catchy name pm2 start tutorial/index.js --name “Tutorial”

Great! Check out the PM2 docs to see what else you can do with process management.

Deploying code into theserver Instead of writing code in an SSH session, let’s push the code to a git repo in Github, SSH into t

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Node.jsGitHTMLUbuntuLinux
分页:12
转载请注明
本文标题:Tutorial: Creating and managing a Node.js server on AWS, part 2
本站链接:http://www.codesec.net/view/522990.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(40)