PHP: PHP 服務快速入門

如何在 Stackhero 上開始使用 PHP

👋 歡迎瀏覽 Stackhero 文件!

Stackhero 提供受管理的 PHP 託管服務,專為快速、可靠的部署而設:

  • 只需一個簡單的 git push,即可部署您的 PHP 應用程式到生產環境,過程只需數秒。
  • 可使用您自己的網域名稱,系統會自動為您配置HTTPS 憑證,無需手動操作。
  • 享有自動備份、一鍵更新及清晰、可預測的收費模式。Stackhero 會處理這些細節,讓您專注於開發程式碼。
  • 在專為您的應用程式而設的私有專屬基礎設施上,獲得卓越的效能與安全性。

讓您的工作流程更順暢,節省寶貴時間。 開始使用 Stackhero 的 Laravel 及 Symfony 專用 PHP 託管服務 只需幾分鐘。

在 Stackhero 的 managed PHP hosting 上部署您的 PHP 應用程式既簡單又強大。只需幾個步驟,您的專案就能上線並隨時可用。本指南將以友善且逐步的方式,帶您完成 PHP 服務的建立、設定及部署。

首先,您可以直接在 Stackhero 控制台建立 PHP 服務。介面設計簡潔,讓這個流程快速又直接。

在開始之前,請確保您已準備好以下工具:

  1. Git。可於 https://git-scm.com/downloads 下載。
  2. Windows Terminal(Windows 用戶可選)。這個終端機能帶來更佳的操作體驗,可於 Microsoft Store 取得。

主要需要設定的是您的 SSH 公鑰。您可以透過以下其中一個指令取得您的公鑰:

cat ~/.ssh/id_rsa.pub

或

cat ~/.ssh/id_ed25519.pub

如果您尚未有 SSH 金鑰對,可以在 Linux 或 macOS 上使用 ssh-keygen,或在 Windows 上使用 ssh-keygen.exe 來產生。

取得公鑰後,請前往 Stackhero 控制台,選擇您的 PHP 服務,進入設定頁面,並將公鑰貼到指定欄位。

Tip: 如果您想為所有未來建立的服務預設 SSH 公鑰,可以在全域設定。只需點擊控制台右上角的個人頭像,進入「Your profile」,然後將 SSH 公鑰貼上即可。

為協助您快速上手,我們準備了一個 PHP 範例應用程式。您可以用以下指令複製專案:

git clone https://github.com/stackhero-io/phpGettingStarted.git stackhero-php-getting-started
cd stackhero-php-getting-started

Stackhero 讓您可以輕鬆透過 Git 部署應用程式。請複製控制台 PHP 服務首頁提供的 git remote 指令,格式如下:

git remote add stackhero ssh://stackhero@<XXXXXX>.stackhero-network.com:222/project.git

現在您已準備好部署應用程式。請使用以下指令推送您的程式碼:

git push stackhero main

首次推送時,系統會要求您確認金鑰指紋,請輸入 "yes"。稍等片刻,您的應用程式就會上線。您可以在 Stackhero 控制台提供的網址(通常為 https://<XXXXXX>.stackhero-network.com)檢查狀態。

若要更新應用程式,只需修改 www/index.php(或其他需要的檔案),然後重新部署:

git add -A .
git commit -m "Update www/index.php"
git push stackhero main

如果您已有 PHP 應用程式需要部署,只需將遠端儲存庫加入您的專案(請參考設定遠端 Git 儲存庫),然後推送您的變更:

git push stackhero main

預設情況下,Stackhero 會在 www 目錄尋找您的 PHP 程式碼及靜態檔案。例如,當有人瀏覽 yourdomain.com/myphoto.jpg,系統會從 www/myphoto.jpg 提供檔案。如果您的應用程式使用不同的公開目錄,可以在 PHP 服務設定中調整。

如果部署時遇到以下錯誤:

error: failed to push some refs to '[...]'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

這代表遠端 Git 儲存庫有您本地端沒有的變更。如果您確定要用本地版本覆蓋遠端,可以強制推送:

git push -f stackhero main

如果看到這個錯誤:

error: src refspec main does not match any
error: failed to push some refs to 'ssh://<XXXXXX>.stackhero-network.com:222/project.git'

通常表示本地沒有 main 分支。此時,您可以改為推送 master 分支:

git push stackhero master

如果 Git 顯示 Everything up-to-date,但您沒看到變更被部署,通常是因為忘記提交變更。請執行:

git add -A .
git commit -m "Your commit message"
git push stackhero main

如果您沒有修改任何程式碼但仍想觸發部署,可以使用空提交:

git commit --allow-empty -m "Force update"
git push stackhero main

如果您想部署其他分支(例如 production),可以這樣操作:

git push stackhero production:main

若要部署特定標籤(如 v1.0),請執行:

git push stackhero 'v1.0^{}:main'

^{} 可確保標籤對應的 commit 正確推送。

如需部署特定 commit,先用 git log 查找其 hash,然後強制推送:

git push -f stackhero <HASH>:main

常見做法是為不同環境(如 production、staging)建立獨立服務。您可以透過重新命名及新增遠端儲存庫來管理。

將現有遠端從 stackhero 改名為 stackhero-staging:

git remote rename stackhero stackhero-staging

然後在控制台建立新的 production PHP 服務,並新增遠端:

git remote add stackhero-production ssh://stackhero@<XXXXXX>.stackhero-network.com:222/project.git

之後即可針對不同環境部署:

git push stackhero-production main

或

git push stackhero-staging main

如果您在 macOS 上每次推送都被要求輸入 SSH 金鑰密碼,無需移除密碼,您可以將密碼安全地儲存在 macOS 鑰匙圈。只需執行:

/usr/bin/ssh-add --apple-use-keychain ~/.ssh/id_rsa

完成後,macOS 推送程式碼時就不會再要求輸入金鑰密碼。

在 production 和 staging 環境,建議將敏感資料(如 token、密碼)安全儲存。不要將 secrets 寫死在程式碼庫,建議使用環境變數。您可以在 Stackhero 控制台新增這些變數,然後在程式碼中存取。例如,若建立名為 mySecret 的環境變數,可在 PHP 這樣取得:

getenv("mySecret")

當您推送程式碼時,Stackhero 的部署腳本會自動讀取您的 composer.json,並透過 Composer 安裝所需相依套件。

如果您的應用程式需要儲存檔案(如用戶上傳的圖片),建議使用 object storage 方案。這樣能方便多個服務共用檔案,並將上傳檔案與程式碼分離。您可以考慮 MinIO,這是一個快速、可靠且支援 S3 的解決方案。

如果您偏好使用本地儲存,可利用 PHP 實例內建的持久儲存空間,路徑為 /persistent/storage/。

例如,儲存上傳檔案可用 move_uploaded_file:

move_uploaded_file($_FILES['image']['tmp_name'], '/persistent/storage/image.jpg');

更多 PHP 檔案上傳資訊,請參考官方文件:https://www.php.net/manual/en/features.file-upload.php。

注意: 請務必將資料儲存在 /persistent/storage/ 目錄內。

若您的實例重啟或推送程式碼變更,儲存在持久儲存以外的資料可能會遺失。