Share via


教學課程:在虛擬網路中使用App Services Web 應用程式建立 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器實例

適用範圍:適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器

本教學課程說明如何在虛擬網路建立具有 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器的 Azure App 服務 Web 應用程式。

在本教學課程中,您將了解如何:

  • 在虛擬網路中建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例
  • 建立 Web 應用程式
  • 將 Web 應用程式新增至虛擬網路
  • 從 Web 應用程式 連線 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

  • 在本機安裝 Azure CLI 2.0 版或更新版本(或使用 預安裝 CLI 的 Azure Cloud Shell )。 若要查看所安裝的版本,請執行 az --version 命令。

  • 使用 az login 命令登入您的帳戶。 請記下命令輸出中的識別碼屬性,以取得對應的訂用帳戶名稱。

    az login
    
  • 如果您有多個訂用帳戶,請選擇資源計費的適當訂用帳戶。 使用 az account set 命令來選取您帳戶底下的特定訂用帳戶 ID。

    az account set --subscription <subscription ID>
    

在新虛擬網路中建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例

使用下列命令,在虛擬網路 (VNET) 內建立私人 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例:

az postgres flexible-server create --resource-group demoresourcegroup --name demoserverpostgres --vnet demoappvnet --location westus2

此命令會執行下列動作,這可能需要幾分鐘的時間:

  • 建立資源群組 (若尚不存在)。
  • 如果未提供伺服器名稱,則會產生伺服器名稱。
  • 為 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例建立虛擬網路和子網。
  • 如果未提供,請為您的伺服器建立管理員使用者名稱和密碼。
  • 建立名為 postgres 的空白資料庫。

以下是範例輸出。

Creating Resource Group 'demoresourcegroup'...
Creating new Vnet "demoappvnet" in resource group "demoresourcegroup"
Creating new Subnet "Subnetdemoserverpostgres" in resource group "demoresourcegroup"
Creating a private dns zone demoserverpostgres.private.postgres.database.azure.com in resource group "demoresourcegroup"
Creating PostgreSQL Server 'demoserverpostgres' in group 'demoresourcegroup'...
Your server 'demoserverpostgres' is using sku 'Standard_D2s_v3' (Paid Tier). Please refer to https://aka.ms/postgres-pricing for pricing details
Creating PostgreSQL database 'flexibleserverdb'...
Make a note of your password. If you forget, you would have to reset your password with "az postgres flexible-server update -n demoserverpostgres -g demoresourcegroup -p <new-password>".
Try using 'az postgres flexible-server connect' command to test out connection.
{
  "connectionString": "postgresql://generated-username:generated-password@demoserverpostgres.postgres.database.azure.com/postgres?sslmode=require",
  "host": "demoserverpostgres.postgres.database.azure.com",
  "id": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/demoresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/demoserverpostgres",
  "location": "East US",
  "password": "generated-password",
  "resourceGroup": "demoresourcegroup",
  "skuname": "Standard_D2s_v3",
  "subnetId": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/demoresourcegroup/providers/Microsoft.Network/virtualNetworks/demoappvnet/subnets/Subnetdemoserverpostgres",
  "username": "generated-username",
  "version": "12"
}

建立 Web 應用程式

在本節中,您會在 App Service 應用程式中建立應用程式主機,將此應用程式連線到 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器資料庫,然後將程式代碼部署至該主機。 確定您位於終端中包含應用程式程式碼的儲存機制根路徑。 注意 基本方案不支援 VNET 整合。 使用標準或 進階版。

使用 az webapp up 命令建立 App Service 應用程式 (主機處理序)。

az webapp up --resource-group demoresourcegroup --location westus2 --plan testappserviceplan --sku P2V2 --name mywebapp

注意

  • 針對 --location 自變數,請使用與上一節中資料庫相同的位置。
  • 將所有 Azure 中唯一的名稱取代 <app-name> 。 <app-name> 的允許字元為 A-Z、0-9 和 -。 良好的模式是使用您的公司名稱和應用程式識別碼的組合。

此命令會執行下列動作,這可能需要幾分鐘的時間:

  • 建立資源群組 (若尚不存在)。 (在此命令中,您使用先前用來建立資料庫的相同資源群組)。
  • 建立 App Service 應用程式 (如果不存在)。
  • 啟用應用程式的預設記錄 (如果尚未啟用)。
  • 使用已啟用建置自動化的 ZIP 部署來上傳存放庫。

建立 Web 應用程式的子網

啟用 VNET 整合之前,您必須有委派給 App Service Web 應用程式的子網。 建立子網之前,請先檢視資料庫子網位址,以避免針對 Web 應用程式子網使用相同的地址前綴。

az network vnet show --resource-group demoresourcegroup -n demoappvnet

執行下列命令,在建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例的相同虛擬網路中建立新的子網。 更新位址前置詞,以避免與 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器子網發生衝突。

az network vnet subnet create --resource-group demoresourcegroup --vnet-name demoappvnet --name webappsubnet  --address-prefixes 10.0.1.0/24  --delegations Microsoft.Web/serverFarms

將 Web 應用程式新增至虛擬網路

使用 az webapp vnet-integration 命令,將區域虛擬網路整合新增至 WebApp。

az webapp vnet-integration add --resource-group demoresourcegroup -n  mywebapp --vnet demoappvnet --subnet webappsubnet

設定環境變數以連線資料庫

現在將程式代碼部署至 App Service,下一個步驟是將應用程式連線到 Azure 中的 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例。 應用程式程式代碼預期會在許多環境變數中尋找資料庫資訊。 若要在 App Service 中設定環境變數,請使用 az webapp config appsettings set 命令。

  
az webapp config appsettings set  --name mywebapp --settings DBHOST="<postgres-server-name>.postgres.database.azure.com" DBNAME="postgres" DBUSER="<username>" DBPASS="<password>" 
  • 取代新建立 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例命令的 postgres-server-nameusernamepassword
  • 使用此命令也會以為您產生的認證來取代 <username><password>
  • 資源群組和應用程式名稱均提取自 .azure/config 檔案中的快取值。
  • 此命令會建立名為 DBHOSTDBNAMEDBUSER*和 DBPASS設定。 如果您的應用程式程式代碼針對資料庫資訊使用不同的名稱,請使用這些名稱作為程式碼中所述的應用程式設定。

設定 Web 應用程式以允許虛擬網路中的所有輸出連線。

az webapp config set --name mywebapp --resource-group demoresourcegroup --generic-configurations '{"vnetRouteAllEnabled": true}'

清除資源

使用下列命令清除您在教學課程中建立的所有資源。 此命會刪除此資源群組內的所有資源。

az group delete -n demoresourcegroup

下一步