2018年6月13日 星期三

[GCP+Laravel5.5] Google Cloud Platform踩雷筆記



1.開始一個Laravel專案(已有專案者可跳過)

在xampp的htdocs資料夾打開git bash
輸入指令(5.5版本是用php7.0 ,gcp支援,5.6是用php7.1,gcp不支援)
composer create-project --prefer-dist laravel/laravel myProject '5.5.*'

cd myProject //到專案資料夾
php artisan make:auth //直接開一個使用者登入和註冊的功能
php artisan session:table //開session資料表
php artisan cache:table //開cache資料表


2.增加本機資料庫

去phpmyadmin
增加一個資料庫叫myproject
再去改你專案的.env檔
//.env檔

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myproject
DB_USERNAME=root //xampp的mysal預設的
DB_PASSWORD=     //xampp的mysal預設的
php artisan migrate //殖民資料庫!

啊咧出錯
Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`

// app\Providers\AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

手動增加設定(Laravel何時會發現這個問題捏)

去phpmyadmin刪掉剛剛加一半的所有資料表

再跑一次!
php artisan migrate //殖民資料庫!


3.直接上github

先去github開一個repository
開完以後複製git remote add origin https://github.com/xxxxx/xxxxx.git 那一段
git init //開始git
git add . //全部加入stage
git commit -m "my first commit" //上commit
git remote add origin https://github.com/xxxxx/xxxxx.git //指定github要放到哪去
git push -u origin master //推囉!

輸入帳號和密碼以後就可以推上上去囉

4.開google cloud platform專案

前往GCP
https://cloud.google.com/
1.建立專案
2.專案建立中,又上角圈圈轉啊轉
3.打開終極好物「Google Cloud Shell」(右上角哦),直接在瀏覽器上操作Server
4.開始clone專案
mkdir src //增加一個src資料夾
cd src //進去
git clone https://github.com/xxxxx/xxxxx.git //把你剛剛放到github上的東西clone下來 


4.去開資料庫囉

1.去SQL頁面
2.增加instance
3.嗯...選一個喜歡的吧
4.取名叫myproject
5.root的密碼設myproject
6.選個離台灣近的asisa-east1
7.建立中,右上角圈圈轉啊轉
8.點進去你新增的instance
9.點選databases頁籤
10.建立資料庫
11.取名叫myproject
12.編碼方式就參考專案內config\database.php的utf8mb4和utf8mb4_unicode_ci吧


5.匯入資料囉!

1.去你本機端的phpmyadmin
2.按你的資料庫(myproject)
3.匯出
4.自訂
5.以文字顯示輸出結果
6.執行
7.全選 複製SQL碼
8.回到GCP頁面
9.打開好物Google Cloud Shell(右上角哦)
10.下指令
gcloud sql connect myproject --user=root
//連線到SQL,並連接myproject這個instance,並用root登入
11.轉啊轉
12.輸入你剛剛設的密碼myproject
13.下指令
use myproject; //使用你剛剛建立的資料庫
14.貼上你剛剛複製的SQL碼
15.下指令 show tables; 檢查是否匯入成功


6.跑測試環境囉!

1.因為deploy會跑很久(上次測,跑了快九分鐘),所以可以先跑測試環境
2.在好物Google Cloud Shell,你的專案目錄中下指令
sudo apt-get install php //更新php7
composer dump-autoload //更新 autoload_classmap 的內容
composer update --no-scripts //更新google上的套件包
php artisan serve //跑測試環境
3.打開一個新的Shell(左上角有加號可以按),開SQL供測試環境使用
(instance的connectName可以在SQL頁面複製貼上)
cloud_sql_proxy -instances=你的instance的connectName=tcp:3306
4.Shell的右上角有個小視窗圖示,可以開測試環境,因為Laravel的測試環境預設port是8000,所以要改一下
5.啊咧出錯
Whoops, looks like something went wrong.

6.用vim去改.env檔(好像被git ignore了),把本機的複製貼上過去,啊記得要改database的資訊
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myproject //google上你剛剛設定的database名字
DB_USERNAME=root
DB_PASSWORD=myproject //google上你剛剛設定的root的密碼

CACHE_DRIVER=database
SESSION_DRIVER=database
7.再去一次測試環境,畫面就出來囉
8.測試一下註冊和登入功能,都成功!


7.要部屬啦!

1.先gen一個key出來(一樣在google cloud shell運行喔)
php artisan key:gen --show //gen一個key出來
2.看你習慣是要在本機端編輯好以後再用git推上去,還是在好物google cloud shell裡面直接用vim編輯
總之就是要新增一個app.yaml檔,因為部屬的時候,不吃.env檔
3.app.yaml檔內容如下
runtime: php
env: flex

runtime_config:
  document_root: public

# Ensure we skip ".env", which is only for local development
skip_files:
  - .env

env_variables:
  # Put production environment variables here.
  APP_LOG: errorlog
  APP_KEY: xxxxxxxxxxxxxx //剛剛gen出來的key
  STORAGE_DIR: /tmp
  CACHE_DRIVER: database
  SESSION_DRIVER: database
  ## Set these environment variables according to your CloudSQL configuration.
  DB_CONNECTION: mysql
  DB_PORT: 3306
  DB_HOST: 127.0.0.1
  DB_DATABASE: myproject //google上你剛剛設定的database名字
  DB_USERNAME: root
  DB_PASSWORD: myproject //google上你剛剛設定的root的密碼
  DB_SOCKET: "/cloudsql/xxxxx:asia-east1:myproject" //instance的connectName可以在SQL頁面複製貼上

beta_settings:
    # for Cloud SQL, set this value to the Cloud SQL connection name,
    # e.g. "project:region:cloudsql-instance"
    cloud_sql_instances: "xxxxx:asia-east1:myproject" //instance的connectName可以在SQL頁面複製貼上

4.設定composer.json(可用vim),設定部屬結束後,要執行的指令
//寫在scripts下
"post-deploy-cmd": [
    "php artisan cache:clear", //清cache
    "chmod -R 755 bootstrap\/cache" //更改伺服器對於專案的這個資料夾的權限
]
5.萬事俱備只欠東風,下指令啦
gcloud app deploy
6.因為剛剛沒去App Engine增加新的,所以他跟你確定位置要放在哪
Please choose the region where you want your App Engine application
located:

 [1] europe-west2  (supports standard and flexible)
 [2] us-east1      (supports standard and flexible)
 [3] us-east4      (supports standard and flexible)
 [4] asia-northeast1 (supports standard and flexible)
 [5] asia-south1   (supports standard and flexible)
 [6] australia-southeast1 (supports standard and flexible)
 [7] southamerica-east1 (supports standard and flexible)
 [8] northamerica-northeast1 (supports standard and flexible)
 [9] us-central    (supports standard and flexible)
 [10] europe-west3  (supports standard and flexible)
 [11] europe-west   (supports standard and flexible)
 [12] cancel
Please enter your numeric choice:  4   //離台灣比較近
Creating App Engine application in project [xxxx] and region [asia-northeast1]....done.
Services to deploy:
descriptor:      [/home/xxxxx/src/myproject/app.yaml]
source:          [/home/xxxxx/src/myproject]
target project:  [xxxxxx]
target service:  [default]
target version:  [xxxxxx]
target url:      [https://xxxxx.appspot.com]
Do you want to continue (Y/n)?  y
6.部屬結束後,他會顯示網址,也可以去App Engine那邊找網址(在右上角,格式就像https://xxxx.appspot.com/)
7.啊咧又出錯
Whoops, looks like something went wrong.

8.這時去app.yaml檔裡面加一個屬性,再 glcoud app deploy 一次,就可以看到錯誤訊息
 APP_DEBUG: true
9.看起來就是SQL連不上的問題
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `sessions` where `id` = xxxxxxx limit 1)
SQLSTATE[HY000] [2002] No such file or directory
gcloud app logs tail -s default //查詢剛剛deploy時發生了甚麼事

10.少一個步驟,就是去啟用App Engine的SQL連接功能,
要去此網址一鍵啟用:console.developers.google.com/apis/api/sqladmin.googleapis.com/
此網址應該也可以:https://cloud.google.com/sql/docs/mysql/admin-api/
Access Not Configured. 
Cloud SQL Administration API has not been used in project xxxxxx before or it is disabled. 
Enable it by visiting 
https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview?project=xxxxxx then retry.
If you enabled this API recently, 
wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured
11.再gcloud app deploy一次(到底要幾次,翻桌)
12.歷經風雨後,看到Laravel的字樣出現就是開心


沒有留言:

張貼留言