lnmp1.8 安装pureftp
根据lnmp官网的介绍 1.8版lnmp默认是不安装ftp的。需要自行安装。
在安装lnmp1.8解压后会看到目录下有这些文件:
1.输入命令。安装ftp。
./pureftpd.sh2.创建ftp账户及密码
lnmp ftp add
输入账号、密码、路径
username:xxxx
password:xxxx
/home/xxxx
3.OK
根据lnmp官网的介绍 1.8版lnmp默认是不安装ftp的。需要自行安装。
在安装lnmp1.8解压后会看到目录下有这些文件:
1.输入命令。安装ftp。
./pureftpd.sh2.创建ftp账户及密码
lnmp ftp add
输入账号、密码、路径
username:xxxx
password:xxxx
/home/xxxx
3.OK
禁止ping设置
临时:
echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all
永久:
echo net.ipv4.icmp_echo_ignore_all=1 >> /etc/sysctl.conf
sysctl -p # 执行这条命令使更改后的 /etc/sysctl.conf 配置文件生效
注意:如果 /etc/sysctl.conf 配置文件里已经有 net.ipv4.icmp_echo_ignore_all 字段了,那么直接用 vim 进去更改对应的值即可。
please install the universal CRT first,you can install it via window-update(kb2999226) or you can manually download it from Miscrosoft by doing a search for credist 2019 download

点击下载☞
VC_redist.x64.rar
github仓库地址是:https://github.com/mqttjs/MQTT.js
websocket客户端连接需要用到一个js库:mqtt.js
GitHub地址:https://github.com/mqttjs/MQTT.js
下面填写配置连接MQTT的信息
//MQTT连接的配置
options: {
protocolVersion:4, //MQTT连接协议版本
clientId: 'miniTest',
clean: false,
password: '123456',
username: 'socketer',
reconnectPeriod: 10000, //10000毫秒,两次重新连接之间的间隔
connectTimeout: 30 * 1000, //1000毫秒,两次重新连接之间的间隔
resubscribe: false //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true)
}
连接MQTT实例
onClick_connect: function() {
var that = this;
//开始连接
this.data.client = mqtt.connect(host, this.data.options);
this.data.client.on('connect', function(connack) {
wx.showToast({
title: '连接成功'
})
})
//服务器下发消息的回调
that.data.client.on("message", function(topic, payload) {
console.log(" 收到 topic:" + topic + " , payload :" + payload)
wx.showModal({
content: " 收到topic:[" + topic + "], payload :[" + payload + "]",
showCancel: false,
});
})
//服务器连接异常的回调
that.data.client.on("error", function(error) {
console.log(" 服务器 error 的回调" + error)
})
//服务器重连连接异常的回调
that.data.client.on("reconnect", function() {
console.log(" 服务器 reconnect的回调")
})
//服务器连接异常的回调
that.data.client.on("offline", function(errr) {
console.log(" 服务器offline的回调")
})
},
订阅主题(单个)
onClick_SubOne: function() {
if (this.data.client && this.data.client.connected) {
//仅订阅单个主题
this.data.client.subscribe('Topic0', function(err, granted) {
if (!err) {
wx.showToast({
title: '订阅主题成功'
})
} else {
wx.showToast({
title: '订阅主题失败',
icon: 'fail',
duration: 2000
})
}
})
} else {
wx.showToast({
title: '请先连接服务器',
icon: 'none',
duration: 2000
})
}
},
订阅主题(多个)
onClick_SubMany: function() {
if (this.data.client && this.data.client.connected) {
//仅订阅多个主题
this.data.client.subscribe({
'Topic1': {
qos: 0
},
'Topic2': {
qos: 1
}
}, function(err, granted) {
if (!err) {
wx.showToast({
title: '订阅多主题成功'
})
} else {
wx.showToast({
title: '订阅多主题失败',
icon: 'fail',
duration: 2000
})
}
})
} else {
wx.showToast({
title: '请先连接服务器',
icon: 'none',
duration: 2000
})
}
},
发布消息
onClick_PubMsg: function() {
if (this.data.client && this.data.client.connected) {
this.data.client.publish('Topic0', 'i am a boy');
wx.showToast({
title: '发布成功'
})
} else {
wx.showToast({
title: '请先连接服务器',
icon: 'none',
duration: 2000
})
}
},
取消订阅(单个)
onClick_unSubOne: function() {
if (this.data.client && this.data.client.connected) {
this.data.client.unsubscribe('Topic1');
} else {
wx.showToast({
title: '请先连接服务器',
icon: 'none',
duration: 2000
})
}
},
取消订阅(多个)
onClick_unSubMany: function() {
if (this.data.client && this.data.client.connected) {
this.data.client.unsubscribe(['Topic1', 'Topic2']);
} else {
wx.showToast({
title: '请先连接服务器',
icon: 'none',
duration: 2000
})
}
},
本实例的mosquitto文件位于/etc/mosquitto
1.打开配置文件编辑,关于用户认证的方式和读取的配置都在这个文件中进行配置。
vi /etc/mosquitto/mosquitto.conf
【i】编辑模式
#允许匿名 我这里是将#注释去掉 将ture改为false
allow_anonymous false
#password_file 这个位置将#注释 密码文件为pwfile
password_file /etc/mosquitto/pwfile
#acl_file 这个位置将#注释 然后权限文件为acl
acl_file /etc/mosquitto/acl
2.创建pwfile文件
命令:通过以下命令可以创建pwfile文件 并输入相应的密码,见下图
mosquitto_passwd -c /etc/mosquitto/pwfile admin

3.查看pwfile文件,可看到admin:xxxx的文件 则可说明创建成功并可用。
cat pwfile
4.alc文件的创建 基于mosquitto下有个aclfile.example的示例文件。这里就复制一份重命名。
命令如下:
cp aclfile.example acl
5.查看alc文件 可见里面有相应的配置说明及注释
cat alc
一、mosquito.conf开启相应的端口和协议
本示例的目录:mosquito.conf位于/etc/mosquito/mosquito.conf下。
命令:
vi /etc/mosquito/mosquito.conf
【i】 进入编辑模式
添加以下代码,如果有则把#注释去掉
#MQTT协议
port 1883
protocol mqtt
#websockets协议
listener 8083
protocol websockets
#如果需要查看websockets日志还可以加入以下面,上面有提过。
log_type websockets
websockets_log_level 0【ESC】退出编辑模式
输入:wq 保存退出
△这里是很关键的一步。
二、Nginx 配置socket转发
本实例是基于Lnmp下的nginx来进行配置,示例网站为iot.xx.com
我要实现是
iot.xx.com/mqtt 进行转发。
我的vhost配置文件在/usr/local/nginx/conf/vhost/iot.xx.com.conf
server{
}
在这上面添加以下代码
map $http_upgrade $connection_upgrade{
default upgrade;
'' close;
}
没有添加以上代码,下面引用时候就会报$connection_upgrade错误
然后在server{}里面包含位置添加转发代码
#socket转发
location /mqtt {
proxy_pass http://127.0.0.1:8083/;
proxy_http_version 1.1;
proxy_redirect off;
proxy_buffering off;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Host $host;
proxy_set_header Http-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
在这里我踩了不少坑。都无法进行访问。搜索百度也无法解决。最后在一篇攻略中受到启发。并记录下来。
重启服务Nginx
lnmp nginx reload
OK 完成以上几步就可以轻松进行测试了。
这里推荐两个socket在线测试平台
A、jsons http://www.jsons.cn/websocket/
B、yunjson http://www.yunjson.com/websocket/