|
1 | 1 | ---
|
2 |
| -title: 'Quickstart: Create a Linux Python app' |
3 |
| -description: Get started with Linux apps on Azure App Service by deploying your first Python app to a Linux container in App Service. |
| 2 | +title: 'Quickstart: Create Python app on Linux - Azure App Service' |
| 3 | +description: Deploy your first Python hello world app in Azure App Service on Linux in minutes. |
| 4 | +services: app-service\web |
| 5 | +documentationcenter: '' |
4 | 6 | author: msangapu-msft
|
5 | 7 |
|
6 | 8 | ms.topic: quickstart
|
7 |
| -ms.date: 08/23/2019 |
8 |
| -ms.custom: mvc |
9 |
| -ms.custom: seodec18 |
| 9 | +ms.date: 1/14/2020 |
| 10 | +ms.author: msangapu |
| 11 | +ms.custom: seo-python-october2019 |
10 | 12 |
|
11 | 13 | experimental: false
|
12 |
| -experiment_id: 1e304dc9-5add-4b |
| 14 | +experiment_id: 01a9132f-eaab-4c |
13 | 15 | ---
|
14 |
| -# Create a Python app in Azure App Service on Linux |
| 16 | +# Quickstart: Create a Python app in Azure App Service on Linux |
15 | 17 |
|
16 |
| -In this quickstart, you deploy a simple Python app to [App Service on Linux](app-service-linux-intro.md), which provides a highly scalable, self-patching web hosting service. You use the Azure command-line interface (the [Azure CLI](/cli/azure/install-azure-cli)) through the interactive, browser-based Azure Cloud Shell, so you can follow the steps use a Mac, Linux, or Windows computer. |
| 18 | +In this quickstart, you deploy a Python web app to [App Service on Linux](app-service-linux-intro.md), Azure's highly scalable, self-patching web hosting service. You use the local [Azure command-line interface (CLI)](/cli/azure/install-azure-cli) on a Mac, Linux, or Windows computer. The web app you configure uses a free App Service tier, so you incur no costs in the course of this article. |
17 | 19 |
|
18 |
| - |
| 20 | +If you prefer to deploy apps through an IDE, see [Deploy Python apps to App Service from Visual Studio Code](/azure/python/tutorial-deploy-app-service-on-linux-01). |
19 | 21 |
|
20 |
| -[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)] |
| 22 | +## Prerequisites |
21 | 23 |
|
22 |
| -[!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)] |
| 24 | +- Azure subscription - [create one for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio) |
| 25 | +- <a href="https://www.python.org/downloads/" target="_blank">Python 3.7</a> (Python 3.6 is also supported) |
| 26 | +- <a href="https://git-scm.com/downloads" target="_blank">Git</a> |
| 27 | +- <a href="https://docs.microsoft.com/cli/azure/install-azure-cli" target="_blank">Azure CLI</a> |
23 | 28 |
|
24 | 29 | ## Download the sample
|
25 | 30 |
|
26 |
| -In the Cloud Shell, create a quickstart directory and then change to it. |
| 31 | +In a terminal window, run the following command to clone the sample application to your local computer. |
27 | 32 |
|
28 |
| -```bash |
29 |
| -mkdir quickstart |
30 |
| - |
31 |
| -cd $HOME/quickstart |
| 33 | +```terminal |
| 34 | +git clone https://github.com/Azure-Samples/python-docs-hello-world |
32 | 35 | ```
|
33 | 36 |
|
34 |
| -Next, run the following command to clone the sample app repository to your quickstart directory. |
| 37 | +Then go into that folder: |
35 | 38 |
|
36 |
| -```bash |
37 |
| -git clone https://github.com/Azure-Samples/python-docs-hello-world |
| 39 | +```terminal |
| 40 | +cd python-docs-hello-world |
38 | 41 | ```
|
39 | 42 |
|
40 |
| -While running, it displays information similar to the following example: |
| 43 | +The repository contains an *application.py* file, which tells App Service that the code contains a Flask app. For more information, see [Container startup process and customizations](how-to-configure-python.md). |
41 | 44 |
|
42 |
| -```bash |
43 |
| -Cloning into 'python-docs-hello-world'... |
44 |
| -remote: Enumerating objects: 43, done. |
45 |
| -remote: Total 43 (delta 0), reused 0 (delta 0), pack-reused 43 |
46 |
| -Unpacking objects: 100% (43/43), done. |
47 |
| -Checking connectivity... done. |
| 45 | +## Sign in to Azure |
| 46 | + |
| 47 | +The Azure CLI provides you with many convenient commands that you use from a local terminal to provision and manage Azure resources from the command line. You can use commands to complete the same tasks that you would through the Azure portal in a browser. You can also use CLI commands in scripts to automate management processes. |
| 48 | + |
| 49 | +To run Azure commands in the Azure CLI, you must first sign in using the `az login` command. This command opens a browser to gather your credentials. |
| 50 | + |
| 51 | +```terminal |
| 52 | +az login |
48 | 53 | ```
|
49 | 54 |
|
50 |
| -## Create a web app |
| 55 | +## Deploy the sample |
51 | 56 |
|
52 |
| -Change to the directory that contains the sample code and run the `az webapp up` command. |
| 57 | +The [`az webapp up`](/cli/azure/webapp#az-webapp-up) command creates the web app on App Service and deploys your code. |
53 | 58 |
|
54 |
| -In the following example, replace *\<app_name>* with a globally unique app name (valid characters are `a-z`, `0-9`, and `-`). |
| 59 | +In the *python-docs-hello-world* folder that contains the sample code, run the following `az webapp up` command. Replace `<app-name>` with a globally unique app name (*valid characters are `a-z`, `0-9`, and `-`*). Also replace `<location-name>` with an Azure region such as **centralus**, **eastasia**, **westeurope**, **koreasouth**, **brazilsouth**, **centralindia**, and so on. (You can retrieve a list of allowable regions for your Azure account by running the [`az account locations-list`](/cli/azure/appservice?view=azure-cli-latest.md#az-appservice-list-locations) command.) |
55 | 60 |
|
56 |
| -```bash |
57 |
| -cd python-docs-hello-world |
58 | 61 |
|
59 |
| -az webapp up -n <app_name> |
| 62 | +```terminal |
| 63 | +az webapp up --sku F1 -n <app-name> -l <location-name> |
60 | 64 | ```
|
61 | 65 |
|
62 |
| -This command may take a few minutes to run. While running, it displays information similar to the following example: |
| 66 | +This command may take a few minutes complete run. While running, it displays information similar to the following example: |
63 | 67 |
|
64 |
| -```json |
| 68 | +```output |
65 | 69 | The behavior of this command has been altered by the following extension: webapp
|
66 |
| -Creating Resource group 'appsvc_rg_Linux_CentralUS' ... |
| 70 | +Creating Resource group 'appsvc_rg_Linux_centralus' ... |
67 | 71 | Resource group creation complete
|
68 |
| -Creating App service plan 'appsvc_asp_Linux_CentralUS' ... |
| 72 | +Creating App service plan 'appsvc_asp_Linux_centralus' ... |
69 | 73 | App service plan creation complete
|
70 |
| -Creating app '<app_name>' .... |
| 74 | +Creating app '<app-name>' .... |
71 | 75 | Webapp creation complete
|
72 | 76 | Creating zip with contents of dir /home/username/quickstart/python-docs-hello-world ...
|
73 | 77 | Preparing to deploy contents to app.
|
74 | 78 | All done.
|
75 | 79 | {
|
76 |
| - "app_url": "https:/<app_name>.azurewebsites.net", |
| 80 | + "app_url": "https:/<app-name>.azurewebsites.net", |
77 | 81 | "location": "Central US",
|
78 |
| - "name": "<app_name>", |
| 82 | + "name": "<app-name>", |
79 | 83 | "os": "Linux",
|
80 |
| - "resourcegroup": "appsvc_rg_Linux_CentralUS ", |
81 |
| - "serverfarm": "appsvc_asp_Linux_CentralUS", |
| 84 | + "resourcegroup": "appsvc_rg_Linux_centralus ", |
| 85 | + "serverfarm": "appsvc_asp_Linux_centralus", |
82 | 86 | "sku": "BASIC",
|
83 | 87 | "src_path": "/home/username/quickstart/python-docs-hello-world ",
|
84 | 88 | "version_detected": "-",
|
85 | 89 | "version_to_create": "python|3.7"
|
86 | 90 | }
|
87 | 91 | ```
|
88 | 92 |
|
89 |
| -The `az webapp up` command does the following actions: |
| 93 | +[!INCLUDE [AZ Webapp Up Note](../../../includes/app-service-web-az-webapp-up-note.md)] |
90 | 94 |
|
91 |
| -- Create a default resource group. |
| 95 | +## Browse to the app |
92 | 96 |
|
93 |
| -- Create a default app service plan. |
| 97 | +Browse to the deployed application in your web browser at the URL `http://<app-name>.azurewebsites.net`. |
94 | 98 |
|
95 |
| -- Create an app with the specified name. |
| 99 | +The Python sample code is running a Linux container in App Service using a built-in image. |
96 | 100 |
|
97 |
| -- [Zip deploy](https://docs.microsoft.com/azure/app-service/deploy-zip) files from the current working directory to the app. |
| 101 | + |
98 | 102 |
|
99 |
| -## Browse to the app |
| 103 | +**Congratulations!** You've deployed your Python app to App Service on Linux. |
| 104 | + |
| 105 | +## Run the sample app locally |
| 106 | + |
| 107 | +In a terminal window, use the commands below (as appropriate for your operating system) to install the required dependencies and launch the built-in development server. |
100 | 108 |
|
101 |
| -Browse to the deployed application using your web browser. |
| 109 | +# [Bash](#tab/bash) |
102 | 110 |
|
103 | 111 | ```bash
|
104 |
| -http://<app_name>.azurewebsites.net |
| 112 | +python3 -m venv venv |
| 113 | +source venv/bin/activate |
| 114 | +pip install -r requirements.txt |
| 115 | +FLASK_APP=application.py |
| 116 | +flask run |
105 | 117 | ```
|
106 | 118 |
|
107 |
| -The Python sample code is running in App Service on Linux with a built-in image. |
| 119 | +# [PowerShell](#tab/powershell) |
| 120 | + |
| 121 | +```powershell |
| 122 | +py -3 -m venv env |
| 123 | +env\scripts\activate |
| 124 | +pip install -r requirements.txt |
| 125 | +Set-Item Env:FLASK_APP ".\application.py" |
| 126 | +flask run |
| 127 | +``` |
| 128 | + |
| 129 | +# [Cmd](#tab/cmd) |
| 130 | + |
| 131 | +```cmd |
| 132 | +py -3 -m venv env |
| 133 | +env\scripts\activate |
| 134 | +pip install -r requirements.txt |
| 135 | +SET FLASK_APP=application.py |
| 136 | +flask run |
| 137 | +``` |
108 | 138 |
|
109 |
| - |
| 139 | +--- |
110 | 140 |
|
111 |
| -**Congratulations!** You've deployed your first Python app to App Service on Linux. |
| 141 | +Open a web browser, and go to the sample app at `http://localhost:5000/`. The app displays the message **Hello World!**. |
112 | 142 |
|
113 |
| -## Update and redeploy the code |
| 143 | + |
114 | 144 |
|
115 |
| -In the Cloud Shell, type `code application.py` to open the Cloud Shell editor. |
| 145 | +In your terminal window, press **Ctrl**+**C** to exit the web server. |
116 | 146 |
|
117 |
| - |
| 147 | +## Redeploy updates |
118 | 148 |
|
119 |
| - Make a small change to the text in the call to `return`: |
| 149 | +In your favorite code editor, open *application.py* and change the `return` statement on the last line to match the following code. The `print` statement is included here to generate logging output that you work with in the next section. |
120 | 150 |
|
121 | 151 | ```python
|
| 152 | +print("Handling request to home page.") |
122 | 153 | return "Hello Azure!"
|
123 | 154 | ```
|
124 | 155 |
|
125 |
| -Save your changes and exit the editor. Use the command `^S` to save and `^Q` to exit. |
| 156 | +Save your changes and exit the editor. |
126 | 157 |
|
127 |
| -You'll now redeploy the app. Substitute `<app_name>` with your app. |
| 158 | +Redeploy the app using the following `az webapp up` command, using the same command you used to deploy the app the first time, replacing `<app-name>` and `<location-name>` with the same names you used before. |
128 | 159 |
|
129 |
| -```bash |
130 |
| -az webapp up -n <app_name> |
| 160 | +```terminal |
| 161 | +az webapp up --sku F1 -n <app-name> -l <location-name> |
131 | 162 | ```
|
132 | 163 |
|
133 |
| -Once deployment has completed, switch back to the browser window that opened in the **Browse to the app** step, and refresh the page. |
| 164 | +Once deployment has completed, switch back to the browser window open to `http://<app-name>.azurewebsites.net` and refresh the page, which should display the modified message: |
| 165 | + |
| 166 | + |
134 | 167 |
|
135 |
| - |
| 168 | +> [!TIP] |
| 169 | +> Visual Studio Code provides powerful extensions for Python and Azure App Service, which simplify the process of deploying Python web apps to App Service. For more information, see [Deploy Python apps to App Service from Visual Studio Code](/azure/python/tutorial-deploy-app-service-on-linux-01). |
136 | 170 |
|
137 |
| -## Manage your new Azure app |
| 171 | +## Stream logs |
138 | 172 |
|
139 |
| -Go to the <a href="https://portal.azure.com" target="_blank">Azure portal</a> to manage the app you created. |
| 173 | +You can access the console logs generated from inside the app and the container in which it runs. Logs include any output generated using `print` statements. |
| 174 | + |
| 175 | +First, turn on container logging by running the following command in a terminal, replacing `<app-name>` with the name of your app and `<resource-group-name>` with the name of the resource group shown in the output of the `az webapp up` command you used (such as "appsvc_rg_Linux_centralus"): |
| 176 | + |
| 177 | +```terminal |
| 178 | +az webapp log config --name <app-name> --resource-group <resource-group-name> --docker-container-logging filesystem |
| 179 | +``` |
140 | 180 |
|
141 |
| -From the left menu, click **App Services**, and then click the name of your Azure app. |
| 181 | +Once container logging is turned on, run the following command to show the log stream: |
| 182 | + |
| 183 | +```terminal |
| 184 | +az webapp log tail --name <app-name> --resource-group <resource-group-name> |
| 185 | +``` |
| 186 | + |
| 187 | +Refresh the app in the browser to generate console logs, which should include lines similar to the following text. If you don't see output immediately, try again in 30 seconds. |
| 188 | + |
| 189 | +```output |
| 190 | +2019-10-23T12:40:03.815574424Z Handling request to home page. |
| 191 | +2019-10-23T12:40:03.815602424Z 172.16.0.1 - - [23/Oct/2019:12:40:03 +0000] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.63 Safari/537.36 Edg/78.0.276.19" |
| 192 | +``` |
142 | 193 |
|
143 |
| - |
| 194 | +You can also inspect the log files from the browser at `https://<app-name>.scm.azurewebsites.net/api/logs/docker`. |
| 195 | + |
| 196 | +To stop log streaming at any time, type `Ctrl`+`C`. |
| 197 | + |
| 198 | +## Manage the Azure app |
| 199 | + |
| 200 | +Go to the <a href="https://portal.azure.com" target="_blank">Azure portal</a> to manage the app you created. Search for and select **App Services**. |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | +Select the name of your Azure app. |
| 205 | + |
| 206 | + |
144 | 207 |
|
145 | 208 | You see your app's Overview page. Here, you can perform basic management tasks like browse, stop, start, restart, and delete.
|
146 | 209 |
|
147 |
| - |
| 210 | + |
| 211 | + |
| 212 | +The App Service menu provides different pages for configuring your app. |
| 213 | + |
| 214 | +## Clean up resources |
148 | 215 |
|
149 |
| -The left menu provides different pages for configuring your app. |
| 216 | +In the preceding steps, you created Azure resources in a resource group. The resource group has a name like "appsvc_rg_Linux_CentralUS" depending on your location. If you use an App Service SKU other than the free F1 tier, these resources will incur ongoing costs. |
150 | 217 |
|
151 |
| -[!INCLUDE [cli-samples-clean-up](../../../includes/cli-samples-clean-up.md)] |
| 218 | +If you don't expect to need these resources in the future, delete the resource group by running the following command, replacing `<resource-group-name>` with the resource group shown in the output of the `az webapp up` command, such as "appsvc_rg_Linux_centralus". The command may take a minute to complete. |
| 219 | + |
| 220 | +```terminal |
| 221 | +az group delete -n <resource-group-name> |
| 222 | +``` |
152 | 223 |
|
153 | 224 | ## Next steps
|
154 | 225 |
|
155 | 226 | > [!div class="nextstepaction"]
|
156 |
| -> [Tutorial: Python app with PostgreSQL](tutorial-python-postgresql-app.md) |
| 227 | +> [Tutorial: Python (Django) web app with PostgreSQL](tutorial-python-postgresql-app.md) |
157 | 228 |
|
158 | 229 | > [!div class="nextstepaction"]
|
159 | 230 | > [Configure Python app](how-to-configure-python.md)
|
160 | 231 |
|
161 | 232 | > [!div class="nextstepaction"]
|
162 |
| -> [Tutorial: Deploy from private container repository](tutorial-custom-docker-image.md) |
| 233 | +> [Tutorial: Run Python app in custom container](tutorial-custom-docker-image.md) |
0 commit comments