准备
购买证书后下载,将证书放在还在项目的中(包括一个证书和一个密钥)
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访问站点了