一、上传 Git 仓库
二、购买云服务器
https://www.aliyun.com/
教程中选择【香港】,是因为后期演示域名解析,不用备案。
按需选择
下单购买
等待启动 - 复制公网ip
三、远程服务器
进入远端系统,安装软件:
(1)Git:https://gitforwindows.org/
(2)node:https://nodejs.cn/download/
(3)mongodb:https://www.mongodb.com/
四、代码克隆
(1)创建文件夹,名字随意,一般使用 www
(2)使用 git 克隆项目
(3)安装依赖 npm i
和 nodemon npm i -g nodemon
(4)运行项目 npm start
公网 ip 也可以访问
注意:为了更规范,在本地修改运行命令后再提交git,远端服务再用git 拉取代码并运行。
五、购买域名和解析
解析
直接访问域名地址。
注意:可能需要添加安全组才能访问!
六、配置 HTTPS 证书
必须在远程服务器操作才生效!
工具官网:https://certbot.eff.org/
操作流程:
先停止当前 80 端口的服务
1、下载工具 https://dl.eff.org/certbot-beta-installer-win_amd64.exe
2、安装工具
3、管理员运行命令 certbot certonly --standalone
4、代码配置
const https = require('https');
https.createServer(
{
key: fs.readFileSync('/etc/letsencrypt/path/to/privkey.pem'),
cert: fs.readFileSync('/ect/letsencrypt/path/to/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/path/to/chain.pem'),
}, app
).listen(443, () => {
console.log('Listening...')
})