Skip to content

Commit 22f7249

Browse files
committed
2nd pass
1 parent 6377d96 commit 22f7249

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

articles/app-service/containers/tutorial-python-postgresql-app.md

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ You can follow the steps in this article on macOS, Linux, or Windows. The steps
3535
Before you start this tutorial:
3636

3737
- [!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
38-
- Install [Git](https://git-scm.com/)
39-
- Install [Python 3](https://www.python.org/downloads/)
40-
- Install and run [PostgreSQL](https://www.postgresql.org/download/)
38+
- Install [Git](https://git-scm.com/).
39+
- Install [Python 3](https://www.python.org/downloads/).
40+
- Install and run [PostgreSQL](https://www.postgresql.org/download/).
4141

42-
## Connect to your local PostgreSQL server and create a database
42+
## Create a local PostgreSQL database
4343

44-
First, create and connect to a local PostgreSQL server and database:
44+
First, connect to your local PostgreSQL server and create a database:
4545

4646
In a local terminal window, run `psql` to connect to your local PostgreSQL server as the built-in `postgres` user.
4747

@@ -55,7 +55,7 @@ If the connection isn't successful, make sure your PostgreSQL server is running,
5555
pg_ctl.exe restart -D "<postgresql data directory>"
5656
```
5757

58-
In your *postgres* directory, create a new database called *pollsdb*, and set up a new database user named *manager* with password *supersecretpass*:
58+
Create a new database called *pollsdb*, and set up a database user named *manager* with password *supersecretpass*:
5959

6060
```sql
6161
CREATE DATABASE pollsdb;
@@ -71,11 +71,11 @@ Type `\q` to exit the PostgreSQL client.
7171

7272
Next, set up and run the sample Python Django web app.
7373

74-
The [djangoapp](https://github.com/Azure-Samples/djangoapp) sample repository contains the finished data-driven [Django](https://www.djangoproject.com/) polls app you get from following [Writing your first Django app](https://docs.djangoproject.com/en/2.1/intro/tutorial01/) in the Django documentation. You can connect this, or any other data-driven Python Django web app, to a local or cloud PostgreSQL database back end, and deploy the app to Azure App Service.
74+
The [djangoapp](https://github.com/Azure-Samples/djangoapp) sample repository contains the data-driven [Django](https://www.djangoproject.com/) polls app you get by following [Writing your first Django app](https://docs.djangoproject.com/en/2.1/intro/tutorial01/) in the Django documentation. You can connect this, or any other data-driven Python Django web app, to a local or cloud PostgreSQL database back end.
7575

7676
### Clone the sample app
7777

78-
In a terminal window, run the following commands to clone the sample app repository and change to the new working directory:
78+
In a terminal window, run the following commands to clone the sample app repository, and change to the new working directory:
7979

8080
```bash
8181
git clone https://github.com/Azure-Samples/djangoapp.git
@@ -96,7 +96,7 @@ py -3 -m venv venv
9696
venv\scripts\activate
9797
```
9898

99-
In the `venv` environment, change to your *djangoapp* folder and run *env.sh* or *env.ps1* to set the environment variables *azuresite/settings.py* will use for the database connection settings.
99+
In the `venv` environment, run *env.sh* or *env.ps1* to set the environment variables *azuresite/settings.py* will use for the database connection settings.
100100

101101
```bash
102102
source ./env.sh
@@ -148,9 +148,9 @@ To stop the Django server, type Ctrl+C in the terminal.
148148

149149
Most of the remaining steps in this article use Azure CLI commands in the Azure Cloud Shell.
150150

151-
## Create and connect to an Azure Database for PostgreSQL database
151+
## Create and connect to Azure Database for PostgreSQL
152152

153-
In this section, you create an Azure Database for PostgreSQL database, and connect your web app to it. When you deploy your web app to Azure App Service, the app uses this cloud database.
153+
In this section, you create an Azure Database for PostgreSQL server and database, and connect your web app to it. When you deploy your web app to Azure App Service, the app uses this cloud database.
154154

155155
### Create a resource group
156156

@@ -160,20 +160,23 @@ You can create a new resource group for your Azure Database for PostgreSQL serve
160160

161161
### Create an Azure Database for PostgreSQL server
162162

163-
You create a PostgreSQL server with the [`az postgres server create`](/cli/azure/postgres/server?view=azure-cli-latest#az-postgres-server-create) command in the Cloud Shell.
163+
You create a PostgreSQL server with the [az postgres server create](/cli/azure/postgres/server?view=azure-cli-latest#az-postgres-server-create) command in the Cloud Shell.
164164

165-
In the following command, replace *\<postgresql-name>* with a unique server name. The server name is part of your PostgreSQL endpoint *https://<postgresql-name>.postgres.database.azure.com*, so the name needs to be unique across all servers in Azure.
165+
> [!NOTE]
166+
> Before you create an Azure Database for PostgreSQL server, check which [compute generation](/azure/postgresql/concepts-pricing-tiers#compute-generations-and-vcores) is available in your region. If your region doesn't support Gen4 hardware, change *--sku-name* in the following command line to a value that's supported in your region, such as Gen5.
167+
168+
In the following command, replace *\<postgresql-name>* with a unique server name. The server name is part of your PostgreSQL endpoint *https://\<postgresql-name>.postgres.database.azure.com*, so the name needs to be unique across all servers in Azure.
166169

167-
Replace *\<resourcegroup-name>* and *\<region>* with the name and region of the resource group you created or want to use. Replace *\<admin-username>* and *\<admin-password>* with user credentials for the database administrator account.
170+
Replace *\<resourcegroup-name>* and *\<region>* with the name and region of the resource group you want to use. Replace *\<admin-username>* and *\<admin-password>* with user credentials for the database administrator account.
168171

169172
> [!NOTE]
170-
> Before you create an Azure Database for PostgreSQL server, check which [compute generation](https://docs.microsoft.com/azure/postgresql/concepts-pricing-tiers#compute-generations-and-vcores) is available in your region. If your region doesn't support Gen4 hardware, change *--sku-name* in the following command line to a value that's supported in your region, such as Gen5.
173+
> Remember the *\<admin-username>* and *\<admin-password>* to use later for signing in to the PostgreSQL server and databases.
171174
172175
```azurecli-interactive
173176
az postgres server create --resource-group <resourcegroup-name> --name <postgresql-name> --location "<region>" --admin-user <admin-username> --admin-password <admin-password> --sku-name B_Gen4_1
174177
```
175178

176-
When the Azure Database for PostgreSQL server is created, the Azure CLI shows information similar to the following example:
179+
After the Azure Database for PostgreSQL server is created, the Azure CLI returns JSON code like the following example:
177180

178181
```json
179182
{
@@ -194,29 +197,26 @@ When the Azure Database for PostgreSQL server is created, the Azure CLI shows in
194197
}
195198
```
196199

197-
> [!NOTE]
198-
> Remember the *\<admin-username>* and *\<admin-password>* to use later for signing in to the PostgreSQL server and databases.
199-
200200
### Create firewall rules for the Azure Database for PostgreSQL server
201201

202-
In the Cloud Shell, run the following Azure CLI command to allow access to the database from Azure resources. Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
202+
Run the [az postgres server firewall-rule create](/cli/azure/postgres/server/firewall-rule#az-postgres-server-firewall-rule-create) command to allow access to the database from Azure resources. Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
203203

204204
```azurecli-interactive
205205
az postgres server firewall-rule create --resource-group <resourcegroup-name> --server-name <postgresql-name> --start-ip-address=0.0.0.0 --end-ip-address=0.0.0.0 --name AllowAllAzureIPs
206206
```
207207

208208
> [!NOTE]
209-
> The preceding setting allows network connections from all IP addresses within the Azure network. For production use, try to configure the most restrictive firewall rules possible by [using only the outbound IP addresses your app uses](../overview-inbound-outbound-ips.md?toc=%2fazure%2fapp-service%2fcontainers%2ftoc.json#find-outbound-ips).
209+
> The preceding setting allows network connections from all IP addresses within the Azure network. For production use, try to configure the most restrictive firewall rules possible by [allowing only the outbound IP addresses your app uses](../overview-inbound-outbound-ips.md?toc=%2fazure%2fapp-service%2fcontainers%2ftoc.json#find-outbound-ips).
210210
211-
Run the `firewall-rule-create` command again to allow access from your local computer. Replace *\<your-ip-address>* with [your local IPv4 IP address](https://www.whatsmyip.org/). Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
211+
Run the `firewall-rule create` command again to allow access from your local computer. Replace *\<your-ip-address>* with [your local IPv4 IP address](https://www.whatsmyip.org/). Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your own values.
212212

213213
```azurecli-interactive
214214
az postgres server firewall-rule create --resource-group <resourcegroup-name> --server-name <postgresql-name> --start-ip-address=<your-ip-address> --end-ip-address=<your-ip-address> --name AllowLocalClient
215215
```
216216

217217
### Create and connect to the Azure Database for PostgreSQL database
218218

219-
In the Cloud Shell, connect to the Azure Database for PostgreSQL server by running the following command. Use the server name, admin username, and password you created for the server.
219+
Connect to your Azure Database for PostgreSQL server by running the following command. Use your own *\<postgresql-name>* and *\<admin-username>*, and sign in with the password you created for the server.
220220

221221
```bash
222222
psql -h <postgresql-name>.postgres.database.azure.com -U <admin-username>@<postgresql-name> postgres
@@ -230,14 +230,13 @@ CREATE USER manager WITH PASSWORD 'supersecretpass';
230230
GRANT ALL PRIVILEGES ON DATABASE pollsdb TO manager;
231231
```
232232

233-
Type `\q` to exit the PostgreSQL client.
234-
235233
> [!NOTE]
236-
> It's a best practice to create database users with restricted permissions for specific apps, instead of using the admin user. In this example, the `manager` user has full privileges to *only* the `pollsdb` database.
234+
> It's a best practice to create database users with restricted permissions for specific apps, instead of using the admin user. The `manager` user has full privileges to *only* the `pollsdb` database.
235+
Type `\q` to exit the PostgreSQL client.
237236

238237
### Test app connectivity to the Azure Database for PostgreSQL production database
239238

240-
Edit your local *env.sh* or *env.ps1* file to change the environment variables to your cloud PostgreSQL database by replacing *\<postgresql-name>* with your Azure Database for PostgreSQL server name.
239+
Edit your local *env.sh* or *env.ps1* file to point to to your cloud PostgreSQL database, by replacing *\<postgresql-name>* with your Azure Database for PostgreSQL server name.
241240

242241
```bash
243242
export DBHOST="<postgresql-name>.postgres.database.azure.com"
@@ -262,7 +261,7 @@ or
262261
.\env.ps1
263262
```
264263

265-
Run Django migration to the Azure database and create an admin user.
264+
Run Django migration to the Azure database, and create an admin user.
266265

267266
```bash
268267
python manage.py migrate
@@ -275,13 +274,13 @@ Once the admin user is created, run the Django server.
275274
python manage.py runserver
276275
```
277276

278-
In a browser, go to *http:\//localhost:8000* again. You should see the message **No polls are available** again.
277+
In a browser, go to *http:\//localhost:8000*, and you should see the message **No polls are available** again.
279278

280-
Go to *http:\//localhost:8000/admin* and sign in using the admin user you created, and create a poll question like before.
279+
Go to *http:\//localhost:8000/admin*, sign in using the admin user you created, and create a poll question like before.
281280

282281
![Run Python Django app in App Services locally](./media/tutorial-python-postgresql-app/run-python-django-app-locally.png)
283282

284-
Go to *http:\//localhost:8000* again and see the poll question displayed. Your app is now writing data to the Azure Database for PostgreSQL database.
283+
Go to *http:\//localhost:8000* again, and see the poll question displayed. Your app is now writing data to the Azure Database for PostgreSQL database.
285284

286285
## Deploy the web app to Azure App Service
287286

@@ -293,15 +292,15 @@ You need to change and add some settings in your *djangoapp/azuresite/settings.p
293292

294293
1. Django validates the `HTTP_HOST` header in incoming requests. For your Django web app to work in App Service, you need to add the fully qualified domain name of the app to the allowed hosts.
295294

296-
Edit *azuresite/settings.py* to change the `ALLOWED_HOSTS` line as follows.
295+
Edit *azuresite/settings.py* to change the `ALLOWED_HOSTS` line as follows:
297296

298297
```python
299298
ALLOWED_HOSTS = [os.environ['WEBSITE_SITE_NAME'] + '.azurewebsites.net', '127.0.0.1'] if 'WEBSITE_SITE_NAME' in os.environ else []
300299
```
301300

302-
1. Django doesn't support [serving static files in production](https://docs.djangoproject.com/en/2.1/howto/static-files/deployment/). For this tutorial, you use [WhiteNoise](https://whitenoise.evans.io/en/stable/) to enable serving static files in production. The WhiteNoise package was already installed with *requirements.txt*.
301+
1. Django doesn't support [serving static files in production](https://docs.djangoproject.com/en/2.1/howto/static-files/deployment/). For this tutorial, you use [WhiteNoise](https://whitenoise.evans.io/en/stable/) to enable serving the needed files. The WhiteNoise package was already installed with *requirements.txt*.
303302

304-
To configure Django to use WhiteNoise, in *azuresite/settings.py*, find the `MIDDLEWARE` setting, and add the `whitenoise.middleware.WhiteNoiseMiddleware` middleware to the list, just after the `django.middleware.security.SecurityMiddleware` middleware line. Your `MIDDLEWARE` setting should look like this:
303+
To configure Django to use WhiteNoise, in *azuresite/settings.py*, find the `MIDDLEWARE` setting, and add `whitenoise.middleware.WhiteNoiseMiddleware` to the list, just after the `django.middleware.security.SecurityMiddleware` line. Your `MIDDLEWARE` setting should look like this:
305304

306305
```python
307306
MIDDLEWARE = [
@@ -323,7 +322,7 @@ You need to change and add some settings in your *djangoapp/azuresite/settings.p
323322
> [!IMPORTANT]
324323
> The database settings section already follows the security best practice of using environment variables. For complete deployment recommendations, see [Django Documentation: deployment checklist](https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/).
325324
326-
Commit your changes into your fork of the repository:
325+
Commit your changes into your fork of the *djangoapp* repository:
327326

328327
```bash
329328
git commit -am "configure for App Service"
@@ -345,15 +344,15 @@ git commit -am "configure for App Service"
345344

346345
Earlier in the tutorial, you defined environment variables to connect to your PostgreSQL database.
347346

348-
In App Service, you set environment variables as *app settings* by using the [az webapp config appsettings set](/cli/azure/webapp/config/appsettings?view=azure-cli-latest#az-webapp-config-appsettings-set) command in Cloud Shell.
347+
In Azure App Service, you set environment variables as *app settings* by using the [az webapp config appsettings set](/cli/azure/webapp/config/appsettings?view=azure-cli-latest#az-webapp-config-appsettings-set) command.
349348

350-
The following example specifies the database connection details as app settings.
349+
In the Azure Cloud Shell, run the following command to specify the database connection details as app settings. Replace *\<app-name>*, *\<resourcegroup-name>*, and *\<postgresql-name>* with your own values.
351350

352351
```azurecli-interactive
353-
az webapp config appsettings set --name <app-name> --resource-group myResourceGroup --settings DBHOST="<postgresql-name>.postgres.database.azure.com" DBUSER="manager@<postgresql-name>" DBPASS="supersecretpass" DBNAME="pollsdb"
352+
az webapp config appsettings set --name <app-name> --resource-group <resourcegroup-name> --settings DBHOST="<postgresql-name>.postgres.database.azure.com" DBUSER="manager@<postgresql-name>" DBPASS="supersecretpass" DBNAME="pollsdb"
354353
```
355354

356-
For information on how these app settings are accessed in your code, see [Access environment variables](how-to-configure-python.md#access-environment-variables).
355+
For information on how your code accesses these app settings, see [Access environment variables](how-to-configure-python.md#access-environment-variables).
357356

358357
### Push to Azure from Git
359358

@@ -385,9 +384,9 @@ The App Service deployment server sees *requirements.txt* in the repository root
385384

386385
### Browse to the Azure app
387386

388-
Browse to the deployed app with URL *https:\//\<app-name>.azurewebsites.net*. It takes some time to start because the container needs to be downloaded and run when the app is requested for the first time. If the page times out or displays an error message, wait a few minutes and refresh the page.
387+
Browse to the deployed app with URL *https:\//\<app-name>.azurewebsites.net*. It takes some time to start, because the container must be downloaded and run when the app is requested for the first time. If the page times out or displays an error message, wait a few minutes and refresh the page.
389388

390-
You should see the poll question that you created earlier.
389+
You should see the poll questions that you created earlier.
391390

392391
App Service detects a Django project in your repository by looking for a *wsgi.py* file in each subdirectory, which `manage.py startproject` creates by default. When App Service finds the file, it loads the Django web app. For more information on how App Service loads Python apps, see [Configure built-in Python image](how-to-configure-python.md).
393392

@@ -403,7 +402,7 @@ Go to *https:\//\<app-name>.azurewebsites.net/admin* and sign in using the admin
403402

404403
## Manage your app in the Azure portal
405404

406-
In the [Azure portal](https://portal.azure.com) search for and select the app you created.
405+
In the [Azure portal](https://portal.azure.com), search for and select the app you created.
407406

408407
![Navigate to your Python Django app in the Azure portal](./media/tutorial-python-postgresql-app/navigate-to-django-app-in-app-services-in-the-azure-portal.png)
409408

0 commit comments

Comments
 (0)