Powered By GitBook
ghost-on-app-engine-part-1-deploying
Learn how to deploy a Ghost blog to App Engine flexible environment.
Contributed by Google employees.
This tutorial explains how to deploy and scale a Ghost blog on the App Engine flexible environment.
Ghost is a simple blogging platform that can be self-hosted. It's built with Node.js, and it can be customized or transformed into a bigger site. It serves as a template for a larger application.
App Engine makes it easy to run web applications that must scale to meet worldwide demand. It lets you focus on your code without having to worry about operations, load balancing, servers, or scaling to satisfy incoming traffic.
App Engine can take a Ghost web application and scale it to handle your growing global demand, while giving you all the benefits of Google Cloud, including Cloud SQL, Cloud Source Repositories, Cloud Debugger, Error Reporting, Logging, Monitoring, Trace, and more.

Objectives

    Create a Cloud SQL instance, a database, and a user.
    Download Ghost.
    Run Ghost locally.
    Configure Ghost for App Engine.
    Deploy Ghost to App Engine.

Costs

This tutorial uses billable components of Google Cloud, including:
    Google Cloud SQL
    App Engine flexible environment
Use the pricing calculator to generate a cost estimate based on your projected usage.

Before you begin

    1.
    Select or create a Google Cloud project.
    2.
    Enable billing for your project. Enable billing.
    3.
    Install the Cloud SDK.
    4.
    Authenticate gcloud with Google Cloud:
    1
    gcloud init
    Copied!
    5.
    Create a new Second Generation Cloud SQL instance. You can do this from the Cloud Console or via the Cloud SDK.
      1.
      In order for some of the commands below to work, you need to enable the
      2.
      Create it via the following SDK command:
      1
      gcloud sql instances create YOUR_INSTANCE_NAME \
      2
      --activation-policy=ALWAYS \
      3
      --tier=db-f1-micro
      Copied!
      YOUR_INSTANCE_NAME is a name of your choice. You may want to set your region: --region=YOUR_REGION_NAME
      3.
      Set the root password on your Cloud SQL instance:
      1
      gcloud sql instances set-root-password root% YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
      Copied!
      YOUR_INSTANCE_NAME is the name you chose in step 1. YOUR_INSTANCE_ROOT_PASSWORD is a password of your choice.
      4.
      Create and download a service account key file for your project. You will use this service account to connect to your Cloud SQL instance locally.
      5.
      Download and install the Cloud SQL Proxy.
      6.
      ​Start the proxy to allow connecting to your instance from your local machine:
      1
      ./cloud_sql_proxy \
      2
      -instances=YOUR_INSTANCE_CONNECTION_NAME=tcp:3306 \
      3
      -credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON_FILE &
      Copied!
      YOUR_INSTANCE_CONNECTION_NAME is the connection name of your instance on its overview page in the Cloud Console, or use YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME.
      7.
      Use the MySQL command-line tools (or a management tool of your choice) to create a new user and database for your application:
      1
      mysql -u root -p -h 127.0.0.1
      2
      mysql> create database `YOUR_DATABASE`;
      3
      mysql> create user 'YOUR_USER'@'%' identified by 'PASSWORD';
      4
      mysql> grant all on YOUR_DATABASE.* to 'YOUR_USER'@'%';
      Copied!
      You will be asked to enter the root password you chose earlier.
      8.
      Set the MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE environment variables (see below). This allows your local Ghost app to connect to your Cloud SQL instance through the proxy.
      9.
      In adding Cloud SQL, dependencies need to be added in package.json for Google Cloud Node.
      1
      "dependencies": {
      2
      "express": "^4.16.3",
      3
      "mysql": "^2.15.0",
      4
      "prompt": "^1.0.0"
      5
      }
      Copied!

Install Ghost as an NPM module

Follow the instructions on the Ghost website to install Ghost.

Configure

    1.
    Create a config.development.json file from the default configuration file:
    1
    cp node_modules/ghost/core/server/config/env/config.development.json config.development.json
    Copied!
    2.
    Create a config.production.json file from the default config file:
    1
    cp node_modules/ghost/core/server/config/env/config.production.json config.production.json
    Copied!

Run the app locally

    1.
    Edit config.development.json and set it to the following:
    1
    {
    2
    "url": "http://localhost:2368",
    3
    "fileStorage": false,
    4
    "mail": {},
    5
    "database": {
    6
    "client": "mysql",
    7
    "connection": {
    8
    "host": "127.0.0.1",
    9
    "user": "YOUR_MYSQL_USERNAME",
    10
    "password": "YOUR_MYSQL_PASSWORD",
    11
    "database": "YOUR_MYSQL_DATABASE_NAME",
    12
    "charset": "utf8"
    13
    },
    14
    "debug": false
    15
    },
    16
    "paths": {
    17
    "contentPath": "content/"
    18
    },
    19
    "privacy": {
    20
    "useRpcPing": false,
    21
    "useUpdateCheck": true
    22
    },
    23
    "useMinFiles": false,
    24
    "caching": {
    25
    "theme": {
    26
    "maxAge": 0
    27
    },
    28
    "admin": {
    29
    "maxAge": 0
    30
    }
    31
    }
    32
    }
    Copied!
    2.
    Install dependencies:
    1
    npm install --production
    Copied!
    3.
    Start the app:
    1
    npm start
    Copied!
    4.
    To view the app, browse to:
    1
    http://localhost:2368
    Copied!
    5.
    Now stop your app by pressing Ctrl+C.

Deploy

    1.
    Edit config.production.json and set it to the following:
    1
    {
    2
    "url": "https://YOUR_PROJECT_ID.appspot.com",
    3
    "fileStorage": false,
    4
    "mail": {},
    5
    "database": {
    6
    "client": "mysql",
    7
    "connection": {
    8
    "user": "YOUR_MYSQL_USERNAME",
    9
    "password": "YOUR_MYSQL_PASSWORD",
    10
    "database": "YOUR_MYSQL_DATABASE_NAME",
    11
    "charset": "utf8"
    12
    },
    13
    "debug": false
    14
    },
    15
    "server": {
    16
    "host": "0.0.0.0",
    17
    "port": "8080"
    18
    },
    19
    "paths": {
    20
    "contentPath": "content/"
    21
    },
    22
    "logging": {
    23
    "level": "info",
    24
    "rotation": {
    25
    "enabled": true
    26
    },
    27
    "transports": ["file", "stdout"]
    28
    }
    29
    }
    Copied!
    Here's some information about each setting:
      url: The URL at which the blog will be deployed. This is the URL users will use to access the blog.
      fileStorage: Setting this value to false forces image uploads to use an image URL because App Engine doesn't have persistent disks. Without this setting, any photos uploaded to the blog will eventually disappear.
      mail: Configure this setting according to the instructions at http://support.ghost.org/mail/.
      database: Tells Ghost how to connect to the Cloud SQL instance.
      server: Tells Ghost how to listen for web traffic.
    2.
    Prepare for deployment. Create an app.yaml file with the following contents:
    1
    runtime: nodejs
    2
    env: flex
    3
    manual_scaling:
    4
    instances: 1
    5
    env_variables:
    6
    MYSQL_USER: YOUR_MYSQL_USER
    7
    MYSQL_PASSWORD: YOUR_MYSQL_PASSWORD
    8
    MYSQL_DATABASE: YOUR_MYSQL_DATABASE
    9
    # e.g. my-awesome-project:us-central1:my-cloud-sql-instance-name
    10
    INSTANCE_CONNECTION_NAME: YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME
    11
    beta_settings:
    12
    # The connection name of your instance on its Overview page in the
    13
    # Cloud Console, or use `YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME`
    14
    cloud_sql_instances: YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME
    15
    skip_files:
    16
    - ^(.*/)?#.*#$
    17
    - ^(.*/)?.*~$
    18
    - ^(.*/)?.*\.py[co]$
    19
    - ^(.*/)?.*/RCS/.*$
    20
    - ^(.*/)?\..*$
    21
    - ^(.*/)?.*\.ts$
    22
    - ^(.*/)?config\.development\.json$
    Copied!
    Here's some information about each setting:
      runtime: Tells App Engine to use the Node.js runtime.
      manual_scaling: Forces App Engine to run one and only one instance. To automatically scale, remove this setting or change to automatic_scaling and configure according to the documentation.
      resources: You didn't change this setting, but the default instance size corresponds to a g1.small virtual machine. You can configure smaller or larger instances sizes as required. See the documentation.
      Read more about using app.yaml.
    3.
    Migrate the database to allow use in production:
    1
    NODE_ENV=production knex-migrator init --mgpath node_modules/ghost
    Copied!
    4.
    Add "socketPath": "/cloudsql/YOUR_INSTANCE_NAME" in the connection properties section of your config.production.json, so you end up with the following:
    1
    {
    2
    "url": "http://YOUR_PROJECT_ID.appspot.com",
    3
    "fileStorage": false,
    4
    "mail": {},
    5
    "database": {
    6
    "client": "mysql",
    7
    "connection": {
    8
    "socketPath": "/cloudsql/YOUR_INSTANCE_NAME",
    9
    "user": YOUR_MYSQL_USERNAME,
    10
    "password": YOUR_MYSQL_PASSWORD,
    11
    "database": YOUR_MYSQL_DATABASE_NAME,
    12
    "charset": "utf8"
    13
    },
    14
    "debug": false
    15
    },
    16
    "server": {
    17
    "host": "0.0.0.0",
    18
    "port": "8080"
    19
    },
    20
    "paths": {
    21
    "contentPath": "content/"
    22
    },
    23
    "logging": {
    24
    "level": "info",
    25
    "rotation": {
    26
    "enabled": true
    27
    },
    28
    "transports": ["file", "stdout"]
    29
    }
    30
    }
    Copied!
It's very important that you only do this step after migrating the database. The socketPath property is required to deploy on App Engine, but it causes knex-migrator to throw an error.
    1.
    Deploy the app:
    1
    gcloud app deploy
    Copied!
    2.
    After deployment completes, view your deployed app at https://YOUR_PROJECT_ID.appspot.com (in which YOUR_PROJECT_ID is your Google Cloud project ID).

What's next

Last modified 7mo ago