Ghost from an NPM module and hosted in Azure

It was great to see you can now use Ghost as an NPM module. The upgrade process used to be a pain prior to the 1.0 release. Saying this, I ran into a few bumps trying to host this on Azure.

  1. Follow the docs on Ghost as an NPM module to get started
  2. Set server port at runtime

Node runs behind IIS and is reverse proxied so the port we are assigned is not static. In your index.js as setup in 1. add a line to set the port from the environment variable, so your ghost server start lines are as so:

ghost().then(ghostServer => {
    ghostServer.config.set('server:port', process.env.PORT);
    ghostServer.start();
});
  1. Add a file ‘iisnode.yml’ to the root of your project. (Without this change I got an infinite redirect loop inside Ghost) set its contents to be:

    enableXFF: true

This adds the X-Forwarded-Proto header in IIS and resolves the redirect loop issue

  1. You need to migrate the database on the host.

I used Kudu Git deploy but however you get your code onto your site, you can visit https://.scm.azurewebsites.net and head to the DebugConsole. Navigate to site/wwwroot so your cmd prompt is at the right location and run the node_modules\.bin\knex-migrator init to initialise the database

  1. Finally I also had an error as it was missing some node modules apparently. Running npm install again inside the kudu cmd prompt at site/wwwroot solved the issues.

Bonus Points - Add App Insights

Setup app insights for Node.js and add the SDK Script into the Blog Header under Ghost Settings -> Code Injection once your site is up and running