ubuntu源码安装 Mosquitto 支持 Websockets

1.去 https://github.com/warmcat/libwebsockets/ 下载 libwebsockets-main.zip

2.去 http://mosquitto.org/files/source 下载 mosquitto-2.0.9.tar.gz

3.解压上面两个文件

4. 编译安装 libwebsockets

  cd libwebsockets/
  make
  clear
  mkdir build
  cd build
  cmake ..
  make
  sudo make install

6.从 https://github.com/DaveGamble/cJSON.git 下载 cJSON

7.编译安装cJSON

  cd cJSON/
  mkdir build
  cd build
  cmake ..
  sudo make install

8.进入 mosquitto-2.0.9 

修改 config.mk

找到 WITH_WEBSOCKETS:=no 把 no修改为 yes

9. 编译安装 mosquitto-2.0.9

cd mosquitto-2.0.9/
make
sudo make install

10 配置 config.conf

在/etc/mosquitto/conf.d目录下,新建config.conf配置文件
sudo vim /etc/mosquitto/conf.d/001_config.conf
编辑内容
allow_anonymous false
password_file /etc/mosquitto/pwfile.txt

11 创建用户

sudo mosquitto_passwd -c /etc/mosquitto/pwfile.txt user1
接着输入密码

12 配置 主配置文件 /etc/mosquitto/mosquitto.conf

#pid_file /var/run/mosquitto.pid
#persistence true
#persistence_location /var/lib/mosquitto/
#log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous false

port 1883
protocol mqtt
listener 9001
protocol websockets

13 运行

sudo /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

14 运行时出现错误 

/usr/local/sbin/mosquitto: error while loading shared libraries: libwebsockets.so.19: cannot open shared object file: No such file or directory,

需要在 /usr/lib文件夹添加一个符号链接

sudo ln -s /usr/local/lib/libwebsockets.so.19 /usr/lib/libwebsockets.so.19

15 为 mosquitto 一个服务设置

#! /bin/sh

### BEGIN INIT INFO
# Provides:		mosquitto
# Required-Start:	$remote_fs $syslog
# Required-Stop:	$remote_fs $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	mosquitto MQTT v3.1 message broker
# Description: 
#  This is a message broker that supports version 3.1 of the MQ Telemetry
#  Transport (MQTT) protocol.
#  
#  MQTT provides a method of carrying out messaging using a publish/subscribe
#  model. It is lightweight, both in terms of bandwidth usage and ease of
#  implementation. This makes it particularly useful at the edge of the network
#  where a sensor or other simple device may be implemented using an arduino for
#  example.
### END INIT INFO

set -e

PIDFILE=/var/run/mosquitto.pid
DAEMON=/usr/local/sbin/mosquitto

# /etc/init.d/mosquitto: start and stop the mosquitto MQTT message broker

test -x ${DAEMON} || exit 0

umask 022

. /lib/lsb/init-functions

# Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
  start)
	if init_is_upstart; then
	    exit 1
	fi
	log_daemon_msg "Starting network daemon:" "mosquitto"
	if start-stop-daemon --start --quiet --oknodo --background  --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;
  stop)
	if init_is_upstart; then
	    exit 0
	fi
	log_daemon_msg "Stopping network daemon:" "mosquitto"
	if start-stop-daemon --stop --quiet --oknodo --pidfile ${PIDFILE}; then
	    log_end_msg 0
	    rm -f ${PIDFILE}
	else
	    log_end_msg 1
	fi
	;;


  reload|force-reload)
	if init_is_upstart; then
	    exit 1
	fi
	log_daemon_msg "Reloading network daemon configuration:" "mosquitto"
        if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PIDFILE; then
            log_end_msg 0
        else
            log_end_msg 1
        fi	
	;;

  restart)
	if init_is_upstart; then
	    exit 1
	fi
	log_daemon_msg "Restarting network daemon:" "mosquitto"
	if start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile ${PIDFILE}; then
	    rm -f ${PIDFILE}
	fi
	if start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;

  try-restart)
	if init_is_upstart; then
	    exit 1
	fi
	log_daemon_msg "Restarting Mosquitto message broker" "mosquitto"
	set +e
	start-stop-daemon --stop --quiet --retry 30 --pidfile ${PIDFILE}
	RET="$?"
	set -e
	case $RET in
	    0)
		# old daemon stopped
		rm -f ${PIDFILE}
		if start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
		    log_end_msg 0
		else
		    log_end_msg 1
		fi
		;;
	    1)
		# daemon not running
		log_progress_msg "(not running)"
		log_end_msg 0
		;;
	    *)
		# failed to stop
		log_progress_msg "(failed to stop)"
		log_end_msg 1
		;;
	esac
	;;

  status)
	if init_is_upstart; then
	    exit 1
	fi
	status_of_proc -p ${PIDFILE} ${DAEMON} mosquitto && exit 0 || exit $?
	;;

  *)
	log_action_msg "Usage: /etc/init.d/mosquitto {start|stop|reload|force-reload|restart|try-restart|status}"
	exit 1
esac

exit 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值