본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

PHP를 Azure App Service에 배포하기

원문 보기 위키 갱신

지속적 배포(CD) 워크플로의 일부로 PHP 프로젝트를 Azure App Service에 배포하는 방법을 알아봐요. Composer로 종속성을 설치하고 PHP 앱을 Azure 웹 앱에 빌드·배포하는 워크플로를 살펴봐요.

출처: 문서

본문

사전 요구 사항

GitHub Actions 워크플로를 만들기 전에 먼저 다음 설정 단계를 완료해야 해요.

  1. Azure App Service 플랜을 만들어요.

    예를 들어 Azure CLI를 사용해 새 App Service 플랜을 만들 수 있어요:

    az appservice plan create \
       --resource-group MY_RESOURCE_GROUP \
       --name MY_APP_SERVICE_PLAN \
       --is-linux
    

    위 명령에서 MY_RESOURCE_GROUP을 기존 Azure 리소스 그룹으로, MY_APP_SERVICE_PLAN을 App Service 플랜의 새 이름으로 바꿔요.

    Azure CLI 사용에 대한 자세한 내용은 Azure 문서를 확인하세요:

  2. 웹 앱을 만들어요.

    예를 들어 Azure CLI를 사용해 PHP 런타임이 있는 Azure App Service 웹 앱을 만들 수 있어요:

    az webapp create \
        --name MY_WEBAPP_NAME \
        --plan MY_APP_SERVICE_PLAN \
        --resource-group MY_RESOURCE_GROUP \
        --runtime "php|7.4"
    

    위 명령에서 파라미터를 나만의 값으로 바꾸되, MY_WEBAPP_NAME은 웹 앱의 새 이름이에요.

  3. Azure 게시 프로필을 구성하고 AZURE_WEBAPP_PUBLISH_PROFILE 시크릿을 만들어요.

    게시 프로필을 사용해 Azure 배포 자격 증명을 생성해요. 자세한 내용은 Azure 문서의 배포 자격 증명 생성하기를 참고하세요.

    GitHub 저장소에 게시 프로필의 내용을 담은 AZURE_WEBAPP_PUBLISH_PROFILE이라는 시크릿을 만들어요. 시크릿을 만드는 방법에 대한 자세한 내용은 GitHub Actions에서 시크릿 사용하기를 참고하세요.

  4. 선택적으로, 배포 환경을 구성해요. 환경은 production, staging, development 같은 일반적인 배포 대상을 설명하는 데 사용돼요. GitHub Actions 워크플로가 환경으로 배포하면 환경이 저장소 메인 페이지에 표시돼요. 환경을 사용해 작업 진행에 승인을 요구하거나, 어떤 브랜치가 워크플로를 트리거할 수 있는지 제한하거나, 커스텀 배포 보호 규칙으로 배포를 게이팅하거나, 시크릿 접근을 제한할 수 있어요. 환경을 만드는 방법에 대한 자세한 내용은 배포용 환경 관리하기를 참고하세요.

워크플로 만들기

사전 요구 사항을 완료했다면 워크플로를 만들 수 있어요.

다음 예제 워크플로는 main 브랜치로 푸시가 있을 때 PHP 프로젝트를 Azure App Service에 빌드하고 배포하는 방법을 보여줘요.

워크플로 env 키에서 AZURE_WEBAPP_NAME을 만든 웹 앱 이름으로 설정해야 해요. 프로젝트 경로가 저장소 루트가 아니라면 AZURE_WEBAPP_PACKAGE_PATH를 프로젝트 경로로 변경해요. 8.x가 아닌 다른 PHP 버전을 사용한다면 PHP_VERSION을 사용 중인 버전으로 변경해요.

배포 환경을 구성했다면 environment 값을 환경 이름으로 변경해요. 환경을 구성하지 않았거나 워크플로가 비공개 저장소에 있고 GitHub Enterprise Cloud를 사용하지 않는다면 environment 키를 삭제해요.

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Build and deploy PHP app to Azure Web App

env:
  AZURE_WEBAPP_NAME: MY_WEBAPP_NAME   # set this to your application's name
  AZURE_WEBAPP_PACKAGE_PATH: '.'      # set this to the path to your web app project, defaults to the repository root
  PHP_VERSION: '8.x'                  # set this to the PHP version to use

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Setup PHP
        uses: shivammathur/setup-php@1f2e3d4c5b6a7f8e9d0c1b2a3e4f5d6c7b8a9e0f
        with:
          php-version: ${{ env.PHP_VERSION }}

      - name: Check if composer.json exists
        id: check_files
        uses: andstor/file-existence-action@2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b
        with:
          files: 'composer.json'

      - name: Get Composer Cache Directory
        id: composer-cache
        if: steps.check_files.outputs.files_exists == 'true'
        run: |
          echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Set up dependency caching for faster installs
        uses: actions/cache@v4
        if: steps.check_files.outputs.files_exists == 'true'
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-

      - name: Run composer install if composer.json exists
        if: steps.check_files.outputs.files_exists == 'true'
        run: composer validate --no-check-publish && composer install --prefer-dist --no-progress

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v4
        with:
          name: php-app
          path: .

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v5
        with:
          name: php-app

      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
          package: .

더 알아보기 (Learn more)

  • 원본 워크플로 템플릿은 GitHub Actions starter-workflows 저장소의 azure-webapps-php.yml을 참고하세요.
  • 웹 앱을 배포하는 데 사용된 액션은 공식 Azure Azure/webapps-deploy 액션이에요.
  • Azure로 배포하는 GitHub Action 워크플로의 더 많은 예시는 actions-workflow-samples 저장소를 참고하세요.