当前位置: 首页 > 工具软件 > Parse Server > 使用案例 >

Parse Server(含Dashboard)部署于Centos7.6 64位

宋烨烁
2023-12-01

一、安装配置Nginx

1.1安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
复制代码

1.2安装 PCRE

PCRE 作用是让 Ngnix 支持 Rewrite 功能 

1、下载PCRE安装包

cd /usr/local/src复制代码

wget https://sourceforge.net/projects/pcre/files/pcre/8.43/pcre-8.43.tar.gz/复制代码

如若提示没有wget,则先安装wget

yum -y install wget复制代码

2、解压安装包

tar zxvf pcre-8.43.tar.gz复制代码

注意将文件名替换成你下载下来的安装包的文件名

3、进入安装包目录

cd perc-8.43复制代码

4、编译安装

./configure 复制代码

make && make install复制代码

5、查看PCRE版本

pcre-config –version复制代码

1.3安装nginx

1、下载Nginx

cd /usr/local/src 复制代码

wget http://nginx.org/download/nginx-1.15.9.tar.gz 复制代码

2、解压安装包

tar zxvf nginx-1.15.9.tar.gz 复制代码

3、进入安装包目录

cd nginx-1.15.9复制代码

4、编译安装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.43复制代码

make && make install复制代码

5、查看nginx版本

/usr/local/nginx/sbin/nginx -v复制代码

6、启动nginx

/usr/local/nginx/sbin/nginx复制代码

7、nginx其他命令

/usr/local/nginx/sbin/nginx -s reload     # 重新载入配置文件 
/usr/local/nginx/sbin/nginx -s reopen    # 重启 Nginx /usr/local/nginx/sbin/nginx -s stop         # 停止 Nginx
复制代码

二、安装MongoDB

2.1安装Vim及其依赖

yum install -y vim*复制代码

2.2.进入网址

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/复制代码

2.3创建文件

vim /etc/yum.repos.d/mongodb-org-4.0.repo复制代码

2.4.在mongodb-org-4.0.repo中键入以下内容

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc复制代码

2.5.安装配置

yum install -y mongodb-org复制代码

2.6.修改配置文件

vim /etc/mongod.conf复制代码

将net:bindIp: 127.0.0.1 改为 0.0.0.0

2.7.开放27017端口

1.SElinux

semanage port -a -t mongod_port_t -p tcp 27017复制代码

2.防火墙(先打开防火墙,然后打开27017端口,最后重新载入配置)

systemctl start firewalld复制代码
firewall-cmd --zone=public --add-port=27017/tcp --permanent复制代码

firewall-cmd --reload复制代码

2.8开启服务 mongod

service mongod start
#或者
systemctl start mongod复制代码

关闭 service mongod stop 或者 systemctl stop mongod

重启 service mongod restart 或者 systemctl restart mongod

2.9开机自启动

chkconfig mongod on
#或者
systemctl ennable mongod复制代码

2.10启动客户端

mongo --host 127.0.0.1:27017复制代码

三、配置MongoDB

3.1创建一个root用户,并赋予超级管理员权限(root);超级管理员可以管理MongoDB下的所有库以及权限、备份及集群等操作。

mongo --host 127.0.0.1:27017
use admin
db.createUser({user:"root",pwd:"root_password",roles:["root"]})复制代码

这里的root_password改成你自己想要的密码,最好设置的难一点

3.2为admin库创建一个admin用户,并赋予管理员权限。

use admin
db.createUser({user:"admin",pwd:"admin_password",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})复制代码

同样,这里的admin_password也改成你自己的密码

3.3新建一个数据库foo,给这个库添加一个用户admin,并赋予读写及管理员权限。

use foo
db.createUser({user:"admin", pwd:"admin_password", roles:["readWrite", "dbAdmin"]})
复制代码

foo可以改成任何你自己想要的数据库名

3.4认证创建的用户,此步骤非常重要。认证完成之后,退出shell。

db.auth('admin', 'admin_password')
1 # 1 - 表示认证成功,0 - 表示失败
exit
bye复制代码

四、下载安装最新版的Nodejs与npm

4.1在CentOS 7中安装Node.js 10 LTS的方法,使用NodeSource方式:

curl -sL https://rpm.nodesource.com/setup_10.x | bash -复制代码

上面命令将配置Node.js RPM存储库

4.2下面你需要做的就是安装nodejs包:

sudo yum clean all && sudo yum makecache fastsudo yum install -y gcc-c++ makesudo yum install -y nodejs复制代码

4.3通过以上命令安装的Node.js的版本为v10.13.0,运行以下命令查验:

node -v复制代码

4.4设置镜像源为淘宝镜像

npm config set registry https://registry.npm.taobao.org复制代码

4.5查看镜像源

npm config get registry复制代码

五、安装最新版git,wget组件

5.1什么是EPEL 及 Centos上安装EPEL

什么是EPEL 及 Centos上安装EPEL

RHEL以及他的衍生发行版如CentOS、Scientific Linux为了稳定,官方的rpm repository提供的rpm包往往是很滞后的,当然了,这样做这是无可厚非的,毕竟这是服务器版本,安全稳定是重点,官方的rpm repository提供的rpm包也不够丰富,很多时候需要自己编译那太辛苦了,而EPEL恰恰可以解决这两方面的问题。

5.2在CentOS 7 使用IUS第三方源安装git2

先输入git --version查看是否已经安装了git,如果已经安装了就先yum remove git 再执行下面的步骤

curl https://setup.ius.io | sh
yum install -y git2u
git --version复制代码

5.3安装wget

yum install -y wget复制代码

六、安装parse-server

6.1进入opt目录下

cd opt/复制代码

我个人喜欢把软件安装到这个目录,你要是不喜欢就跳下一步

6.2从GitHub下载源码

git clone https://github.com/ParsePlatform/parse-server-example.git复制代码

6.3进入项目文件夹并修改配置文件

cd parse-server-example/
vim index.js复制代码

6.4编辑index.js(经整合的index.js的文件内容如下)

// Example express application adding the parse-server module to expose Parse
// compatible API routes.

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var ParseDashboard = require('parse-dashboard');
var path = require('path');

var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;

//Parse服务的域名
var parseDomain = process.env.PARSE_DOMAIN;

if (!databaseUri) {
  console.log('DATABASE_URI not specified, falling back to localhost.');
}

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/meetwish',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'myAppId',
  masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
  serverURL: `http://localhost:1337/parse`,  // Don't forget to change to https if needed
  liveQuery: {
    classNames: ['Posts', 'Comments'] // List of classes to support for query subscriptions
  }
});

var dashboard = new ParseDashboard({
  'apps': [
    {
      'serverURL': `https://${parseDomain}/parse`,
      'appId': process.env.APP_ID || 'myAppId',
      'masterKey': process.env.MASTER_KEY || '',
      'appName': 'meetwish'
    }
  ],
  'users': [
    {
      'user': process.env.USER_NAME || 'username',
      'pass': process.env.PASSWORD || 'password'
    }
  ]
}, false);

// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

var app = express();

// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));

// make the Parse Dashboard available at /dashboard
app.use('/dashboard', dashboard);

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
  res.status(200).send('I dream of being a website.  Please star the parse-server repo on GitHub!');
});

// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
  res.sendFile(path.join(__dirname, '/public/test.html'));
});

var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
    console.log('parse-server running on port ' + port + '.');
});

// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);
复制代码

启动时需指定APP_ID,MASTER_KEY,USER_NAME,PASSWORD,PARSE_DOMAIN等环境变量。

  • APP_ID:应用的ID,所有的接口中都会用到该参数
  • MASTER_KEY:应用的密钥,需妥善保管,避免泄露,通过该key可以忽略所有的访问权限控制。
  • USER_NAME:访问Dashboard的用户名
  • PASSWORD:访问Dashboard的密码
  • PARSE_DOMAIN:部署Parse Server的域名或公网IP

6.5进入package.json中,添加parse-dashboard的依赖,版本自己去npm看

"parse-dashboard":"*"复制代码

6.6退出vim,开始安装依赖

npm install
npm audit fix #提示修复就修复一下,不提示就不管了复制代码

6.7然后安装mongodb的启动组件

npm install -g mongodb-runner复制代码

6.8启动项目

node index.js复制代码

七、配置nginx反向代理Parse Server及Parse Dashboard

7.1配置nginx.conf文件

假设Parse Server服务器的域名是:api.example.com

新版的nginx语法有些不一样

server {
    listen 443;
    server_name api.example.com;
    ssl on;
    ssl_certificate  cert/312431314412324.pem;
    ssl_certificate_key cert/312431314412324.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://127.0.0.1:1337;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
复制代码

7.2修改完配置以后,重启nginx

service nginx restart复制代码

八、开放服务端口

8.1开放80、443、1337、4040、27017端口

firewall-cmd --add-port=80/tcp --permanent
复制代码

firewall-cmd --add-port=443/tcp --permanent复制代码

firewall-cmd --add-port=1337/tcp --permanent复制代码

firewall-cmd --add-port=4040/tcp --permanent复制代码

firewall-cmd --add-port=27017/tcp --permanent复制代码

这步要是不做,你只能从localhost访问,之前我就被坑了,总之你最后只要保证服务运行且能在远程端口访问到就OK了

1.查看已打开的端口  # netstat -anp
2.查看想开的端口是否已开 # firewall-cmd --query-port=4040/tcp
  若此提示 FirewallD is not running 
  表示为不可知的防火墙 需要查看状态并开启防火墙

3. 查看防火墙状态  # systemctl status firewalld
 running 状态即防火墙已经开启
 dead 状态即防火墙未开启
4. 开启防火墙,# systemctl start firewalld  没有任何提示即开启成功
5. 开启防火墙 # service firewalld start  
   关闭防火墙 # systemctl stop firewalld
   centos7.3 上述方式可能无法开启,可以先#systemctl unmask firewalld.service 然后 # systemctl start firewalld.service

6. 查看想开的端口是否已开 # firewall-cmd --query-port=4040/tcp    提示no表示未开
7. 开永久端口号 firewall-cmd --add-port=4040/tcp --permanent   提示    success 表示成功
8. 重新载入配置  # firewall-cmd --reload    比如添加规则之后,需要执行此命令
9. 再次查看想开的端口是否已开  # firewall-cmd --query-port=4040/tcp  提示yes表示成功
10. 若移除端口 # firewall-cmd --permanent --remove-port=4040/tcp复制代码



转载于:https://juejin.im/post/5d47aa8ce51d45620d2cb88c

 类似资料: