使用 Azure CLI 快速建立 Azure Web App 測試網站、資料庫與儲存體帳戶
source link: https://blog.miniasp.com/post/2021/09/09/Quickly-create-Azure-Web-App-SQL-Database-Storage-account
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
架設一個測試網站,最常見的需求就是一個網站(Azure Web App)外加一個資料庫(Azure SQL Database),如果有檔案儲存需求,頂多就再加一個儲存體帳戶(Azure Storage Account)就可以搞定。這篇文章我將分享如何透過 Azure CLI 快速建立網站、資料庫與儲存體帳戶,你完全可以不透過 Azure Portal 建立這些服務,使用 無互動 (non-Interactive) 的方式就可以建立所有必要的 Azure 資源。
以下所有 Azure CLI 命令都是在 WSL 2 環境下執行。
登入 Azure CLI 並取得授權
-
登入 Azure CLI
az login --use-device-code
-
切換到正確的 Azure 訂用帳戶
az account set -s "Microsoft Azure Sponsorship"
建立資源群組
-
建立資源群組
az group create -l 'eastasia' -n 'myapp'
-
建立資源群組標籤
az group update -g 'myapp' \ --set tags.申請人員='張OO' \ --set tags.客戶名稱='台灣微軟' \ --set tags.環境名稱='Testing' \ --set tags.專案名稱='台灣微軟 - 神奇的系統'
建立 Azure Service 服務
-
建立 Azure Service Plan 方案
找出你想使用的價格等級 (pricing tier) (
--sku
)az appservice plan create -h
依據你想建立的
--sku
建立 Web Appaz appservice plan create -g 'myapp' -n 'myapp-plan' -l 'eastasia' --sku 'B1'
-
建立 Azure Web App 站台
查出目前 Azure Web App 支援的 Runtimes 清單:
az webapp list-runtimes
aspnet|V4.8 aspnet|V3.5 DOTNETCORE|2.1 DOTNETCORE|3.1 DOTNET|5.0 DOTNET|6.0 node|10.6 node|10.10 node|10.14 node|12-lts node|14-lts php|7.3 php|7.4 python|3.6 java|1.8|Tomcat|7.0 java|1.8|Tomcat|8.5 java|1.8|Tomcat|9.0 java|1.8|Java SE|8 java|7|Tomcat|7.0 java|7|Tomcat|8.5 java|7|Tomcat|9.0 java|7|Java SE|8 java|11|Tomcat|7.0 java|11|Tomcat|8.5 java|11|Tomcat|9.0 java|11|Java SE|8
依據不同的 Runtime 建立 Web App
az webapp create -g 'myapp' -p 'myapp-plan' -n 'myapp' --runtime "DOTNET|5.0"
取得 Azure Web App 站台完整屬性清單
az webapp show -g 'myapp' -n 'myapp' -o json
-
設定 Azure Web App 啟用 Always On 特性 (僅
B1
等級以上才支援 Always On 特性)az webapp config set -g 'myapp' -n 'myapp' --always-on true
取得 Azure Web App 預設域名
az webapp show -g myapp -n myapp -o json | jq '.defaultHostName' -r
-
設定 Azure Web App 應用程式參數
列出目前有多少應用程式設定
az webapp config appsettings list -g 'myapp' -n 'myapp'
設定應用程式參數值
az webapp config appsettings set -g 'myapp' -n 'myapp' --settings 'APPINSIGHTS_INSTRUMENTATIONKEY=2e938371-xxxx-xxxx-xxxx-cf43f85f91ac' az webapp config appsettings set -g 'myapp' -n 'myapp' --settings 'APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=2e938371-xxxx-xxxx-xxxx-cf43f85f91ac;IngestionEndpoint=https://eastasia-0.in.applicationinsights.azure.com/' az webapp config appsettings set -g 'myapp' -n 'myapp' --settings 'WEBSITE_NODE_DEFAULT_VERSION=6.9.1'
刪除應用程式參數
az webapp config appsettings delete -g 'myapp' -n 'myapp' --setting-names 'WEBSITE_NODE_DEFAULT_VERSION' 'aaa'
-
設定 Azure Web App 資料庫連接字串
列出目前有多少資料庫連接字串
az webapp config connection-string list -g 'myapp' -n 'myapp'
設定資料庫連接字串
az webapp config connection-string set -g 'myapp' -n 'myapp' -t 'SQLServer' --settings DefaultConnection='Server=ip.add.re.ss;Database=myDataBase;User Id=myUsername;Password=myPassword;MultipleActiveResultSets=true'
刪除資料庫連接字串
az webapp config connection-string delete -g 'myapp' -n 'myapp' --setting-names 'DefaultConnection'
-
取得 Azure Web App 發行設定檔
az webapp deployment list-publishing-profiles -g 'myapp' -n 'myapp' --xml > /mnt/g/myapp.pubxml
建立 Azure SQL Database 測試資料庫
-
建立 Azure SQL Server 資料庫伺服器
az sql server create -l 'eastasia' -g 'myapp' -n 'myapp-dbs' -u 'myapp' -p '9gZ8TCzsYzVf'
取得 Azure SQL Server 完整屬性清單
az sql server show -g 'myapp' -n 'myapp-dbs' -o json az sql server show -g 'myapp' -n 'myapp-dbs' -o json | jq '.fullyQualifiedDomainName' -r
重新設定預設管理員密碼
az sql server update -g 'myapp' -n 'myapp-dbs' -p '9gZ8TCzsYzVf'
-
列出 Azure SQL Database 所有資料庫等級 (Service Objective)
az sql db list-editions --location eastasia --output table -a -d az sql db list-editions --location eastasia --output table --edition Basic az sql db list-editions --location eastasia --output table --edition Standard az sql db list-editions --location eastasia --output table --edition Premium az sql db list-editions --location eastasia --output table --edition GeneralPurpose
-
建立 Azure SQL Database 資料庫
az sql db create -g 'myapp' -s 'myapp-dbs' -n 'myappDiscountDev' \ --service-objective 'Basic' \ --backup-storage-redundancy 'Local'
-
建立新的管理者登入帳號
USE [myapp] GO CREATE LOGIN [myapp_admin] WITH PASSWORD=N'68SKYPPBGrws' GO USE [myapp] GO CREATE USER [myapp_admin] FOR LOGIN [myapp_admin] GO ALTER ROLE [db_owner] ADD MEMBER [myapp_admin] GO
-
建立新的網站使用者登入帳號
USE [master] GO CREATE LOGIN [myapp_user] WITH PASSWORD=N'Thw2DEahZPc9' GO USE [myapp] GO CREATE USER [myapp_user] FOR LOGIN [myapp_user] GO ALTER ROLE [db_owner] ADD MEMBER [myapp_user] GO CREATE USER [myapp_user] FOR LOGIN [myapp_user] ALTER ROLE [db_datareader] ADD MEMBER [myapp_user] ALTER ROLE [db_datawriter] ADD MEMBER [myapp_user] CREATE ROLE db_executor GRANT EXECUTE TO db_executor ALTER ROLE [db_executor] ADD MEMBER [myapp_user]
建立 Azure Storage 服務
-
建立 Azure Storage 儲存體帳戶
az storage account create -g 'myapp' -n 'myappstorage' -l 'eastasia' --sku Standard_LRS
取得 Azure Storage Account 完整屬性清單
az storage account show -g 'myapp' -n 'myappstorage' -o json
-
建立 Blob 容器
az storage container create -n 'files' --account-name 'myappstorage' --public-access 'off' az storage container create -n 'files' --account-name 'myappstorage' --public-access 'blob' az storage container create -n 'files' --account-name 'myappstorage' --public-access 'conainer'
-
取得 Azure Storage 儲存體帳戶金鑰 (Account Key)
az storage account keys list -g 'myapp' --account-name 'myappstorage' -o json
-
建立 Azure Storage 特定容器的 SAS Token
az storage blob generate-sas \ --account-name 'myappstorage' \ --account-key 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX+LSBqQtRk7Rhrd46RpidW25IbwSrg6D1AP9oL+/2W2gOht6XVBnBgb5bg==' \ --container-name 'files' \ --name '' \ --permissions r \ --expiry 2021-09-15 --https-only
-
建立 Azure Storage 特定容器與特定檔案的 SAS Token
az storage blob generate-sas \ --account-name 'myappstorage' \ --account-key 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX+LSBqQtRk7Rhrd46RpidW25IbwSrg6D1AP9oL+/2W2gOht6XVBnBgb5bg==' \ --container-name 'files' \ --name 'folder1/abc.txt' \ --permissions r \ --expiry 2021-09-15 --https-only
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK