最近需要写一个简单的API并部署到EC2上,步骤备忘如下:
1. 通过AWS Dashboard新建一个ubuntu EC2 instance
过程简单,不再赘述。新建过程中AWS会提示要不要新建一个私钥.pem文件,Yes。
2. 通过PuTTYgen和PuTTY远程SSH登录虚拟机
具体步骤见Connecting to Your Linux Instance from Windows Using PuTTY中的Prerequisites、Converting Your Private Key Using PuTTYgen和Starting a PuTTY Session三节。
3. 安装node和npm
-
$ sudo apt-get update
-
$ sudo apt- get install nodejs npm
确认安装成功:
node -v
npm -v
node -v 如果出现如下情况:
可通过一下步骤解决(http://stackoverflow.com/a/32740546/2177408):
Do some additional work to use only
node
for the command line:
install package manager npm:
sudo apt-get install npm
then upgrade npm:
sudo npm cache clear --force && sudo npm install -g npm
next install n:
sudo npm install -g n
which is a version manager vor node.after this upgrade your node installation:
sudo n stable
this will create a/usr/bin/node
script which fixes your described issue so you can usenode app.js
to execute your app instead ofnodejs app.js
.You can downgrade node to a desired version, e.g:
sudo n 0.12.4
check your version to verify:
node --version
4. 安装git并clone repo
$ sudo apt-get install nodejs npm
具体步骤参见:https://help.ubuntu.com/lts/serverguide/git.html
5. 设置Security group
EC2的security group的Inbound标签中,默认有一条SSH的rule,PuTTY正是通过这一条rule进行远程登录。
点击Edit按钮然后Add Rule,添加另外两条rule:
其中All ICMP是为了让EC2能够被ping到,Custom TCP Rule则是为了开放端口3000(因为我的服务器在端口3000监听对API的请求)。
完成最后一步后,便可以通过
http://<Public IP>:3000
来访问API了。