当前位置: 移动技术网 > 科技>操作系统>windows > 自建windows服务器如何部署egg应用

自建windows服务器如何部署egg应用

2019年10月17日  | 移动技术网科技  | 我要评论

我的东方天使,新年祝贺,斗战神牛魔前期加点

1. 使用ie浏览器登陆vpn


2. 远程登陆

3. 在服务器安装最新的node.js,git等

4. 下载源码

> git clone ****.git

5. npm安装依赖

> cd you-project
> npm i

6. 使用egg单进程启动

// 安装最新的egg包
// 在项目根目录下新建run.js

const egg = require('egg');

function normalizeport(val) {
  const listenport = parseint(val, 10);

  if (isnan(listenport)) {
    return val;
  }

  if (listenport >= 0) {
    return listenport;
  }

  return false;
}

const port = normalizeport(process.env.port) || 3000;

egg.start({ ignorewarning: true })
  .then(app => {
    app.listen(port);
    app.logger.info(`server running  on ${port} ...`);
  });

测试启动

> node run.js

7. pm2启动

  • 安装pm2
> npm i pm2 -g
  • 新建pm2启动文件
module.exports = {
  apps : [{
    name: '****',
    script: 'run.js',

    // options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
    args: 'one two',
    instances: 4,
    autorestart: true,
    watch: false,
    max_memory_restart: '4g',
    env: {
      node_env: 'development',
    },
    env_production: {
      node_env: 'production',
      app_url: '*****',
      db_host: 'localhost',
      db_port: '3306',
      db_username: '*****',
      db_password: '*****',
      db_database: '*****',
      egg_server_env: '****',
    },
  }],
};

  • 生产环境启动
$ pm2 start ecosystem.config.js --env production
  • 测试环境启动
$ pm2 start ecosystem.config.js

8. 开放3000端口

参考

9. 安装mysql,

参考: https://blog.csdn.net/u013235478/article/details/50623693, 设置mysql开机启动

10. 设置pm2开机启动,使用nssm

  • 查看pm2_home, pm2 save
  • 设置系统环境变量 pm2_home = c:\users\gysd\.pm2
  • 验证 echo %pm2_home%
  • 创建启动脚本 pm2_startup.bat
@echo off
set homedrive=c:
set pm2_home=c:\users\***\.pm2

@rem ensure that pm2 command is part of your path variable
@rem  if you're not sure, add  it here, as follow:
set path=c:\users\****\appdata\roaming\npm;%path%

@rem optionally, you can add 'pm2 kill' just before 
@rem  resurrect (adding a sleep between 2 commands):
@rem    pm2 kill
@rem    timeout /t 5 /nobreak > nul
@rem    pm2 resurrect
@rem otherwise, you can simple call resurrect as follow:
pm2 resurrect

echo "done"

  • nssm.exe install mypm2service

    • 选择自己的 pm2_startup.bat 路径
  • 重启查看

参考:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网