Amazon EC2 Instance Express API配置HTTPS
来源:互联网 发布:阿里云部署git服务器 编辑:程序博客网 时间:2024/05/16 05:18
最近在做一个API的项目,放在Amazon EC2上跑,为了提高安全性想把链接搞成HTTPS的。API放在Express上,GoDaddy上面没有Node Server配置的教程,搜到很多教程都是通过AWS Load Balancer来配置的,所以自己把各教程综合了一下,完成了配置。
第一步:买一个SSL证书
我看很多人都是自己签发证书。但是我因为有折扣,打完折之后$3/年,很便宜,就直接从GoDaddy买了。
第二步:生成CSR
买完证书之后,在账户里进行配置的时候发现GoDaddy要求输入CSR。生成CSR这一部分需要在EC2 Instance里面完成。主要是根据Creating And Installing A SSL Certificate On Amazon EC2 里面第1-6步来做的。
1. 生成私钥(private key)
$ openssl genrsa -des3 -out host.key 2048Generating RSA private key, 2048 bit long modulus..................................................+++...............................+++e is 65537 (0x10001)Enter pass phrase for host.key:Verifying - Enter pass phrase for host.key:
注意:(1) EC2里openssl是装好可以直接用的。
(2) pass phrase要记住,后面还要用
2. 用私钥生成certificate signing request,也就是前面所提到的CSR。
$ openssl req -new -key host.key -out host.csr
输入这个命令之后openssl会让你输入上一步里提到的pass phrase,然后在输入若干跟你的网站和公司有关的信息如下。其中Organizational Unit Name和Common Name是最重要的,要输入你的网站的域名,其他可以随便填。
Enter pass phrase for host.key:You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [AU]:USState or Province Name (full name) [Some-State]:MissouriLocality Name (eg, city) []:Saint LouisOrganization Name (eg, company) [Internet Widgits Pty Ltd]:My CompanyOrganizational Unit Name (eg, section) []:www.mycompany.comCommon Name (e.g. server FQDN or YOUR name) []:www.mycompany.comEmail Address []:contact@mycompany.comPlease enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:An optional company name []:
第三步:提交CSR
1. 上面的命令完成后会生成一个host.csr,用vi打开这个csr文件,会看到:-----BEGIN CERTIFICATE REQUEST-----[encoded text here]-----END CERTIFICATE REQUEST-----把这个文件的内容(包括“BEGIN CERTIFICATE REQUEST”和“END CERTIFICATE REQUEST”这两行)复制黏贴到GoDaddy的CSR request form里,提交,等待GoDaddy验证审批完成。
2. 审批完成之后,GoDaddy会生成一个压缩包,里面有两个crt文件(一个随机命名的crt文件和一个gd_bundle.crt文件)。
第四步:上传.crt文件
我的笔记本是windows系统,所以需要下载一个WinSCP,通过这个软件把crt文件上传到EC2。
这个软件跟Putty很类似,用法跟通过putty ssh到ec2一样,用过putty ssh ec2的筒子们应该都很熟悉。主机名就是你的ec2主机名,ec2-xx-xx-xxx-xxx.us-west-x.compute.amazonaws.com,端口22也就是ssh,协议sftp,用户名ubuntu(我的ec2是ubuntu),密码空置。
唯一需要注意的是,要点击“编辑” ==〉“高级” ==〉“SSH” ==〉“验证” ==〉“密钥文件”,把你的ec2的私钥(.ppk)文件的路径输进去。
具体的详细步骤请见 http://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/putty.html#Transfer_WinSCP
ssh连接建立好后就可以很方便的把本地的crt文件上传的ec2了。
第五步:生成pem格式的私钥
之前的私钥host.key并不是pem格式,需要通过下面的命令把.key转成.pem
openssl rsa -in host.key -out newkey.pem && mv newkey.pem key.pem否则你会看到错误
mgechev → MinBook Pro ~/Desktop/test Thu Apr 30 11:56:03 $ node index.js_tls_common.js:67 c.context.setKey(options.key); ^Error: error:0906A068:PEM routines:PEM_do_header:bad password read at Error (native) at Object.createSecureContext (_tls_common.js:67:17) at Server (_tls_wrap.js:595:25) at new Server (https.js:36:14) at Object.exports.createServer (https.js:56:10) at Object.<anonymous> (/Users/mgechev/Desktop/test/index.js:6:11) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:349:32) at Function.Module._load (module.js:305:12)以上步骤来自http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/。
第六步:更改代码
证书和密钥设置好了,下一步需要更改代码以监听443端口。直接上代码
var express = require('express');var https = require('https');var http = require('http');var fs = require('fs');// This line is from the Node.js HTTPS documentation.var options = { key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')};// Create a service (the app object is just a callback).var app = express();// Create an HTTP service.http.createServer(app).listen(80);// Create an HTTPS service identical to the HTTP service.https.createServer(options, app).listen(443);以上代码来自http://stackoverflow.com/a/14272874/2177408
第七步:运行
运行服务器之后,可能会发现程序报错
$ node server.jsevents.js:154 throw er; // Unhandled 'error' event ^Error: listen EACCES 0.0.0.0:80这个Error可能是80端口也可能是443端口,取决于你监听哪个端口。
这是因为我们没有权限来监听这两个端口。所以需要用sudo
sudo node server.jshttp://stackoverflow.com/a/39320632/2177408
完成这些步骤之后,我们就可以通过https来访问跑在ec2上的API了。
最后友情提醒一下,不要忘记在aws ec2 security group上打开443和80端口。
- Amazon EC2 Instance Express API配置HTTPS
- Try Magento on an Amazon EC2 Instance
- 为Amazon EC2 Instance 设置允许ping
- Remote connect to Amazon EC2 instance
- Amazon Elastic Compute Cloud(EC2) Local Instance Store Volumes
- Amazon EC2
- amazon ec2
- Amazon EC2
- 【Amazon EC2 Python API系列】boto的介绍和安装
- 【Amazon EC2 Python API系列】获取实例信息
- Amazon EC2 Ubuntu Server PPTP(VPN)配置手记
- Amazon EC2安装mysql多实例并配置主从复制
- [转贴] 体验Amazon EC2
- Amazon EC2初探
- 转载---about Amazon EC2
- Amazon EC2 CPU steal
- Amazon Web Services EC2
- 查询amazon ec2费用
- Windows下Tensorflow安装流程
- 205. Isomorphic Strings
- C语言计算日期,前n天,后n天方法
- 80老翁谈人生(314):别了,亲爱的CSDN读者朋友们!
- 【LeetCode】 413. Arithmetic Slices
- Amazon EC2 Instance Express API配置HTTPS
- ZCMU-1787-Babelfish
- Erlang 实现互斥量
- android_39_跳转至第2个Activity
- java常用单词
- Leetcode-455. Assign Cookies
- 将netbeans界面语言设置为英文
- 将netbeans界面语言设置为英文
- 循序渐进讲解 Python 装饰器