# Google Cloud Platform VM個體使用 - 2 ### 一、透過SSH連線 1. 點選SSH進行連線,透過SSH連線僅能透過終端機輸入指令操作,後續會教如何藉由Google遠端桌面進行連線  2. 點選`Authorize`  3. SSH介面  ### 二、透過Google遠端桌面連線 1. 打開[Google遠端桌面](https://remotedesktop.google.com/access/?hl=zh-tw) 2. 點選`透過SSH設定`->`開始`  3. 點選`繼續`->`授權`   4. 複製`Linux`的部分  5. 將複製的指令貼至`SSH`  6. 即可在`遠端存取`中看到可遠端的電腦  ### 三、安裝Apache網頁伺服器 已下指令皆在SSH中輸入 1. 更新套件庫 ``` sudo apt update ``` 2. 安裝Apache ``` sudo apt install apache2 ``` 3. 啟動Apache ``` sudo systemctl status apache2 ``` 4. 設定開機啟動 ``` sudo systemctl enable apache2 ``` 5. 查看Apache伺服器狀態 ``` sudo systemctl status apache2 ``` ### 四、安裝MySQL 1. 更新套件庫 ``` sudo apt update ``` 2. 安裝MySQL ``` sudo apt install mysql-server ``` 3. 啟動MySQL ``` sudo systemctl status mysql ``` 4. 設定開機啟動 ``` sudo systemctl enable mysql ``` 5. 查看MySQL伺服器狀態 ``` sudo systemctl status mysql ``` 6. 重啟MySQL ``` sudo systemctl restart mysql ``` ### 五、設定MySQL 設定MySQL可對外連接 1. 在終端機輸入下方指令 ``` sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf ``` 2. 修正`bind-address` ``` #預設:bind-address = 127.0.0.1 bind-address = 0.0.0.0 ``` ### 六、安裝MQTT伺服器 1. 更新套件庫 ``` sudo apt update ``` 2. 安裝MQTT伺服器及測試軟體 ``` #mosquitto 是 MQTT 伺服器軟體 #mosquitto-clients 是用於測試 MQTT 伺服器的工具 sudo apt install mosquitto mosquitto-clients ``` 3. 啟動伺服器 ``` sudo systemctl start mosquitto ``` 4. 重啟伺服器 ``` sudo systemctl restart mosquitto ``` 5. 檢查伺服器狀態 ``` sudo systemctl status mosquitto ``` 3. 設定開機自動啟動Server ``` sudo systemctl enable mosquitto sudo systemctl start mosquitto ``` 4. Linux系統Mosquitto伺服器相關設定 ``` #各作業系統檔案位置 Linux:/etc/mosquitto/mosquitto.conf macOS:/usr/local/etc/mosquitto/mosquitto.conf Windows:C:\Program Files\mosquitto\mosquitto.conf ``` 5. 創建使用者(輸入完會要求輸入密碼) ``` sudo mosquitto_passwd -c /etc/mosquitto/passwd <username> ``` 6. mosquitto.conf配置選項 ``` 設置端口 listener 1883 默認是1883(非加密連線) ``` ``` 加密連線(尚未測試)listener 8883 cafile /path/to/ca.crt certfile /path/to/server.crt eyfile /path/to/server.key ``` 7. 配置密碼文件 ``` password_file /etc/mosquitto/passwd ``` 8. 允許匿名訂閱訊息 ``` allow_anonymous true ``` ### 七、開始使用MQTT 1. 發布訊息 `-h伺服器位置、-t發布的主題、-m訊息、-u使用者名稱、-P使用者密碼、-p prot` ``` mosquitto_pub -h <伺服器位置> -t "test/topic" -m "Hello MQTT" -u <username> -P <password> -p <prot> ``` 2. 訂閱訊息 `-h伺服器位置、-t訂閱的主題、-u使用者名稱、-p使用者密碼` ``` 有使用者訂閱 mosquitto_sub -h <伺服器位置> -t "test/topic" -u <username> -p <password> ``` ``` 匿名訂閱 mosquitto_sub -h <伺服器位置> -t "test/topic" ``` ### 八、啟用MQTT WebSocket 1. 修改 `mosquitto.conf` 設定 ``` listener 1883`指定MQTT的Port protocol mqtt`指定1883是給未加密的MQTT使用 listener 8083`指定加密MQTT的Port protocol websockets`啟用WebSocket服務 http_dir /var/www/html/mqtt/`指定Web的資料夾 ``` ### 九、PHP資料夾給予權限 1. 設定資料夾權限 ``` sudo chown -R ivm1110630:ivm1110630 /var/www/html/ ``` 2. 重啟伺服器 ``` sudo systemctl restart apache2 ``` ### 十、設定防火牆 1. 設定GCP防火牆 點擊左側->`虛擬私有雲網路`->`防火牆`->建立防火牆規則  2. 相關設定 | 項目 | 設定值 | 備註 | | --- | ----- | --- | | 名稱 | demo | 可自訂義 | |目標 |網路中的所有執行個體 | | |來源IPv4範圍 |0.0.0.0/0| | |TCP |3306 |資料庫專用通訊埠 |  --- 延伸閱讀 1. [Google Cloud Platform VM個體使用 - 1](https://hackmd.io/@0q3lEDkPQdaD6eZ8vpOC_A/S1jjJDdrJx) 2. [Google Cloud Platform VM個體使用 - 2](https://hackmd.io/@0q3lEDkPQdaD6eZ8vpOC_A/HkkKfOur1g) 3. [Arduino ESP32使用步驟](https://hackmd.io/@0q3lEDkPQdaD6eZ8vpOC_A/SJRUP4xBke) 4. [ESP32-WROOM-32 30P 全彩LED模組](https://hackmd.io/@0q3lEDkPQdaD6eZ8vpOC_A/r1u5SKxr1g) 5. [ESP32使用DHT11溫濕度辨識結合LED燈](https://hackmd.io/@0q3lEDkPQdaD6eZ8vpOC_A/Bke9M_zB1e) 6. [ESP32實作溫濕度智慧監測結合DHT11及LED開發](https://hackmd.io/@0q3lEDkPQdaD6eZ8vpOC_A/H10M6LOr1g)
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.