centos使用node搭建https服务

准备

购买证书后下载,将证书放在还在项目的中(包括一个证书和一个密钥)
在这里插入图片描述
npm下载下载 express (先安装好node环境)

npm install  express --save

app.js代码

//app.js代码
'use strict'
const https = require('https');
const fs = require('fs');
const express = require('express');
const path = require('path');
const app = express();
// app.use('/public', express.static("/public"))
app.use(express.static(path.join(__dirname, './public')));
app.get('/', (request, response) => {
    response.writeHead(200)
    fs.createReadStream(__dirname + "/public/index.html")
      .pipe(response)
})
const options = {
	key  : fs.readFileSync('./cert/4579472_www.guoang.xyz.key'),
	cert : fs.readFileSync('./cert/4579472_www.guoang.xyz.pem') 
}
const https_server = https.createServer(options, app);
https_server.listen(443, '0.0.0.0');

配置安全组

添加一条443端口安全组规则
在这里插入图片描述

允许防火墙放行443端口

1.先查看服务器防火墙开放的端口

firewall-cmd --zone=public --list-ports     //查看防火墙的开放端口

2.允许防火墙放行443端口

firewall-cmd --zone=public --add-port=443/tcp --permanent、
//zone #作用域
//add-port=443/tcp #添加端口,格式为:端口/通讯协议
//permanent #代表永久生效,没有此参数重启后失效

3.重启防火墙

firewall-cmd --reload

启动服务

要使用root用户 1024以下的端口监听需要root权限
1.启动服务

pm2 start app.js

2.查看443端口有没有被监听

netstat -apn|grep '443' 

在这里插入图片描述
当443端口在被监听时就可以通过https访问站点了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值