# Google App Engine

In this guide we are going to:

# New Strapi project

When the setup completes, register an admin user using the form which opens in the browser. This user will be only relevant in local development.

The sqlite database is created at .tmp/data.db.

Login, but don't add content types yet. Close the browser. Quit the running app.

# Initial commit

This may be a good point to commit the files in their initial state.

cd my-project
git init
git add .
git commit -m first

# Install the Cloud SDK CLI tool

Cloud SDK: Command Line Interface

# New App Engine project

Create a new App Engine project.

Select the region, such as europe-west.

  • Language: Node JS
  • Environment: Standard (or Flexible)

(A note on performance and cost: the Standard Environment is sufficient for development, but it may not be for production. Review the resources your application will need to determine the cost. When you sign up for Google App Engine, it offers a certain amount of free credits which will not be billed.)

Create the project. Take note of the instance identifier, which is in the form of <instance_id>:<region>:<instance_name>.

Check if gcloud lists the project:

gcloud projects list

Run init to authenticate the cli, and select current cloud project.

gcloud init

# Create the database (PostgreSQL)

Create the Cloud SQL database which the app is going to use.

Take note of the user name (default is postgres) and password.

The first database will be created with the name postgres. This cannot be deleted.

Create another database, named strapi for example. It may be useful to delete and and re-create this while you are experimenting with the application setup.

# Create app.yaml and .gcloudignore

Create the app.yaml file in the project root.

Add app.yaml to .gitignore.

The instance identifier looks like myapi-123456:europe-west1:myapi.

The myapi-123456 part is the project identifier. (The number is automatically added to short project names).

The following is an example config for Standard Environment or Flexible Environment.

Create .gcloudignore in the project root, include app.yaml here as well.

app.yaml
.gcloudignore
.git
.gitignore
node_modules/
#!include:.gitignore

In the case of Strapi, the admin UI will have to be re-built after every deploy, and so we don't deploy local build artifacts, cache files and so on by including the .gitignore entries.

# Configure the database

The PostgreSQL database will need the pg package.

yarn add pg

Google App Engine requires to connect to the database using the unix socket path, not an IP and port.

Edit database.json, and use the socket path as host.

config/environments/production/database.json
{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "bookshelf",
      "settings": {
        "client": "postgres",
        "host": "/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}",
        "database": "${process.env.DATABASE_NAME}",
        "username": "${process.env.DATABASE_USERNAME}",
        "password": "${process.env.DATABASE_PASSWORD}"
      },
      "options": {}
    }
  }
}

Edit server.json to pick up the deployed hostname from the HOST variable in app.yaml.

config/environments/production/server.json
{
  "host": "${process.env.HOST}",
  "port": "${process.env.PORT || 1337}",
  "production": true,
  "proxy": {
    "enabled": false
  },
  "cron": {
    "enabled": false
  },
  "admin": {
    "autoOpen": false
  }
}

# Auto-build after deploy

After deployment, the admin UI has to be re-built. This generates the contents of the build folder on the server.

In package.json, add the gcp-build command to scripts:

{
  "scripts": {
    "gcp-build": "strapi build"
  }
}

# Deploy

gcloud app deploy app.yaml --project myapi-123456

Watch the logs:

gcloud app logs tail --project=myapi-123456 -s default

Open the admin page and register and admin user.

https://myapp-123456.appspot.com/admin/

# File uploading to Google Cloud Storage

Lith/strapi-provider-upload-google-cloud-storage

yarn add strapi-provider-upload-google-cloud-storage

Deploy so that the server app includes the dependency from package.json.

Follow the documentation of the plugin for the full configuration.

# Post-setup configuration

CORS

CORS is enabled by default, allowing * origin. You may want to limit the allowed origins.

config/environments/production/security.json

Changing the admin url

config/environments/production/server.json
{
  "admin": {
    "path": "/dashboard"
  }
}