LoginSignup
0

More than 5 years have passed since last update.

posted at

updated at

推一個最小限度可執行的 PHP 網頁到 Heroku 上面去

概觀

主要有三個東西
- php 檔案
- Procfile
- composer.json

資料夾長這樣

composer.json
Procfile
web/
  - index.php

預先要裝的東西

  • 自己喜歡的編輯器
  • Git
  • Heroku CLI

因為這邊不會用到本機上執行 php 和使用 composer 的功能,所以這邊就不列了。
想知道怎麼在 mac 上面裝 PHP 和 composer 的話可以看這篇 - 在 Mac 透過 Homebrew 裝 PHP7 和 Composer

index.php

用喜歡的編輯器開一個檔案,加入以下程式碼並存成 index.php

index.php
<?php echo "hello world"; ?>

composer.json

因為什麼都沒有要加,只需要建立檔案,內容空著沒關係

Procfile

這個檔案不用副檔名,直接叫這樣就好。
檔案中的內容如下:

web: vendor/bin/heroku-php-nginx web/
  • 指定要使用用 ngix 當作是 web server
  • 指定網站根目錄在 web/ 資料夾之下。也就是剛剛 index.php 所放的位置

Commit, 並推送到 heroku

如往常地把這些新增 commit 完之後。
推送的目的地,通常是會推到長得像這樣子的位置,就像是一般的 git repo 位址。

https://git.heroku.com/<heroku_app_id>.git

接著,把她加入 remote 中。

git remote add heroku https://git.heroku.com/<heroku_app_id>.git

加完之後可以透過 git remote -v 來看有沒有加成功。成功的話就可以 push 了:

git push heroku master

推的過程就會看到終端機裡面跳出一堆處理中的訊息。
沒有任何問題的話,就可以在資料夾處用 heroku open 指令直接開啟網站來看看了

.gitignore

目前忽略掉兩個,一個是 composer 產生的 vendor 資料夾,另外一個則是會被 monolog 用到的 .env

vendor/
.env

下一個

來弄弄看 MySQL ...

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0