从零开始的在Centos上搭建easy-mock服务

声明

由于文章比较久远了,当时安装的easy-mock版本大概是V1.2.3的,可能跟现在easymock版本相差比较多了,大家看的时候可以关注一下坑点,但是安装步骤还是参考官方文档比较好。官方文档传送门

1. 安装nodejs

在root用户下运行

# Run as root on RHEL, CentOS or Fedora:
# 在root用户下运行一下两句命令
curl -sL https://rpm.nodesource.com/setup | bash -

yum install -y nodejs

发现 install nodejs提示

Error: Package: 1:nodejs-6.11.1-1.el7.x86_64 (eles-epel)
Requires: http-parser >= 2.7.0
查了下是epel的问题,于是运行

 yum install epel-release

发现还是报错,百度说需要更新python版本(后来发现不是python的问题)

# 将旧版本的python保留下来
mv /usr/bin/python /usr/bin/python2.7.5
# 安装依赖组件
 sudo yum -y install gcc make gcc-c++ openssl-devel wget

# 下载python
cd /usr/local/src
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz

./configure --prefix=/usr/local/python27

make

make install

ln -s /usr/local/python27/bin/python2.7  /usr/bin/python

安装好python后,发现运行yum install提示yum模块不存在,按照百度到的方法修改yum的运行脚本即可(如果不更新python,就不需要这个操作)

# root权限下
cd /usr/bin
vi yum
# 将第一句改为指向旧版本的python,保存即可
#!/usr/bin/python  >> #!/usr/bin/python2.7.5

结果闹腾了一圈发现回到原地仍然有这个报错:

Error: Package: 1:nodejs-6.11.1-1.el7.x86_64 (epel)
Requires: libhttp_parser.so.2()(64bit)
Error: Package: 1:nodejs-6.11.1-1.el7.x86_64 (epel)
Requires: http-parser >= 2.7.0

好吧原来是要安装http-parser,不是很懂这些依赖,直接按照网上的方法来

sudo rpm -ivh https://kojipkgs.fedoraproject.org//packages/http-parser/2.7.1/3.el7/x86_64/http-parser-2.7.1-3.el7.x86_64.rpm

继续安装又报错

Total download size: 7.1 M
Installed size: 26 M
Downloading packages:
Traceback (most recent call last):
File "/usr/libexec/urlgrabber-ext-down", line 22, in
from urlgrabber.grabber import
ImportError: No module named urlgrabber.grabber
Traceback (most recent call last):
File "/usr/libexec/urlgrabber-ext-down", line 22, in
from urlgrabber.grabber import
ImportError: No module named urlgrabber.grabber
Traceback (most recent call last):
File "/usr/libexec/urlgrabber-ext-down", line 22, in
from urlgrabber.grabber import
ImportError: No module named urlgrabber.grabber

百度了一下说要,vi /usr/libexec/urlgrabber-ext-down
把头部的python改成和/usr/bin/yum中一样的(如果不更新python,就不需要这个操作)

vi /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python  >> #!/usr/bin/python2.7.5

最后终于安装好nodejs了

2. 安装mongodb,并配置数据库

# 获取mongodb
touch /etc/yum.repos.d/mongodb-org-3.4.repo
vi /etc/yum.repos.d/mongodb-org-3.4.repo

# 在mongodb-org-3.4.repo写入
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

# 用yum命令安装
sudo yum install -y mongodb-org

# 新建数据库储存文件夹
mkdir /data/db

# 启动mongod 
mongod

# 新建数据库easymockdb
mongo #进入数据库
> use easymockdb  #新建数据库

# 保持数据库运行
mkdir /data/logs
mkdir /data/logs/mongodb
mkdir /data/logs/mongodb/logs
mongod --fork --logpath=/data/logs/mongodb/logs/mogondb.log

出现以下信息就ok

about to fork child process, waiting until server is ready for connections.
forked process: 5500
child process started successfully, parent exiting

3. 部署项目

  1. 下载项目
# 直接去github上git clone
git clone https://github.com/easy-mock/easy-mock.git

# 如果github连不上,就直接下载下来放到某个文件夹下解压
# 然后npm install,如果github连不上,就去已经搭好的电脑拿node_module放过来再install
npm install
  1. 修改配置&build
cd  /usr/share/easy-mock-dev
cp config/default.json config/local.json
vi config/local.json  
db  ==>  mongodb://localhost:27017/easymockdb
jwt.secret  ==>  easy-mock-jwt-secret
APIPrefix  ==>  http://192.168.22.217:7300/api     
改好了之后就跑起
sudo npm run build
sudo npm run dev

这时出现下面信息就是跑起来了

DONE Compiled successfully in 476ms

5. 调试

跑起来后访问 http://192.168.22.217:7300/ 应当成功打开界面
但是此时却报错了

{"name":"easy-mock","hostname":"eles","pid":5546,"req_id":"5758ea3a-1a70-437a-ab4c-2f370f939bbd","level":50,"msg":"TypeError: Cannot read property 'data' of undefined\n at server-bundle.js:1782:16\n at process._tickCallback (internal/process/next_tick.js:109:7)","time":"2017-09-22T12:45:38.261Z","v":0}
(node:5546) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: connect ETIMEDOUT 193.168.22.217:7300

眼瞎瞎配置文件写错了ip地址,去项目地址/config/local.json改过来即可

6. 持续运行

使用PM2来保证项目的持续运行

sudo npm i -g pm2

sudo sudo NODE_ENV=dev pm2 start app.js

出现下列内容和表格则为运行成功

[PM2] Starting /usr/share/easy-mock-dev/app.js in fork_mode (1 instance)
[PM2] Done.

App name id mode pid status restart uptime cpu mem user watching
app 0 fork 8138 online 0 0s 0% 6.0MB root disabled

感想

由于跟nodejs不是很熟,所以出现错误也知道怎么调试,并且定位问题经常没有定位正确,所以走了好多弯路,但还是想把整个过程写下来向大家分享,万一也有别的小傻瓜跟我一样踩错坑了呢?

你可能感兴趣的:(从零开始的在Centos上搭建easy-mock服务)