# Google Cloud Platform 架站轉移教學
**contributed by<`任品謙`、`李承儒`>**
:::info
以下在需要代換的部份會用 [代換部份] 來表示,記得整個代換掉不要留下 [ ] <-方括號的部份
:::
---
### 建立虛擬機
建立虛擬機可以參考這篇[【教學】Google Cloud Platform 申請試用,並建立免費專案!](https://izo.tw/gcp-apply/),裡面有很詳細步驟說明,按照步驟就可以建好虛擬機。
---
### 安裝 lamp
所謂的LAMP分別是四個東西的組合
L = linux
A = apache
M = mySQL
P = php
參考這篇[【教學】Google Cloud Platform 下,部署VM環境 Ubuntu16.04 + LAMP
](https://izo.tw/gcp-ubuntu/),這裡面詳細的介紹如何在Google Cloud Platform下完成LAMP的建置
:::info
記得連線時用 http 不要加 s,沒有 ssl 驗證會連不上
:::
---
### 安裝 phpmyadmin
接著我們要安裝phpmyadmin,安裝指令
sudo apt-get install phpmyadmin
如果phpmyadmin 一開始裝錯地方要自己做連結
在Terminal中輸入
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
就會在你的 `/var/www/html/` 裡面新增一個跳轉捷徑
然後就可以開啟瀏覽器在網址列打上
http://[Your IP]/phpmyadmin/index.php
測試看看有沒有沒有辦法連上php
---
### 與伺服器建立 ftp 連線
現在要讓我們可以用 ftp 來連 server 傳檔案
可以參考[【教學】Google Cloud Platform 下,使用 FileZilla 進行 SSH 連線](https://izo.tw/gcp-ssh-filezilla/),裡面詳細的教你如何與伺服器建立 ftp 連線
> 如果看不懂沒關係我幫你列在下面
> [name=任品謙]
:::warning
以下指令都是以 root 權限執行!!!
以下指令都是以 root 權限執行!!!
以下指令都是以 root 權限執行!!!
:::
首先要在你的 server 上下載 google sdk
curl https://sdk.cloud.google.com | bash
執行 google sdk
exec -l $SHELL
然後重開 apache
sudo service apache2 restart
初始化 google sdk
gcloud init
接著就會跑出以下訊息
```
Choose the account you would like to use to perform operations for
this configuration:
[1] 291592864927-compute@developer.gserviceaccount.com
[2] Log in with a new account
Please enter your numeric choice:
```
輸入`2`之後會出現
```
You are running on a Google Compute Engine virtual machine.
It is recommended that you use service accounts for authentication.
You can run:
$ gcloud config set account `ACCOUNT`
to switch accounts if necessary.
Your credentials may be visible to others with access to this
virtual machine. Are you sure you want to authenticate with
your personal account?
Do you want to continue (Y/n)?
```
接著輸入`Y` ,就會跑出一串像下面這樣的訊息
```
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?redirect_uri=xxx%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&prompt=select_account&response_type=code&client_id=xxxxxxxxxxx.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&access_type=offline
Enter verification code:
```
把中間那串網址複製出來(選取之後就會自動複製),然後會要你登入**你建立這個 Google cloud** 用的帳號密碼,登入完會跳出一串授權碼,複製貼回來然後` enter`
接著就會要你選擇區域
```
Which Google Compute Engine zone would you like to use as project
default?
If you do not specify a zone via a command line flag while working
with Compute Engine resources, the default is assumed.
[1] us-east1-b
[2] us-east1-c
[3] us-east1-d
[4] us-east4-c
[5] us-east4-b
[6] us-east4-a
[7] us-central1-c
[8] us-central1-a
[9] us-central1-f
[10] us-central1-b
[11] us-west1-b
[12] us-west1-c
[13] us-west1-a
[14] europe-west4-a
[15] europe-west4-b
[16] europe-west4-c
[17] europe-west1-b
[18] europe-west1-d
[19] europe-west1-c
[20] europe-west3-b
[21] europe-west3-c
[22] europe-west3-a
[23] europe-west2-c
[24] europe-west2-b
[25] europe-west2-a
[26] asia-east1-b
[27] asia-east1-a
[28] asia-east1-c
[29] asia-southeast1-b
[30] asia-southeast1-a
[31] asia-southeast1-c
[32] asia-northeast1-b
[33] asia-northeast1-c
[34] asia-northeast1-a
[35] asia-south1-c
[36] asia-south1-b
[37] asia-south1-a
[38] australia-southeast1-b
[39] australia-southeast1-c
[40] australia-southeast1-a
[41] southamerica-east1-b
[42] southamerica-east1-c
[43] southamerica-east1-a
[44] europe-north1-a
[45] europe-north1-b
[46] europe-north1-c
[47] northamerica-northeast1-a
[48] northamerica-northeast1-b
[49] northamerica-northeast1-c
[50] Do not set default zone
Please enter numeric choice or text value (must exactly match list
item):
```
輸入對應代號,這樣就初始化完畢了,接著要用 ssh 在 root 權限下自己連線自己來自動生成連線用的private 和 public key
gcloud compute ssh root@[lamp-tutorial]
:::success
[lamp-tutorial] 請自行代換為你的 instance 名稱
:::
如果不確定自己的 instance 的名稱可以輸入下面的指令,就會列出你的 instance 名稱
gcloud compute instances list
如果順利的完成上述步驟了他就會開始自己連線自己
```
WARNING: The public SSH key file for gcloud does not exist.
WARNING: The private SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: SSH keygen will be executed to generate a key.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/google_compute_engine.
Your public key has been saved in /root/.ssh/google_compute_engine.pub.
The key fingerprint is:
SHA256:41GE+e8cMU4W6T8hzI7QdFZhNHUDF/SppXi9ugaX9Ik root@lamp-tutorial
The key's randomart image is:
+---[RSA 2048]----+
| o. =OB+|
| o.. =...=|
| +.* . o.|
| ..o @.* |
| S. O.O+o.|
| . o..*Eoo.|
| . oo. o |
| o.. |
| .o. |
+----[SHA256]-----+
Updating project ssh metadata.../Updated [https://www.googleapis.com/compute/v1/projects/my-project-1501655585463].
Updating project ssh metadata...done.
Waiting for SSH key to propagate.
Warning: Permanently added 'compute.25962473954298485' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-1019-gcp x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
0 packages can be updated.
0 updates are security updates.
```
中間會出現要不要輸入亂數種子之類的問題,可以不用管他直接用預設值就行,看到 WARNING 也是正常的,這樣他才會生出 key 來,看到最下面有
`Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-1019-gcp x86_64)
就是成功自己連線自己了,這個時候輸入
cat ~/.ssh/google_compute_engine
就可以看到連線用的 private key ,把它整個複製下來,格式應該會類似下面這樣
```
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuQMml4nMZxao+FEkisj8aj3kCXgbdBSJAEvepWGE1K1kZi+G
7LgNXnIJFNKYRHNtCDfWz5/zck+ro6qi3iN0kUMqdgq4/4JDDMMMXTuCBHSnJKoy
+XC6xqJ3o6p72Q+XbXxoqoDF+1kc720wUPWdsocQ96OOAgT8wBilab2ifJ0cYkIs
BopuykBTRD5LRGiiY3sYZdpiL+4mziQc9/3nB84U+axXYfrT9yYrWEHTm+/wemUK
W1sXO6RJUsFNlETIEDNCNXwrSEoyzfKafjPC86dRWiq3081Zs82/EC+dgCFVgdZd
ha4zw2qdI9CpimPaDiiZCACRGqqyOlBNSpklKQIDAQABAoIBAGGpkx6dRAQLBykF
lFeanvvqLcz91OgiyusYIbCFWNv3p671WbDtu0jmw3p1bKF9mMmkyEUTdywZeE5O
DelbJ/ZiUTC8mkluVryO3aD9f7WganbFTWE1K7MdIjCIFxZOz62g5ZNTpK74rHiK
1910ukj6WW+Pq6/POCijGfZGf1Ztpp35PmJfXFdNVcHY0bn1gvhPm3CBOOyAAwmG
nNhz2kdv2kPdynOQEtfMzjNBG4cph8xuStcZlQhkTJ/AflkIaEks7HaZE2r2Prxu
r3djaopfY6tJ12UX2h7VN2Su9QGLv93HxSSeO4mMdrJ6vdYrYtkHX59mLgif0IE5
nkW2dP0CgYEA6nX0EXBaZ9v51mWM7f7eU/7UK5S37D1NFNOJRx8DtOQTKkCboG3b
PwDooZ1oBmKBKqvyYyqi6XIU73/o/k1lLwD/jsFrO1inXXBgm+EYdQueMnNY9i1P
99mXxUQSczWWEct4NxIdYpoXyTqNGoJgIcRJDusA9KFkfwoXDG2q6ecCgYEAygJE
4YOUJmez8mTbPHgu+h3xXmPVPzKnWUm0gL2dW8i9ZedpGMzPpwTC+SZiUR5PxHTM
HMHZz/uom04Y1/TtuNKpZjd09jX/N1mjSBJ0keupf6w1UTWLBq4tyi/ggMUvQDzm
oJs4E30L0P78nu1OfQyXxxj/IITsPP4zBr/BNm8CgYEAwwS5roe1qw3eEecGAjAs
3dDoyhc5pNaYduo9FVtGyf+rsUlsKVU3OICY/1TeYfEtX0EGrFWajAhQQaYzNtNJ
hn9eEm/vsb1b+OywbvgcT78KJwyqwpmOGqpzAzJL5mh8GvETXTtEFe/2OPrqw1IO
hAewfSgb818LKGBMK5pMv6ECgYBOKAqWZd4P7deFVNgt0SH7y0+hOWbsCKEOvqGD
ndkgBc6heqMC4VsF96Wn1936HIwoKjg+eBHZkjhtvg2GoFDJTVgVICe0irevppKx
89H+EvuFVg9a2Cht8T3uGA/4QlJBvE5OnT9Y6uyDc+QxaQcRAqpGcbljz4nHH2KG
nfIXKQKBgQCc62N+oB6PFNtaBqyMR4qznE5i9SICu1Wo8dSUK0CtA4KL4c//YdVP
U+UgyFIxzHq+RBdeF1LAXmKLbJyKiEEK6tjmDA6J75gW/aqkHx8kqoLhSsVQucRE
Add0sECQ16rK+WqHbNNxsHwSo3ARNLIWAZOz+xLm4e0gW5gYYPGjbQ==
-----END RSA PRIVATE KEY-----
```
其中 private key 也包含
-----BEGIN RSA PRIVATE KEY-----
這個部分不要忘記了
最後我們另外開一個空白檔案把 priviate Key 貼過去,並且儲存成 rootKey.ppk ,完成後就可以用來連線
---
#### 連線方式
首先要去[Filezilla](https://filezilla-project.org/)中下載並安裝 FileZilla
![Filezilla](https://i.imgur.com/MpV0n7B.png)
完成安裝後,打開 FileZilla並點選建立新連線 → 輸入外部IP → 選擇SFTP模式 → Key file
在`使用者`欄位輸入 root ,並在 `key file `欄位選擇對應的檔案就是剛剛的 rootKey.ppk,如下圖所示
![](https://i.imgur.com/phu92Of.png)
這樣就可以從我們的電腦透過 ftp 連線到 google cloud 了。
---
### 開始建置 api server
#### 下載SQL 資料表
首先從[下載點](https://drive.google.com/file/d/15HuuTthEbuZldWg8Zb0TT_UNQ_HNZd6h/view?usp=sharing)匯入建好的 SQL 資料表 `news (1).sql`,下載完成後打開
http://[Your IP]/phpmyadmin/index.php
建立一個叫 `news` 的資料庫,編碼用 `utf8_unicode_ci` 然後匯入剛剛下載的 `news(1).sql`,就會看到直接被建好並含有定位資料的表格,其中定位會用到的表格是 `wifilist`
---
#### 建置 api server
從[RESTful api server 下載點](https://drive.google.com/file/d/16_zQLbDOaLqHtYgAsyKlESDTTDbttjST/view?usp=sharing)中,把 `api.zip` 載下來,下載完成之後解壓縮並用 ftp 連線把它放到 Google cloud 上的
/var/www/html/
然後到
/var/www/html/api/application/config/database.php
修改連線到 mySQL 的對應資料,就可以連線了
如下的`password`部份
```=php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '[Your Password]',
'database' => 'news',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
```
當完成上述設定後你剛剛丟上 google cloud 的 API server 就建置完成了,這時可以測試看看能不能取得`API.php`裡面函式設定的回傳值,其中`API.php`的位址在
/var/www/html/api/application/controller/API.php
---
最後只要在網址列輸入
http://[Your IP]/api/index.php/API/product
可以取得`function product_get()`的回傳資料
:::info
應該會是 {"status":"success","contain":"null"}
:::
http://[Your IP]/api/index.php/API/wifilist
可以取得`function wifilist_get()`的回傳資料
:::info
應該會是 {"max_SearchTime":"206"}
:::
:::success
[Your IP] 請自己代換成你的 ip
:::
---
#### 設定 python 執行環境
python 在 ubuntu 中有內建了,輸入
python
就會啟動你目前安裝的 python 編程環境,接著輸入
exit()
來離開 python 的編程環境
這次我們要安裝的是 pip ,它是python 的套件管理工具,首先輸入
sudo apt-get install python-pip
接著安裝定位用到的套件
pip install numpy
pip install IPython
pip install pandas
pip install scipy
pip install -U scikit-learn
:::danger
注意!!!
pip 安裝完東西的時候會有提示要你更新
千萬別更新!!!
千萬別更新!!!
千萬別更新!!!
pip10 目前有 bug 無法修復,如果不小心更新的話之後的指令請改用備份的
python -m pip install IPython
:::
安裝完成後到[定位模組下載點](https://drive.google.com/file/d/1NOdL2QrnLTMqm1Q5FAEF4c17ReFNwxCD/view?usp=sharing)下載 python 的定位模組並把載下來的 python.zip 跟定位資料檔都丟進
/home
> 補充一下
> 手機那邊收到的 wifi 資料會存到資料庫的 wifilist 表格中,透過
> `http://[Your IP]/api/index.php/API/wificsv/format/csv`
> 會下載到一個整理好的 csv 檔,再把該 csv 檔放置到 `test3.py` 指定的位置讀取[color=green]
接著輸入
python /home/test3.py
看看會不會丟出 ERRORrrr 就知道環境是否建置完成
> 如果你套件沒有安裝完全的話,是不會丟出 ERRORrrr [color=green]
若是要看能不能正常運行可輸入
```
python /home/test3.py [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36]
```
看到回應是
{"status": "Yes!", "location": 2, "accuracy": 0.8548387096774194}
就表示可以正常運作。
---
#### 設定 php 呼叫 python 函式位置
將裡面的 pythoncall_post 跟 pythoncall_get 部分內容修改如下
$result = shell_exec('python "/home/test3.py" ' . escapeshellarg(json_encode($finalArray)));
`pythoncall_get` 中的
$data = [1,2,3,4,5,6];
改成
$data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36];
還有
/home/test3.py
請修改成自己放 test3.py 的位置,修改好後可以輸入以下網址測試
http://[Your IP]/api/index.php/API/pythoncall
:::info
[Your IP] 請自己代換成你的 IP
後面的 API 記得要大寫,大小寫是有分的!!!
:::
當看到
{"status":"success","contain":"{\"status\": \"Yes!\", \"location\": 1, \"accuracy\": 0.8225806451612904}\n"}
就代表全部修改完成了!
恭喜你的 API server 可以正常運作了!
請記得把 apk 內呼叫的位置改為對應的 IP 喔~
>有問題可以聯繫 BodomMoon@gmail.com [name=任品謙]
---
###### tags: `教學`