Prerequisites
- a free Heroku account.
- PHP installed locally.
- Composer installed locally.
Install Heroku CLI
在mac上:
$ brew install heroku/brew/heroku
ubuntu:
$ sudo snap install heroku --classic
然後先從自己電腦上的command line登入
這個指令會打開瀏覽器並導向到登入頁面,如果你在你電腦上的瀏覽器已經登入Heroku帳號的話,你就只要按下頁面上的登入按鈕就搞定。
接著確認所有必備的工具已經安裝:
$ php -vPHP 7.4.16 (cli) (built: Mar 4 2021 20:52:51) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
接著確認是否已安裝composer:
$ composer -VComposer version 2.1.3 2021-06-09 16:31:20
確認git版本:
$ git --versiongit version 2.31.0
以上確認完畢之後就可以開始來部署了
建立Laravel專案
$ composer create-project laravel/laravel --prefer-dist hello_laravel_herokuInstalling laravel/laravel (v5.1.11)
- Installing laravel/laravel (v5.1.11)
Downloading: 100%
Created project in hello_laravel_heroku
...
...
$ cd hello_laravel_heroku
Git初始化
$ git initInitialized empty Git repository in ~/hello_laravel_heroku/.git/
$ git add .
$ git commit -m "new laravel project"
[master (root-commit) 6ae139d] new laravel project
76 files changed, 5458 insertions(+)
...
部署到Heroku
建立Procfile檔案
這個檔案是讓heroku知道這個專案 root的路徑在 /public
資料夾
$ echo "web: vendor/bin/heroku-php-apache2 public/" > Procfile
$ git add .
$ git commit -m "Procfile for Heroku"[master 1eb2be6] Procfile for Heroku
1 file changed, 1 insertion(+)
create mode 100644 Procfile
建立Heroku App
在你的git專案底下輸入指令:
$ heroku apps:create your-app-name
如果你不想自訂app名稱也可以直接使用:
$ heroku create
這樣的話heroku 會自動幫你生成一個隨機的名稱。
當你建立一個新app的時候同時也會在你的專案底下加入一個heroku的repository。
$ git remote -v
heroku https://git.heroku.com/hosomikai-laravel-demo.git (fetch)
heroku https://git.heroku.com/hosomikai-laravel-demo.git (push)
設定Laravel 的 encryption key
$ heroku config:set APP_KEY=your-encryption-key
如果你還沒有固定使用的key可以使用下面的指令
$ heroku config:set APP_KEY=$(php artisan --no-ansi key:generate --show)
Setting APP_KEY and restarting ⬢ hosomikai-laravel-demo... done, v6
APP_KEY: base64:dyy+KxjVQSE/P4WpCCt20h+nEX2wm1lqnorpoe3V8o1=
encryption key用來加密加密session與處理其他加密資訊,如果網站已上線不能隨意更換。
接著我們直接將分支推上heroku上就可以了
$ git push heroku master
這樣app就被部署完成,你可以透過指令直接在瀏覽器打開網站:
$ heroku open