centos7部署rap2


本文章学习自: https://blog.csdn.net/weixin_41004350/article/details/81737354

介绍

服务 版本
node.js 8.9.4+
mysql 5.7+
redis 4.0 +

yum安装mysql5.7版本

  • 修改mysql yum源
rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm;
  • 修改yum源配置文件,关闭8.0版本开启5.7版本
vim /etc/yum.repos.d/mysql-community.repo;
#---------------------------------------------------------------------------
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file://etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 
  • 安装mysql
yum install mysql-community-server mysql-community-client mysql-community-libs
  • 修改mysql配置文件
vim /etc/my.cnf;
--------------------------------
[mysqld]
port = #端口号
character_set_server = utf8
  • 启动mysql,创建用户
service mysqld start;
grep 'temporary password' /var/log/mysqld.log;#查看初始密码
set password for 'root'@'localhost' = password('yourpasswd');#修改root密码
#为root赋权限,远程
grant all privileges on *.* to root@"%" identified by '{yourpasswd}' with grant option;
#为root用户赋权限,本机
grant all privileges on *.* to root@localhost identified by '{yourpasswd}' with grant option;
#为普通用户权限,本机
grant all privileges on {database_name}.* to {user}@localhost identified by '{user_passwd}';
#为普通用户赋权限,远程
grant all privileges on {database_name}.* to {user}@"%" identified by '{user_passwd}' with grant option;
#更新权限设置
flush privileges;

yum及rpm安装redis

  • yum安装
yum install redis;#查看是否由Yum源
yum install epel-release;#下载fedora的epel仓库
yum install yum install redis; 安装redis

service redis start#开启服务
redis-server /etc/redis.conf #开启服务
vim /etc/redis.conf #配置文件
redis-cli  shutdown; #关闭服务
redis-server -v; #查看版本
  • rpm安装
#安装依赖
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm && rpm -ivh epel-release-7-11.noarch.rpm;
yum install -y jemalloc;
#安装redis
wget http://www.rpmfind.net/linux/remi/enterprise/7/test/x86_64/redis-4.0.0-1.el7.remi.x86_64.rpm && rpm -ivh redis-4.0.0-1.el7.remi.x86_64.rpm;
#设置开机自启
systemctl enable redis;
#查看状态启动关闭
systemctl status/start/stop/restart redis;

安装node.js

  • 安装依赖
uname -a; #查看Linux版本
#添加epel源
rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm;#64位
rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm;#32位
#导入key
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6;
#添加 remi 源
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm;
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi;
#安装
curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -;#5.
curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -;#8.
curl --silent --location https://rpm.nodesource.com/setup_7.x | bash -;#7.
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -;#6.
yum -y install nodejs;

#查看版本
node -v;
npm -v;
  • 安装指定版本

# 下载安装包
wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz
 
# 解压
tar -xvf node-v8.11.4-linux-x64.tar.xz
 
# 设置环境变量,添加如下内容
vim /etc/profile
--------------------------------------
#nodejs env
export NODE_HOME={your_dir}
export PATH=$PATH:$NODE_HOME/bin
---------------------------------------
# 生效环境变量
source /etc/profile
 
# 安装完成,查看
node -v
--------------------- 

安装RAP2后端

  • 拉取github代码
git clone https://github.com/thx/rap2-delos.git;
  • 创建数据库
CREATE DATABASE IF NOT EXISTS RAP2_DELOS_APP DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
  • 修改配置文件./src/config/config.*
vim src/config/config.dev.ts;
cp src/config/config.dev.ts src/config/config.local.ts;
vim src/config/config.prod.ts;
let config: IConfigOptions = { 
  version: '2.3',
  serve: {
    port: 8080, #配置端口号
  },  
  keys: ['some secret hurr'],
  session: {
    key: 'rap2:sess',
  },  
  db: {
    dialect: 'mysql',   #配置Mysql信息
    host: 'IP地址',
    port: 端口号,
    username: '用户',
    password: '密码',
    database: 'RAP2_DELOS_APP',
    pool: {
      max: 5,
      min: 0,
      idle: 10000,
    },  
    logging: false,
  },  
  redis: {              #配置redis信息
    host: '127.0.0.1',                                                                                            
    port: 6379
         },  
  mail: {
    host: 'smtp-mail.outlook.com',
    port: 587,
    secure: false,
    auth: {
      user: '[email protected]',
      pass: ''
    }   
  },  
  mailSender: '[email protected]',
}
  • 构建及初始化项目

# 初始化
npm install
# 安装 && TypeScript编译
npm install -g typescript
npm run build
--------------------------------------
如果报错如下:
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `rimraf -rf dist/ && tsc`
npm ERR! Exit status 2
修改文件:
###############################
vim ./tsconfig.json
"noImplicitAny": false,
###############################
--------------------------------------

# 初始化数据库
npm run create-db
  • 修改配置文件
cp src/config/config.dev.ts src/config/config.local.ts;
vim src/config/config.prod.ts;
#修改config.prod.ts的Mysql和Redis
----------------------------------------------------------
db: {
        dialect: 'mysql',
        host: process.env.MYSQL_URL || '本机ip',
        port: (process.env.MYSQL_PORT && parseInt(process.env.MYSQL_PORT)) || 33966,
        username: process.env.MYSQL_USERNAME || 'root',
        password: process.env.MYSQL_PASSWD || '密码',
        database: process.env.MYSQL_SCHEMA || 'RAP2_DELOS_APP',
        pool: {
            max: 80,
            min: 0,
            idle: 20000,
            acquire: 20000,
        },
        logging: false,
    },
    redis: {
        host: process.env.REDIS_URL || '127.0.0.1',
        port: (process.env.REDIS_PORT && parseInt(process.env.REDIS_PORT)) || 6379
    },

---------------------------------------------------
#后台启动服务
npm start;
# 开放后台服务端口8080
#firewall-cmd --add-port=8080/tcp --permanent
#firewall-cmd --reload
  • 安装pm2管理后台运行服务
npm install -g pm2 #-g全局安装
---------------------------------
#pm2常用命令
pm2 list;
pm2 show app_id/app_name;
pm2 stop/start <name/id>;
pm2 log <name/id>;
pm2 status <name/id>;
#日志地址
ls -al /root/.pm2/logs;
  • 启动后访问本地的8080端口出现以下结果即搭建成功:
    在这里插入图片描述

搭建RAP2前端服务

  • 下拉git源码
git clone https://github.com/thx/rap2-dolores.git;
  • 修改配置文件
vim src/config/config.prod.js
-----------------------------------------------------
module.exports = {
  serve: '{前端地址http://xxx.xx.xx.xxx:8080/}',   # 这里填后端的外网访问地址,一定要填ip,不能直接localhost,因为浏览器会访问这个地址,调用后端接口的。
  keys: ['some secret hurr'],
  session: {
    key: 'koa:sess'
  }
}
-----------------------------------------------------
  • 编译启动前端
npm install
 
# 编译构建build文件夹
npm run build
 
# 这里注意,官网教程是安装serve工具,然后启动服务,如下:(强烈不推荐这样)
npm install -g serve
serve -s build -p 8082
 
# 但是serve启动的静态文件服务器不能在后台运行,不能方便的管理。
# 这里推荐直接用pm2来启动静态文件服务器,这样后端服务和前端服务都用pm2管理,更方便,pm2更强大。
# ./build为静态文件夹,8082指定为监听端口,--name 指定一个name,--watch则可以监听文件变化,自动变化。非常强大。
pm2 serve ./build 8082 --name rap2-dolores --watch
 
# 最后别忘了还要开放服务器端口
#firewall-cmd --add-port=8082/tcp --permanent
#firewall-cmd --reload
--------------------- 

若npm run build报错如下:
在这里插入图片描述

#上述报错因root权限不足,运行:
npm install --unsafe-perm;
npm run build
  • 访问本地8082端口,出现以下界面即表示搭建成功:
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43819222/article/details/87201695