[Win服务器] 自建windows服务器如何部署egg应用(图文详解)

1554 0
王子 2022-10-19 14:45:18 | 显示全部楼层 |阅读模式
1. 使用IE浏览器登陆VPN




2. 远程登陆


3. 在服务器安装最新的node.js,git等
4. 下载源码> git clone ****.git
5. npm安装依赖> cd you-project> npm i
6. 使用egg单进程启动
  1. // 安装最新的egg包
  2. // 在项目根目录下新建run.js
  3. const egg = require('egg');
  4. function normalizePort(val) {
  5. const listenPort = parseInt(val, 10);
  6. if (isNaN(listenPort)) {
  7. return val;
  8. }
  9. if (listenPort >= 0) {
  10. return listenPort;
  11. }
  12. return false;
  13. }
  14. const port = normalizePort(process.env.PORT) || 3000;
  15. egg.start({ ignoreWarning: true })
  16. .then(app => {
  17. app.listen(port);
  18. app.logger.info(`server running on ${port} ...`);
  19. });
复制代码
测试启动
> node run.js
7. pm2启动安装pm2
> npm i pm2 -g
新建pm2启动文件
  1. module.exports = {
  2. apps : [{
  3. name: '****',
  4. script: 'run.js',
  5. // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
  6. args: 'one two',
  7. instances: 4,
  8. autorestart: true,
  9. watch: false,
  10. max_memory_restart: '4G',
  11. env: {
  12. NODE_ENV: 'development',
  13. },
  14. env_production: {
  15. NODE_ENV: 'production',
  16. APP_URL: '*****',
  17. DB_HOST: 'localhost',
  18. DB_PORT: '3306',
  19. DB_USERNAME: '*****',
  20. DB_PASSWORD: '*****',
  21. DB_DATABASE: '*****',
  22. EGG_SERVER_ENV: '****',
  23. },
  24. }],
  25. };
复制代码
生产环境启动
  1. $ pm2 start ecosystem.config.js --env production
复制代码
测试环境启动
  1. $ pm2 start ecosystem.config.js
复制代码
8. 开放3000端口
参考 https://www.jb51.net/article/172191.htm
9. 安装mysql,
参考:https://www.jb51.net/article/170594.htm
设置mysql开机启动
10. 设置pm2开机启动,使用nssm
查看PM2_HOME, pm2 save
设置系统环境变量 PM2_HOME = C:\Users\GYSD\.pm2
验证 echo %PM2_HOME%
创建启动脚本 pm2_startup.bat
  1. @echo off
  2. set HOMEDRIVE=C:
  3. set PM2_HOME=C:\Users\***\.pm2
  4. @REM Ensure that pm2 command is part of your PATH variable
  5. @REM if you're not sure, add it here, as follow:
  6. set path=C:\Users\****\AppData\Roaming\npm;%path%
  7. @REM Optionally, you can add 'pm2 kill' just before
  8. @REM resurrect (adding a sleep between 2 commands):
  9. @REM pm2 kill
  10. @REM timeout /t 5 /nobreak > NUL
  11. @REM pm2 resurrect
  12. @REM otherwise, you can simple call resurrect as follow:
  13. pm2 resurrect
  14. echo "Done"
复制代码
nssm.exe install MyPM2Service
选择自己的 pm2_startup.bat 路径


重启查看


总结
以上所述是小编给大家介绍的自建windows服务器如何部署egg应用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对中国红客联盟网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行