Skip to content

Commit 12f5440

Browse files
committed
Updating Post - Configuring SSL Certificates with Nodejs
1 parent b5ae4de commit 12f5440

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: "Configuring SSL Certificates with nodejs"
3+
author_name: "Edison Garcia"
4+
tags:
5+
- Nodejs
6+
- Azure VM
7+
- Express
8+
- Configuration
9+
- How-To
10+
date: 2020-02-12 02:40:00
11+
tags:
12+
header:
13+
teaser: "/assets/images/nodelinux.png"
14+
---
15+
## How to configure a SSL Certificate for NodeJS
16+
17+
In specific scenarios you are looking for creating a https server within nodejs as described in the following reference: [Https Server](https://nodejs.org/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server/)
18+
19+
You will need at least a self signed certificate for dev/test into localhost or one signed by a 'Certificate Authority'.
20+
In this example we are going to use one is signed by CA bought from GoDaddy and setup everything inside a Linux VM.
21+
22+
## First Step
23+
24+
1. In your Linux VM ssh session, you can use openssl to create the csr (Certificate signing request) with the following command:
25+
26+
```bash
27+
openssl req -new -newkey rsa:2048 -nodes -keyout yourdomainname.key -out yourdomainname.csr
28+
```
29+
30+
2. You will need to update the required information and for your fully-qualified domain name just put your custom domain or wildcard if you are using one.
31+
3. Once the .csr is generated, you can open this file and copy the content and go your SSL certificate provider and find the CSR part to paste it. In some SSL providers you need to setup the common name in their website as well.
32+
33+
In this example I am using GoDaddy since I have one already there.
34+
![GoDaddyCSR](/media/2020/02/edisga-godaddy-csr.png)
35+
36+
4. After get the ssl certificate, you can download and select the type of servers, try to select other types, we will not use nginx or apache here.
37+
[GoDaddyCSR](/media/2020/02/edisga-godaddy-downloadssl.png)
38+
39+
5. Most SSL providers will provide the following structure, where you can have the certificate (.crt) and (pem) and the bundle where is the intermediate certificate used as proxy for root CA.
40+
[GoDaddyCSR](/media/2020/02/edisga-godaddy-sslstructure.png)
41+
42+
6. Copy these files to your app location and you can use the following code, this is just an example , basically you will use the generated key from step 1, the crt and gd(bundle) as a ca:
43+
44+
```bash
45+
var express = require('express');
46+
var http = require('http');
47+
var https = require('https');
48+
var fs = require('fs');
49+
var server = express();
50+
var port = process.env.PORT || 3001;
51+
52+
var sslOptions = {
53+
key: fs.readFileSync('certificates/domainname.key','utf8'),
54+
cert: fs.readFileSync('certificates/domainname.crt','utf8'),
55+
ca: fs.readFileSync('certificates/domainname-ca.crt','utf8'),
56+
};
57+
58+
server.get('/', function (req, res) {
59+
res.send("Hello World!");
60+
});
61+
62+
https.createServer(sslOptions, server).listen(port);
63+
```
64+
65+
**This should work at this point. Note: There are some conditions where nodejs requires to separate the gd bundle into different files as following:
66+
67+
```bash
68+
var sslOptions = {
69+
key: fs.readFileSync('certificates/domainname.key','utf8'),
70+
cert: fs.readFileSync('certificates/domainname.crt','utf8'),
71+
ca: [
72+
fs.readFileSync('certificates/domainname-gd1.crt','utf8'),
73+
fs.readFileSync('certificates/domainname-gd2.crt','utf8'),
74+
fs.readFileSync('certificates/domainname-gd3.crt','utf8')
75+
]
76+
};
77+
```
78+
79+
Additional Reference can be found [Here](https://nodejs.org/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server/)

media/2020/02/edisga-godaddy-csr.png

266 KB
Loading
226 KB
Loading

0 commit comments

Comments
 (0)