iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 26
1
Software Development

30天開發與部署 Laravel 專案系列 第 26

GCE遠端同步與部署專案

情境
在GCP Compute Engine /Ubuntu/Nginx環境下,
透過 git pull 遠端同步已存在的 Laravel專案。

透過 git 同步遠端資料

本篇採用個人已上傳github的專案,可透過 https git url 進行後續操作,步驟如下:
https://ithelp.ithome.com.tw/upload/images/20201010/20125263ZEM0eQVBDi.png

先找個資料夾並初始化專案git

git init

檢查 git remote

git remote -v
正常會顯示 fetch跟push的git網址
https://ithelp.ithome.com.tw/upload/images/20201010/20125263puS6BERVUO.png
如果沒有要先設定 git remote add origin git網址 後,再做一次確認。

Pull 下載更新

git pull origin master 或是 git pull git網址 master

添加Laravel專案中受git ingore影響的檔案

git ingore

用途:可避免遠端同步機密、非必較檔案、故意忽略的檔案
作法: 在目錄裡放一個 .gitignore 檔,並設定想忽略的規則即可

缺少檔案

  • vendor/composer.lock : composer install --no-scripts
  • .env :複製 .env.example
  • 產生key: php artisan key:generate

連線vm上的資料庫

  • 確認 MySQL有無 .env 指定的database , username, password
    沒有就mysql -u root -p進入MsySQL後逐步建立。

  • 確認MySQL連線的port 3306 有無在防火牆內。
    沒有就用ufw防火牆的設定指令去新增

專案部分資料夾權限調整

同上一篇的 Laravel/storage log 寫入權限調整

Nginx 設定檔調整

  • 設定檔位置:vim etc/nginx/sites-enabled/default
  • root folder調整:
    同上一篇的 Nginx預設資料夾調整,因為暫時還是以一個IP顯示一個專案。
    Laravel專案的進入點是public資料夾,所以要把設定為
    root /var/www/html/專案所屬資料夾/public;
    
  • 設定檔修改完畢記得重啟 Nginx
    systemctl restart nginx

部署專案

啟動前設定

php artisan migrate
建立資料庫遷移,讓GCE上的database符合開發時的設定。

啟動專案

php artisan serve

成果

https://ithelp.ithome.com.tw/upload/images/20201011/20125263w9vs4l3KDK.png

各個專案頁面、RESTful API也可以透過GCE給的外部ip進行測試。


參考資料
https://backlog.com/git-tutorial/tw/reference/remote.html
https://gitbook.tw/chapters/github/pull-from-github.html
https://stackoverflow.com/questions/23411520/how-to-fix-error-laravel-log-could-not-be-opened
https://learnku.com/laravel/wikis/25560
https://git-scm.com/docs/gitignore
https://gitbook.tw/chapters/using-git/ignore.html
https://zlargon.gitbooks.io/git-tutorial/content/file/ignore.html
https://ithelp.ithome.com.tw/articles/10228765
https://dev.to/shuv1824/deploy-laravel-application-with-nginx-on-ubuntu-18-04-on-digitalocean-1egl


上一篇
GCE /Ubuntu/Nginx環境下建立 Laravel專案
下一篇
SSH 進 GCP的3種方式
系列文
30天開發與部署 Laravel 專案30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言