当前位置:   article > 正文

在阿里云上部署nodejs服务 + https_node项目配置ssl证书并绑定域名

node项目配置ssl证书并绑定域名

准备

1.备案域名
2.阿里云服务器
3.node.js环境
  • 1
  • 2
  • 3

开始

1. 创建ECS云服务器实例

(1)所有配置可以选最低即可(省钱)
(2)主机选取: CentOS 7.9.*位
(3)创建完成后,在实例列表中重置实例密码&远程连接密码,修改完之后重启实例
重置密码

2. 用MotaXterm连接web服务器

创建连接
创建session

输入密码显示连接成功连接成功

3. 部署node.js

3.1 环境准备

  • 确认cd /opt/software 目录是否存在,没有的话创建该目录
  • 安装node
// 安装
wget https://nodejs.org/dist/v15.11.0/node-v15.11.0-linux-x64.tar.xz
//解压:
tar xvJf node-v15.11.0-linux-x64.tar.xz
//删除二进制包:
rm -rf node-v15.11.0-linux-x64.tar.xz
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 配置环境变量
//编辑环境变量文件:
vi /etc/profile
export PATH=$PATH:/opt/software/node/bin/

// 保存环境变量
source /etc/profile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 检查安装版本
node -v
npm -v
  • 1
  • 2
  • 安装cnpm镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org
  • 1

3.2 上传项目并启动

3.2.1 创建项目
mkdir testApp
cd testApp
npm init -y
  • 1
  • 2
  • 3

package.json 配置如下

{
  "name": "testApp",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc",
    "start": "tsc && node dist/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@koa/cors": "^3.3.0",
    "@types/koa": "^2.13.4",
    "@types/koa-router": "^7.4.4",
    "koa": "^2.13.4",
    "koa-router": "^10.1.1",
    "koa-sslify": "^5.0.0",
    "typescript": "^4.6.3"
  },
  "devDependencies": {
    "@types/koa-sslify": "^4.0.3",
    "@types/koa__cors": "^3.1.1"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

创建 src/index.ts 内容如下:

import koa from "koa";
import router from "koa-router";
import cors from "@koa/cors";
import crypto from "crypto";

const app = new koa();
const t =  new router();
app.use(cors());
t.get("/", async ctx => {
  ctx.body = {
    code: 200,
    msg: "你好 按理云"
  }
});
t.get("/tt", async ctx => {
  ctx.body = {
    code: 200,
    mag: "你访问成功了噢"
  }
});
app.use(t.routes());
app.listen(5000, () => {
  return console.log('https server is running on 5000');
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

编译

tsc
  • 1
3.2.2 运行

将编译好的 dist/index.jspackage.json 拖到 /home/testApp 目录下
上传项目

然后在命令窗口中执行

// 进入目录
cd /home/testApp
// 安装依赖包
npm install
// 启动
node index.js
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
3.2.3 配置安全组

配置安全组
配置规则
添加规则

现在可以访问 http://www.testwebhook.itwin365.cn

3.3 使用 pm2 启动多个项目

  • 安装
npm install pm2 -g
  • 1
  • 启动
//进入第一个项目下
pm2 start index.js  --name my-server
  • 1
  • 2
  • 常用命令
命令解释
pm2 start index.js --name my-server启动并命名进程
pm2 list显示所有进行
pm2 stop my-server停止my-server这个进程
pm2 restart all启动所有进程
pm2 delete my-server删除某个进程
pm2 show my-server查看某个进程的详情信息
pm2 logs查看日志信息

4. 绑定域名

4.1 申请域名

登录阿里云 -> 域名解析
如果有备案的域名未绑定的,可直接用;如果备案的域名已绑定,可以创建二级域名使用。以下介绍后者方法的使用。
域名解析
申请二级域名

4.2 下载证书

验证完成之后域名后,申请该域名证书并下载。
申请证书

5. 安装Nginx,配置HTTPS

//安装nginx
yum install nginx
//启动nginx
nginx 
//使用命令nginx -t查找nginx配置文件
并使用vi命令修改该配置文件
nginx -t
vi /etc/nginx/nginx.conf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

将下载的 ssl 证书存放到 /root/cert 目录下

修改 nginx.conf ,配置 https

server {
        listen 443 ssl;
        listen [::]:443 ssl http2;
        root /home/index.html;
        server_name www.testwebhook.itwin365.cn;
        ssl_certificate /root/cert/testwebhook.itwin365.cn.pem;
        ssl_certificate_key /root/cert/testwebhook.itwin365.cn.key;
        location / {
                proxy_pass http://8.142.212.182:5000;
        }
    }

    server {
        listen 80;
        server_name www.testwebhook.itwin365.cn;
        root /home/index.html;
        location / {

        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

现在可以访问 htps://www.testwebhook.itwin365.cn

常用的 Nginx 命令:

yum install nginx   //安装nginx
nginx //启动nginx
cd /etc/nginx  //进入nginx的安装目录
  • 1
  • 2
  • 3
 ps -ef |grep nginx  // 查看nginx是否正常运行
 netstat -lntp  //查看所有被主机监听的端口 
  • 1
  • 2
nginx -s stop   //停止nginx
nginx -s reload  //重启nginx
  • 1
  • 2
//修改配置文件
nginx -t
vi /etc/nginx/nginx.conf
nginx -s reload
  • 1
  • 2
  • 3
  • 4
 //再配置一个端口
  server {
        listen 9090;
        server_name chen;
        root /home/html/chen;

        location /{
                
        }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/103667
推荐阅读
相关标签
  

闽ICP备14008679号