# Node-Red 环境搭建
## 1. 使用docker启动nodered
```
docker pull nodered/node-red
docker run -it -p 1880:1880 --name mynodered nodered/node-red
```
-i 交互显示
-t 终端
-p 端口绑定
--name 设定容器的名称
至此,通过浏览器 http:\\\localhost:1880 进入nodered的编程界面了
## 2.给nodered增加权限管理
### 2.1 进入docker中的命令行
```
docker exec -it -u root mynodered /bin/bash
```
-u root 表示使用root权限登录
### 2.2 编辑nodered的设定文件,使能用户登录认证
```
bash-5.0# cd /data
bash-5.0# ls
lib package.json settings.js
bash-5.0# vi settings.js
```
```
// -----------------
// To password protect the Node-RED editor and admin API, the following
// property can be used. See http://nodered.org/docs/security.html for details.
adminAuth: {
type: "credentials",
users: [{
username: "admin",
password: "$2b$08$mIoT0ZrW8XwQ.x1Rf74bgunEyxO0kZgNOJ1bzApcgrT8YLczK9fPS",
permissions: "*"
}]
},
// To password protect the node-defined HTTP endpoints (httpNodeRoot), or
// the static content (httpStatic), the following properties can be used.
```
取消屏蔽 adminAuth部分内容。
Users 是一个数组,可以添加其他用户。password可以使用node-red-admin 加密生成。
Permissions:"*" 表示任意操作 如果是read表示只读。
退出并保存:wq
### 2.3 安装node-red-admin
```
npm install -g node-red-admin
```
如果出错可以尝试下面这条
```
npm install -g node-red-admin --unsafe-perm=true --allow-root --registry=http://registry.npm.taobao.org
```
使用node-red-admin生成密码的hash值
比如我输入密码:123321
```
bash-5.0# node-red-admin hash-pw
Password:
$2b$08$mIoT0ZrW8XwQ.x1Rf74bgunEyxO0kZgNOJ1bzApcgrT8YLczK9fPS
```
然后把生成的密码粘贴到setting.js的密码处。
```
adminAuth: {
type: "credentials",
users: [{
username: "admin",
password: "$2b$08$mIoT0ZrW8XwQ.x1Rf74bgunEyxO0kZgNOJ1bzApcgrT8YLczK9fPS",
permissions: "*"
}]
```
保存。
### 2.4 重启docker容器
<img src="https://img-blog.csdnimg.cn/20200305105804760.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdW1pYW9jbg==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述" style="zoom:50%;" />
输入admin 123321 登录即可。