NodeJS项目迁移兼Ubuntu下NodeJS环境部署

前端之家收集整理的这篇文章主要介绍了NodeJS项目迁移兼Ubuntu下NodeJS环境部署前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

之前做的几个项目都托管在阿里云服务器,但是最近要到期了。想着到底要不要续期,毕竟100/月。后面看着阿里云有个活动,800/三年。果断买下。环境部署折腾了一天,其中也遇到几个坑。

目录

  1. 一、安装环境
  2. 1.1 安装NodeJS环境
  3. 1.2 安装版本控制软件Git
  4. 1.3 安装MongoDB数据库
  5. 1.4 安装Nginx
  6.  
  7. 二、导入数据
  8.  
  9. 三、安装项目
  10.  
  11. 四、部署项目
  12. 4.1 Nginx配置
  13. 4.2 启用HTTPS
  14. 4.3 使用PM2部署项目
  15. 4.4 开启阿里云外网访问
  16.  
  17. 五、踩坑记录
  18. 5.1 数据库导入失败
  19. 5.2 PM2部署失败

一、安装环境

为了保证项目运行不出问题,在新服务器安装和原服务器一致的环境。项目迁移历时一天,两台服务器的系统都是Ubuntu 16.04 64位。

1.1 安装NodeJS环境

自带的NodeJS版本是4.2.6,版本有点低,使用npm的n模块更新到最新版。

安装NPM

  1. sudo apt-get install npm

使用淘宝源

阿里云访问npm的速度非常慢,这里通过设置,让NPM从淘宝镜像更新模块

  1. npm set registry https://registry.npm.taobao.org // 设置从淘宝镜像更新
  2. npm set disturl https://npm.taobao.org/dist
  3. npm cache clean // 清除缓存

更新NodeJS

  1. npm install n // 更新NodeJS的模块
  2. n stable // 更新到最新稳定版
  3. node -v // v8.2.1

1.2 安装版本控制软件Git

  1. sudo apt-get install git

1.3 安装MongoDB数据库

  1. sudo apt-get install mongodb // 安装MongoDB
  2. service mongodb start // 启动服务
  3. mongod // 进入交互式控制台,能加入说明启动成功,ctrl+c退出

1.4 安装Nginx

  1. sudo apt-get install Nginx // 安装Nginx

二、导入数据

把以前的数据库完整的迁移过来

从源服务器导出数据库

  1. mongodump -h localhost --port 27017 -d test -o database_dump

导入MongoDB数据库

  1. mongorestore -d test database_dump/test

三、安装项目

项目是在Github开源,直接拉取就行。

  1. git clone https://github.com/bergwhite/nchat.git // 克隆项目
  2. cd nchat // 进入目录
  3. npm install 安装模块
  4. npm run build

四、部署项目

4.1 Nginx配置

  1. vim /etc/Nginx/Nginx.conf // 编辑Nginx的配置
  2.  
  3. http {
  4.  
  5. server {
  6.  
  7. listen 80;
  8. server_name hostName;
  9. rewrite ^(.*) https://$server_name$1 permanent;
  10. }
  11.  
  12. server {
  13.  
  14. listen 443 ssl;
  15. server_name hostName;
  16. ssl on;
  17. # SSL证书会插入到这里
  18.  
  19. # 完整根目录
  20. location / {
  21.  
  22. root /*/*/*;
  23. index index.html;
  24.  
  25. }
  26.  
  27. # 反向代理V2EX API到本地,解决跨域问题
  28. location /api/ {
  29.  
  30. proxy_set_header X-Real-IP $remote_addr;
  31. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  32. proxy_pass https://www.v2ex.com/api/;
  33.  
  34. }
  35.  
  36. }
  37. ...

4.2 启用HTTPS

  1. sudo apt-get update // 更新软件源
  2. sudo apt-get install software-properties-common // 安装
  3. sudo add-apt-repository ppa:certbot/certbot // 添加仓库
  4. sudo apt-get update // 更新软件源
  5. sudo apt-get install python-certbot-Nginx // 安装
  6.  
  7. sudo certbot --Nginx // 生成证书(自动添加Nginx
  8. sudo certbot --Nginx certonly // 生成证书(手动添加Nginx

4.3 使用PM2部署项目

  1. sudo apt-get install pm2 // 安装pm2

4.4 开启阿里云外网访问

在安全组里添加需要放行的NodeJS项目端口即可。

五、踩坑记录

5.1 数据库导入失败

导入数据库的时候,有一个Collection没有导入成功

  1. Assertion failure amt == (size_t)( size - 4 ) src/mongo/tools/tool.cpp 330

解决方

  1. 把报错的Collection单独导出,然后重新导入到新服务器的数据库

5.2 PM2部署失败

运行下面的代码会失败

  1. pm2 start -i 0 --name test ./bin/www

解决方

使用fork模式启动

  1. pm2 start --name nchat3 ./bin/www

参考连接

猜你在找的Ubuntu相关文章