DUSTIN INGRAM: I'm Dustin Ingram, Developer Advocate
for Google Cloud Platform.
Today I'm going to show you how you
can run a Wagtail application on Google App Engine.
Wagtail is an open-source content management system
built on Django.
It's used by thousands of organizations
worldwide with a strong community.
In fact, Wagtail on App Engine is what powers the Google Cloud
Blog at cloud.google.com/blog.
It also looks great, is focused on user experience,
and offers precise control for both designers and developers.
App Engine makes it easy to deploy your Django
app to the cloud.
And since Wagtail is built with Django,
it's easy to get up and running with Wagtail as well.
Also, similar to the Django project,
Wagtail recently announced it has dropped support for Python
2, which makes it a great fit for the Python 3
Runtime on the App Engine standard environment.
A content management system consists primarily
of three parts--
an admin interface for creating and updating
your content, a database for storing your content,
and a frontend for rendering your content.
Wagtail comes with its own admin interface out of the box.
So the first thing we need to do to get started
is create a new database for our project.
Wagtail works with a number of different databases.
We'll choose Postgres.
On the Google Cloud Platform console,
we'll go to the Cloud SQL Instances page.
Here, we'll create a new instance
and select Postgres 9.6.
We'll just use a development database for now,
but once your app starts getting lots of traffic,
you'll probably want to use a production database instead.
Next, we'll specify an instance ID and default password.
Once the instance is initialized, we'll select it.
Nice.
Ready to go.
Select the Copy button next to Instance connection name
and hang onto this for later.
This string is what we'll use to tell our app how
to connect to our database.
Now, we'll go local.
Being able to run your app locally
makes it easier to test changes as you're
working, without needing to redeploy every time.
In order to run our Wagtail app locally,
we'll need a way to connect to the database we just
created from our local machine.
We could connect directly to it, but this
would require that we allow our database
to accept incoming connections directly
from our local IP address, which would make it slightly less
secure.
Instead, we'll use a tool called Cloud SQL Proxy.
This is a utility that can run on our development machine that
creates a secure tunnel between our machine and the Cloud SQL
database.
Then we could configure our app to talk to this local proxy
instead when running in development mode.
Installing the Cloud SQL Proxy is easy.
See the link in the video description for instructions
for your platform.
We'll start the Cloud SQL Proxy with this command.
You'll use the instance name you copied in the previous step
here.
We'll leave this running in a separate terminal
while we continue.
Now in a new terminal, we'll install the Wagtail package
locally.
I'm already in a virtual environment
that I created for this project.
If you're not familiar with virtual environments,
see the link in the video description
for guides for setting up and activating
a virtual environment.
I highly recommend that any Python project
starts with its own virtual environment.
Installing the Wagtail package gives us the Wagtail command,
which we can use to generate a cookie cutter Wagtail project.
We'll call ours mysite.
So we'll use the command Wagtail start mysite.
This will create a directory called mysite where
our Wagtail app will live.
Let's go into that directory and see what
our starter app looks like.
There's a few things in here that might look familiar.
Most of the core of our app lives in mysite.
The search and home directories are the subpages
that we get by default. We need to update our Wagtail's app
settings so that it has the right information
to interact with our database.
The development settings go in a file at mysite/settings/dev.py.
We'll tell it how to interact with our Cloud SQL Proxy, which
should still be running, by specifying
local host as our host.
Now we need to do the same for our production settings.
Ideally, any secret like a database password
would be encrypted with an encryption key
from Cloud Key Management Service
and held in cloud storage, only to be decrypted at runtime.
But we'll just include our password in the source code
here to simplify things.
But be sure to read the guide to secret management
in the video description.
Similarly, we'll set a secret key
which will be used for cryptographic signing
to secure our tokens, cookies, et cetera,
by just assigning it in the source.
However, in production, this should be stored as a secret
as well.
We'll also need to tell our app which host
names it should respond to.
App Engine gives you an appspot.com domain by default,
so we'll use that here.
Next, we're going to migrate our database
and start up our app locally.
Since we opted to use Postgres as our database,
we'll need to add the psychopg2 Python package
to the requirements.text file at the root of our project's
mysite directory.
Then let's install everything.
We'll run the migrate command to get our database initialized.
Finally, we'll create an administrator account
that we'll use to login to our Wagtail app.
Running Python manage.py createsuperuser
will prompt us for our username and password
for our admin Wagtail user.
Now we can run Python manage.py runserver
to start the server locally.
The Wagtail tail app will be visible on local hosts
at port 8000 by default. Let's check it out in our browser
by visiting http://127.0.0.1:8000.
Once there, we should see this generic welcome message.
Now that we have the app configured, let's deploy it.
There's three things we need to do before deploying.
First, we need to collect all our static assets,
like CSS and JavaScript into a single directory.
The collectstatic command will do this for us.
Next we need to create a main.py file.
This is where App Engine looks for an app variable which
should be your application.
Wagtail puts this elsewhere and calls it application instead,
so we just need to connect the dots.
Finally, we need to create an app.yaml file to hold our App
Engine configuration.
We'll tell it to use the Python 3 Runtime,
and we'll setup handlers for our static assets
and for our other routes.
We'll also tell it to use our production settings once
deployed.
When we're done, our mysite directory
should look something like this.
Now all we need to do is deploy with gcloud app deploy.
Once the deployment is finished, we
can go visit our app at its URL on appspot.com,
and we'll see that same familiar welcome screen.
We can also sign in with the super user account
that we created before.
Looking great.
We've got a content management system.
Now all that's left to do is add the content,
which is easy with Wagtail's web interface.
From here, you might also want to start
working on your app's frontend to create a more
engaging experience for your visitors,
or add some models to begin defining your business logic.
I've shown you how to set up a Cloud SQL database,
create a new Wagtail project, and deploy it to App Engine's
new Python 3 Runtime.
And I'm confident that you'll be able to take this and build
something great on Google Cloud.
The best part about App Engine is that it
grows with your application.
When you aren't getting traffic, you don't pay anything.
And as your app becomes more popular and traffic increases,
it scales automatically to meet that demand.
Stay tuned for the next episode, where
I'll show you how to use Stackdriver
to monitor your Python 3 app.
Feel free to ask questions in the comments below.
And if you liked this video, please
subscribe to the Google Cloud Platform channel
for more videos like this.
Thanks for watching.
[MUSIC PLAYING]