# Google App Engine
In this guide we are going to:
- Create a new Strapi project
- Configure PostgreSQL for the production enviroment
- Deploy the app to Google App Engine
- Add the Google Cloud Storage file uploading plugin (opens new window) by @Lith (opens new window)
# 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 (opens new window)
# New App Engine project
Create a new App Engine (opens new window) 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 (opens new window) 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.
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 (opens new window) to connect to the database using the unix socket path, not an IP and port.
Edit database.js
, and use the socket path as socketPath
for a MySQL database or as host
for a PostgreSQL database.
Path: ./config/env/production/database.js
.
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'postgres',
host: `/cloudsql/${env('INSTANCE_CONNECTION_NAME')}`, // for a MySQL database
// ⚠️ For a PostgreSQL database, use socketPath: `/cloudsql/${env('INSTANCE_CONNECTION_NAME')}` instead
database: env('DATABASE_NAME'),
username: env('DATABASE_USERNAME'),
password: env('DATABASE_PASSWORD'),
},
options: {},
},
},
});
# 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 (opens new window)
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 (opens new window) for the full configuration.
# Post-setup configuration
CORS
CORS is enabled by default, allowing *
origin. You may want to limit the allowed origins.
Read the documentation here.
Changing the admin url
config/env/production/server.js
module.exports = {
admin: {
path: '/dashboard',
},
};