[TOC]
# 我的阿里云服務器
18年10月底個人購買了一臺阿里云服務器實例,配置如下:
* CPU:2核
* 內存:4GB
* 操作系統:Ubuntu 16.04 64位
* 公網IP:我知道
* 私有IP:只有我知道
## MQTT服務的搭建
我打算在阿里云服務器上安裝的MQTT服務器是[EMQTT](http://emqtt.com/) ,別稱*EMQ*.據說是國內的一個MQTT開源項目,號稱百萬級分布式開源物聯網MQTT消息服務器,我先信了,試試去!
### 1.獲取EMQ下載地址
在本地電腦登錄[EMQ的官網下載頁面](http://www.emqtt.com/downloads),我選當前最新的[穩定版:emqtt-2.3.11發布于2018/07/23之Ubuntu16.04(64-bit)](https://www.emqx.io/static/brokers/emqttd-ubuntu16.04-v2.3.11.zip)點擊下載.不用下載完,可以暫停,這里主要是為了獲取該版本程序的下載地址,獲取方法是
打開瀏覽器的下載列表,復制該版本程序的地址,以備云服務器安裝emq是使用.
### 2.下載安裝EMQ
1. 通過SSH遠程登錄云主機;
2. 用wget工具下載EMQ安裝文件
```
wget xxx/emqttd-ubuntu16.04-v2.3.11.zip
```
注:這里的xxx/emqttd-ubuntu16.04-v2.3.11.zip是上節"1.下載EMQ"里獲取的地址
3. 解壓縮下載的zip文件
```
unzip emqttd-ubuntu16.04-v2.3.11.zip
```
emq文件解壓到目錄emqttd里,如下:
```
root@iZm5e0mttckgaqhv4a4vu4Z:/home/qinbao/emqtt/emqttd# ls
bin data erts-9.0 etc hook_lua lib log releases
```
> 注: Ubuntu系統一般都沒有預裝unzip解壓工具,在輸入unzip命令時系統會友好的提示你該如何安裝,按提示安裝就是了,如下:
>```
>sudo apt-get install unzip
>```
### 3. 運行EMQ之MQTT服務器 ###
在emqttd目錄下輸入命令* ./bin/emqttd console *啟動MQTT服務器
```
root@iZm5e0mttckgaqhv4a4vu4Z:/home/qinbao/emqtt/emqttd# ./bin/emqttd console
... 此處省略一萬字 ...
starting emqttd on node 'emq@127.0.0.1'
emqttd ctl is starting...[ok]
emqttd hook is starting...[ok]
emqttd router is starting...[ok]
emqttd pubsub is starting...[ok]
emqttd stats is starting...[ok]
emqttd metrics is starting...[ok]
emqttd pooler is starting...[ok]
emqttd trace is starting...[ok]
emqttd client manager is starting...[ok]
emqttd session manager is starting...[ok]
emqttd session supervisor is starting...[ok]
emqttd wsclient supervisor is starting...[ok]
emqttd broker is starting...[ok]
emqttd alarm is starting...[ok]
emqttd mod supervisor is starting...[ok]
emqttd bridge supervisor is starting...[ok]
emqttd access control is starting...[ok]
emqttd system monitor is starting...[ok]
emqttd 2.3.11 is running now
Eshell V9.0 (abort with ^G)
(emq@127.0.0.1)1> Load emq_mod_presence module successfully.
dashboard:http listen on 0.0.0.0:18083 with 4 acceptors.
mqtt:tcp listen on 127.0.0.1:11883 with 4 acceptors.
mqtt:tcp listen on 0.0.0.0:1883 with 16 acceptors.
mqtt:ws listen on 0.0.0.0:8083 with 4 acceptors.
mqtt:ssl listen on 0.0.0.0:8883 with 16 acceptors.
mqtt:wss listen on 0.0.0.0:8084 with 4 acceptors.
mqtt:api listen on 0.0.0.0:8080 with 4 acceptors.
(emq@127.0.0.1)1>
```
如上顯示MQTT服務器啟動成功
服務器工作后,會監聽四個端口,1883,8883,8083,18083,對應的服務參閱下表
| 端口號 | 服務類型
|-|-
1883 | MQTT協議端口
8883 | MQTT(SSL)端口
8083 | MQTT(WebSocket), HTTP API端口
18083 | Dashboard管理控制臺端口
### 配置系統環境變量 for emqttd ###
把命令emqttd添加到系統中,須設置系統環境變量:
```
> vim /etc/profile
# emqtt
export EMQPATH=/usr/local/emqttd
export PATH=$EMQPATH/bin:$PATH
> source /etc/profile
```
> 注意,EMQPATH須根據emqttd實際的安裝位置確定
### 4. 配置云服務器的端口1883 ###
由于MQTT服務器用的端口號是1883,須打開云服務器相應的端口1883,方法如下:
來到阿里云的管理控制臺,給我的云服務器實例*添加安全組規則*(入方向),規則設置如下:
| 授權策略 | 協議類型 | 端口范圍 | 授權類型 | 授權對象 | 描述 | 優先級 | 創建時間 | 操作|
|-|-|-|-|-|-|-|-|-|
| 允許 | 自定義TCP|1883/1883|地址段訪問|0.0.0.0/0|mqtt|1|xxx|xxx
### 4. 測試MQTT服務器 ###
本地電腦(windows系統)安裝MQTT客戶端軟件MQTT.fx-1.5.0,設置連接參數:
* Broker Address:云服務器的公網IP
* Broker Port:1883
設置以上兩個參數就行了,然后連接,連接成功后就可以發布和訂閱主題了.
發布主題:如主題名為topic1,內容為hello world!
訂閱主題:如主題為topic1,如果以上的主題topic1發布成功,在此能接受到該主題的內容* hello world! *
接收到主題為topic1,內容是hello world!的消息后,說明MQTT服務器成功搭建并運行;
###linux(ubuntu)下mqtt測試
安裝http://mosquitto.org/download/
1. 安裝mosquitto
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
2. 安裝mosquitto客戶端
```
qb@qb-Lenovo-XiaoXin-310-15IKB:~$ mosquitto_pub
The program 'mosquitto_pub' is currently not installed. You can install it by typing:
sudo apt install mosquitto-clients
qb@qb-Lenovo-XiaoXin-310-15IKB:~$ sudo apt^C
qb@qb-Lenovo-XiaoXin-310-15IKB:~$
```
## FTP服務器的搭建 ##
vsftpd是linux下的一款小巧安全易用、在各個linux發行版本中最受推崇的的FTP服務器軟件.
### FTP服務器安裝配置過程 ###
安裝過程別人寫的很詳細了,直接參考[FTP的搭建和使用](https://help.aliyun.com/knowledge_detail/60152.html)就行了.大概的過程如下:
1. apt install vsftpd,其實在shell中輸入vsftpd,沒有安裝的話會提示怎么安裝,按提示安裝就行了;
2. 配置文件* /etc/vsftpd.conf *,刪除了不必要的注釋,內容如下.配置FTP服務器能匿名訪問,不能上傳和新建文件夾,FTP的工作模式是被動模式,即FTP服務器(端口號21)與客戶端的命令通道建立之后,客戶端發送被動連接命令,此時FTP服務器發給客戶端一個用于數據通道的端口范圍,由pasv_min_port和pasv_max_port確定FTP服務器用于數據通道的端口地址:
3. 配置SSL/TLS安全FTP
終于來到重點了。首先我們了解一下FTPS的工作方式。
FTPS工作方式
為了提高整體安全性,我們有兩種選擇,一種是FTPS,相當于加密版的FTP。第二種是SFTP,也就是通過SSH文件傳輸協議加密從客戶機到服務器的FTP連接。
SSL/TLS協議工作在傳輸層(TCP/IP)之上,應用層之下。因此它可以很容易的在諸如HTTP、POP3、IMAP4、SMTP和FTP等應用層協議上來實現。
SSL安全擴展至少有兩種不同的初始化方法,一種是顯式安全,另一種是隱式安全。
顯示安全:為了建立SSL連接,顯式安全要求FTP客戶端在和FTP服務器建立連接之后發送一個特定命令,也就是AUTH SSL或者AUTH TLS,顯式的告訴FTP服務器初始化對應的安全連接。這時默認的FTP端口就是21端口。
隱式安全:當FTP客戶端連接到FTP時,隱式安全將會自動和SSL連接并自動建立安全。
配置SSL加密FTP數據傳輸
首先我們來查看Vsftpd是否支持SSL。
```
[root@localhost ~]# ldd `which vsftpd` | grep libssl
libssl.so.10 => /usr/lib/libssl.so.10 (0x008cd000)
```
如果輸出有 libssl.so.10 => /usr/lib/libssl.so.10 (0x008cd000) 類似的一行,那么Vsftpd就是支持SSL的。
接下來我們使用Openssl生成證書。
生成頂級CA的公鑰和私鑰文件,有效期10年(RSA 1024bits)。
```
[root@localhost ~]# openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout /etc/ssl/private/ssl-cert-snakeoil.key -out /etc/ssl/certs/ssl-cert-snakeoil.pem
Generating a 1024 bit RSA private key
...............................................................++++++
...........++++++
writing new private key to '/etc/vsftpd/vsftpd.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]: #國家
State or Province Name (full name) []: #省份
Locality Name (eg, city) [Default City]: #城市
Organization Name (eg, company) [Default Company Ltd]: #單位名稱
Organizational Unit Name (eg, section) []: #部門
Common Name (eg, your name or your server's hostname) []:192.168.159.136 #域名/主機名/IP地址
Email Address []: #郵件地址
```
需要注意的是,域名/主機名/IP地址這塊需要填寫你主機的真實信息。
編輯Vsftpd的配置文件( /etc/vsftpd/vsftpd.conf),添加SSL支持。
```
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
# 添加如下內容。
ssl_enable=YES #啟用SSL支持
force_local_logins_ssl=YES #對本地用戶登錄啟用ssl加密
force_local_data_ssl=YES #對本地用戶數據傳輸啟用ssl加密
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem #RSA證書保存位置
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key #RSA密鑰文件保存位置
ssl_tlsv1=YES #啟用TLS V1加密
ssl_ciphers=HIGH #啟用高級加密方式,若不啟用此項,會導致Filezilla報錯
pam_service_name=vsftpd #不加這個linux下lftp無法連接
```
4. 本地用戶登錄所做的配置
在ftp服務端,添加一個用戶,并為該用戶創建本地的home工作目錄,
```
useradd -m test #添加用戶test,并創建其home工作目錄/home/test
passwd test #為用戶test設置密碼
```
從2.3.5之后,vsftpd增強了安全檢查,如果用戶被限定在了其主目錄下,則該用戶的主目錄不能再具有寫權限了!如果檢查發現還有寫權限,就會報錯。
?要修復這個錯誤,可以用命令chmod a-w /home/test去除用戶主目錄的寫權限,注意把目錄替換成你自己的。或者你可以在vsftpd的配置文件中增加下列兩項中的一項:
allow_writeable_chroot=YES
> 特別注意,用本地用戶訪問FTP服務器時,一定要把/home/test的文件夾設置為只讀!!!
5. 我的配置文件vsftpd.conf,實現匿名用戶和本地用戶的登錄,匿名用戶以明文方式傳輸,本地用戶登錄以tls加密傳輸.用瀏覽器,filezilla訪問沒問題,智服云的203C模塊用于通過FTA服務器FOTA遠程升級沒問題(須以明文方式訪問升級),以供參考
```
# Example config file /etc/vsftpd.conf
listen=YES #這里是YES,須注釋掉listen_ipv6
#listen_ipv6=YES
#
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
#write_enable=YES
#
local_umask=022
#
#anon_upload_enable=YES
#
#anon_mkdir_write_enable=YES
#
#dirmessage_enable=YES
#
use_localtime=YES
#
#xferlog_enable=YES
#
#passive mode set by qinbao
pasv_enable=YES
pasv_min_port=4460
pasv_max_port=4463
pasv_promiscuous=YES
pasv_address=這里填寫FTP服務器的IP地址
# Make sure PORT transfer connections originate from port 20 (ftp-data).
#port_enable=YES
#connect_from_port_20=YES
#
#chown_uploads=YES
#chown_username=whoever
#
#xferlog_file=/var/log/vsftpd.log
#
#xferlog_std_format=YES
#
#idle_session_timeout=600
#
#data_connection_timeout=120
#
#nopriv_user=ftpsecure
#
#async_abor_enable=YES
#
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
#ftpd_banner=Welcome to blah FTP service.
#
#deny_email_enable=YES
#banned_email_file=/etc/vsftpd.banned_emails
#
#這里chroot_local_user=NO,若chroot_list_enable=YES,則文件chroot_list_file里的用戶名不能切換至根目錄
chroot_local_user=NO
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
#
#ls_recurse_enable=YES
#
#secure_chroot_dir=/var/run/vsftpd/empty
#
pam_service_name=vsftpd
#
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=YES
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
require_ssl_reuse=NO
ssl_ciphers=HIGH
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
#utf8_filesystem=YES
```
> * 特別注意,一定要在云服務器上打開FTP所用的端口(端口20,21,被動模式下用于數據通道的pasv_min_port和pasv_max_port之間的端口),配置相應端口的**安全組規則 **.
> * 還有,匿名用戶的共享文件夾位于*/svr/ftp/pub*,其實匿名用戶也是有用戶名的,叫ftp,無密碼.
### FTP服務器的測試 ###
FTP服務器可以用匿名和本地用戶登錄,測試軟件可以用瀏覽器或filezilla(linux和windows平臺都有).
1. 匿名登錄FTP服務器
* 瀏覽器輸入*ftp://xxx.xxx.xxx.xxx*,正常的話可以看到相應的文件夾,即服務器端/svr/ftp/pub下的內容.
這里的匿名用戶其實也是有用戶名的,是ftp對應的用戶組是ftp,在linux的用戶組和用戶那里(* /etc/passwd和/etc/group */)可以查到.
* filezilla更簡單.
2. 本地用戶登錄FTP服務器
目前我理解本地用戶就是FTP服務器的本地用戶.
用戶名和密碼為:test、test
服務器IP和port為:192.168.0.1:21
登錄后轉到profile文件夾,應該設置為只能訪問該用戶的home目錄(如/home/test),不允許訪問其它的目錄,以保證FTP服務器系統安全
加密方式為tls
* 通過軟件工具filezilla訪問ftp服務器
需要設置的項目:Host(ftp服務器ip),protocol(FTP),Encryption(Use explicit FTP over TLS if available),Login Type(Normal,即本地用戶方式),用戶名和密碼.
以上設置后,連接登錄即可.
* 瀏覽器FTP地址格式為:ftp://用戶名:密碼@地址:端口/ (如果默認端口是21則不需要填寫端口)
如1: ftp://test:test@192.168.0.1:21/profile
如2: ftp://192.168.0.1:21,若FTP服務器只支持本地用戶登錄,此時會彈出輸入用戶名和密碼的對話框,輸入即可訪問
> 實踐證明以上通過瀏覽器本地用戶的方法不能訪問ftp瀏覽器,可能是瀏覽器不支持tls的ftp訪問.
## 參考文章 ##
* [起始篇-簡介與MQTT服務器](https://blog.csdn.net/relijin/article/details/73274739)
* [初識emq](https://blog.csdn.net/qq_38250168/article/details/80848563)
* [EMQ百萬級MQTT消息服務(介紹和搭建)](https://blog.csdn.net/u011142688/article/details/79852284)
* [EMQ官方文檔](http://emqtt.com/docs/v2/install.html)
* [如何安裝和配置vsftpd與TLS在Debian 8(Jessie)](https://www.howtoing.com/setting-up-vsftpd-tls-on-debian-8-jessie/)
* [史上最詳細的vsftpd配置文件講解](https://blog.csdn.net/weiyuefei/article/details/51564367)