Search code examples
node.jsazure-web-app-servicestrapi

How do you host Strapi within an Azure App Service?


After inheriting a Strapi application hosted on Azure Container Instances, which has proved temperamental.

Given it's on the surface just a Nodejs application we want to move it to an Azure App Service to take advantage of hosting savings and also deployments via slot swapping.

The current documentation for Strapi includes guides for both Azure VM and deploying as a Docker conatainer. However we want to host as code on a windows based app service.

The reason for windows over linux is the time that Zip Deploy or Web Deploy taken when running on the linux version. Also we can run the App in an existing App Service plan.

The question is how to host Strapi on an Azure App Service?


Solution

  • After much trial and error we finally have this up and running, here are the steps that made it successful.

    Create the App Service

    Create an App Service that publishes "code", has a runtime of Node 14 LTS & has OS of Windows.

    Configure the App Service platform

    Once created navigate to "Configuration" of the App Service and make sure that WEBSITE_NODE_DEFAULT_VERSION is set to "~14". And on the "General Settings" tab update the platform to "64 bit". This ensures that the "sharp" package that is used by Strapi can run, as it requires 64 bit platform.

    After making these changes, navigate to the console and run the command node -p process.arch you should see the result is result is "x64".

    Application Settings

    Now to configure your application specific settings. For an out of the box Strapi app this is likely to include database configuration and any plugins like mail, identity and storage providers.

    For us the crucial part though was the settings to be used by Strapi to startup and host the application.

    This include the HOST & PUBLIC_URL settings.

    HOST = 0.0.0.0 and PUBLIC_URL = https://{APP_NAME}.azurewebsites.net/ (replace APP_NAME with the name of your app).

    Code changes

    There are a few changes needed to the out of the box Strapi app that are needed to run on Azure.

    1. Add a web.config to the route of the project. An example can be shown here: https://gist.github.com/bradleyisfluent/1033bb9dc908b2386c5ee09e0b36409f. This utilises IISNode to run server.js as the entry point to the app.
    2. Create a server.js in the root of the project. Contents should look like:

    const strapi = require("strapi"); strapi().start();

    1. Modify the current server.js within Strapi config /config/server.js to access the environment variables on the server, like this:

    host: process.env.HOST, port: process.env.PORT || 3000,

    n.b of course here, we are simply accessing environment variables and configuration it can be managed in different ways. The crucial thing though is to access the port via process.env.PORT which is implicitly set on the App Service.

    Deploy code

    This is where we are still a little bit of a work in progress. Utilising Azure Devops pipelines we are building and deploying the application.

    It appears that using "Run From Package" feature of Azure App Service doesn't not work for Strapi. My hypothesis is something is trying to write to the wwwroot dir which is read-only in the this setup and this causes an error.

    So we are using "webDeploy" which given the size of our built project (218mb, thanks node_modules) means the deployment times are slow. However this only applies to our preview builds as we can use slot swaps to release to Production.

    Here is a link to a slightly reducted version of a working azure-pipelines.yml: https://gist.github.com/bradleyisfluent/ffff8c1b6bedd5052274dc0d9f19a91d