|
| 1 | +# Getting started with Django on Google Cloud Platform |
| 2 | + |
| 3 | +This repository is an example of how to run a [Django](https://www.djangoproject.com/) |
| 4 | +app on Google Managed VMs. It uses the [Writing your first Django app](https://docs.djangoproject.com/en/1.9/intro/tutorial01/) as the example app to deploy. |
| 5 | + |
| 6 | + |
| 7 | +## Setup the database |
| 8 | + |
| 9 | +This tutorial assumes you are setting up Django using a SQL database, which is the easiest way to run Django. If you have an existing SQL database running, you can use that, but if not, these are the instructions for creating a managed MySQL instance using CloudSQL. |
| 10 | + |
| 11 | + |
| 12 | +* Create a [CloudSQL instance](https://console.cloud.google.com/project/_/sql/create) |
| 13 | + |
| 14 | + * In the instances list, click your Cloud SQL instance. |
| 15 | + |
| 16 | + * Click Access Control. |
| 17 | + |
| 18 | + * In the IP address subsection, click Request an IPv4 address to enable access to the Cloud SQL instance through an |
| 19 | + IPv4 address. It will take a moment to initialize the new IP address. |
| 20 | + |
| 21 | + * Also under Access Control, in the Authorization subsection, under Allowed Networks, click the add (+) button . |
| 22 | + |
| 23 | + * In the Networks field, enter 0.0.0.0/0. This value allows access by all IP addresses. |
| 24 | + |
| 25 | + * Click Save. |
| 26 | + |
| 27 | +Note: setting allowed networks to 0.0.0.0/0 opens your SQL instance to traffic from any computer. For production databases, it's highly recommended to limit the authorized networks to only IP ranges that need access. |
| 28 | + |
| 29 | +* Alternatively, the instance can be created with the gcloud command line tool as follows, substituting `your-root-pw |
| 30 | + with a strong, unique password. |
| 31 | + |
| 32 | + `gcloud sql instances create <instance_name> --assign-ip --authorized-networks=0.0.0.0/0 set-root-password=your-root-pw` |
| 33 | + |
| 34 | +* Create a Database And User |
| 35 | + |
| 36 | + * Using the root password created in the last step to create a new database, user, and password using your preferred MySQL client. Alternatively, follow these instructions to create the database and user from the console. |
| 37 | + * From the CloudSQL instance in the console, click New user. |
| 38 | + * Enter a username and password for the application. For example, name the user "pythonapp" and give it a randomly |
| 39 | + generated password. |
| 40 | + * Click Add. |
| 41 | + * Click Databases and then click New database. |
| 42 | + * For Name, enter the name of your database (for example, "polls"), and click Add. |
| 43 | + |
| 44 | +Once you have a SQL host, configuring mysite/settings.py to point to your database. Change `your-database-name`, |
| 45 | +`your-database-user`, `your-database-host` , and `your-database-password` to match the settings created above. Note the |
| 46 | +instance name is not used in this configuration, and the host name is the IP address you created. |
| 47 | + |
| 48 | +## Running locally |
| 49 | + |
| 50 | +First make sure you have Django installed. It's recommended you do so in a |
| 51 | +[virtualenv](https://virtualenv.pypa.io/en/latest/). The requirements.txt |
| 52 | +contains just the Django dependency. |
| 53 | + |
| 54 | +`pip install -r requirements.txt` |
| 55 | + |
| 56 | +Once the database is setup, run the migrations. |
| 57 | + |
| 58 | +`python manage.py migrate` |
| 59 | + |
| 60 | +If you'd like to use the admin console, create a superuser. |
| 61 | + |
| 62 | +`python manage.py createsuperuser` |
| 63 | + |
| 64 | +The app can be run locally the same way as any other Django app. |
| 65 | + |
| 66 | +`python manage.py runserver` |
| 67 | + |
| 68 | +Now you can view the admin panel of your local site at |
| 69 | + |
| 70 | +`http://localhost:8080/admin` |
| 71 | + |
| 72 | +## Deploying |
| 73 | + |
| 74 | +Since the Django development server is not built for production, our container uses the Gunicorn server. Since Gunicorn doesn't serve static content, |
| 75 | +the static content is instead served from Google Cloud Storage. |
| 76 | + |
| 77 | +First, make a bucket and make it publically readable, replacing <your-gcs-bucket> with a bucket name, such as your project id: |
| 78 | + |
| 79 | + `gsutil mb gs://<your-gcs-bucket>` |
| 80 | + `gsutil defacl set public-read gs://<your-gcs-bucket>` |
| 81 | + |
| 82 | +Next, gather all the static content locally into one folder using the Django `collectstatic` command |
| 83 | + |
| 84 | + `python manage.py collectstatic` |
| 85 | + |
| 86 | +Then upload it to CloudStorage using the `gsutil rsync` command |
| 87 | + |
| 88 | + `gsutil rsync -R static/ gs://<your-gcs-bucket>/static` |
| 89 | + |
| 90 | +Now your static content can be served from the following URL: |
| 91 | + |
| 92 | + `http://storage.googleapis.com/<your-gcs-bucket/static/` |
| 93 | + |
| 94 | +Make sure to replace <your-cloud-bucket> within `mysite/settings.py` to set STATIC_URL to the correct value to serve static content from, and |
| 95 | +uncomment the STATIC_URL to point to the new URL. |
| 96 | + |
| 97 | +The app can be deployed by running |
| 98 | + |
| 99 | + `gcloud preview app deploy app.yaml --set-default --version=1 --promote` |
| 100 | + |
| 101 | +which deploys to version 1, and `promote` makes version 1 the default version. |
| 102 | + |
| 103 | +Now you can view the admin panel of your deployed site at |
| 104 | + |
| 105 | +`https://<your-app-id>.appspot.com/admin` |
| 106 | + |
| 107 | +## Contributing changes |
| 108 | + |
| 109 | +* See [CONTRIBUTING.md](CONTRIBUTING.md) |
| 110 | + |
| 111 | + |
| 112 | +## Licensing |
| 113 | + |
| 114 | +* See [LICENSE](LICENSE) |
0 commit comments