Sitemap

Deploying Strapi on a Windows IIS Server with Azure DevOps

7 min readOct 10, 2022
Press enter or click to view image in full size

Hosting a Strapi & NodeJS application on a Windows Server is not hard but it can be a big task when done without proper documentation. Assuming you already have a VM with Windows Server (with IIS) setup, you can skip some of the steps below. This is all hosted on Azure. If you don’t have an account, you can register one here. Note * Not advertising Strapi or Azure here.

There are several NodeJS process managers available, unfortunately, none of them supports hosting on a Windows server, to name a few :

  1. PM2 — PM2 is a NodeJS process manager with a built-in load balancer. It is one of the most used, highly capable, and well-documented process managers available.
  2. StrongLoop PM — StrongLoop PM is a NodeJS production process manager that helps you to build and deploy your NodeJS application and run its process and clusters in the production environment.

After all our failed attempts of using process managers, we decided to run NodeJS as a service on our Windows Server and then expose it using reverse proxy on IIS.

Configuring IIS on Windows Server
To enable IIS and the required IIS components on Windows Server, do the following: (If you already setup IIS, continue to the next steps)

  • Open Server Manager and click Manage > Add Roles and Features.
  • Select Role-based or feature-based installation.
  • Select the appropriate server. The local server is selected by default.
  • Enable Web Server (IIS).
  • No additional features are necessary to install the Web Adapter.
  • On the Web Server Role (IIS) dialog box.
  • On the Select role services dialog box, verify that the web server components listed in the next section are enabled.
  • Verify that your settings are correct and click install.
  • When the installation completes, close to exit the wizard.

Setup a Website in IIS
This will help you step by step through setting up a new website in Internet Information Services (IIS). Based on Windows Server with IIS 10, but the same basic procedures apply to all Windows (Server as well) & IIS versions.

  • Open Internet Information Services (IIS) Manager. You can find it quickly by typing “IIS” in the search field.
Press enter or click to view image in full size
  • Expand your computer’s directory by clicking the > to the left of the icon in the left-hand pane. Then, right-click sites and select add website.
  • The add website window will open. First, add an easy-to-remember name for the website in the site name field.
  • Navigate to the directory containing your website’s files so that it appears in the physical path field.
  • Under binding, select the type (HTTP or HTTPS), IP address, port, and host name. (Note, you need a SSL Certificate if you chose HTTPS, use Certify the web, links somewhere below)
  • If you would like to start the website right away, make sure that start website immediately is checked. If not, uncheck it.
  • Click the OK button to finish creating your new website.

Reverse Proxy with IIS
For running the Strapi application on Windows Server, we will create an additional layer of reverse proxy with the built-in web service manager IIS.

  • Make sure the IIS is running on your system. You can start it from the “Turn windows feature on and off” setting in windows 10.
  • To ensure that IIS is running, the Internet Information Services checkbox should be selected and on running http://localhost. A windows IIS site should appear.
  • Install “Microsoft Web Platform Installerif not present already. Run the application. If a User Account Control box appears. The Web Platform Installer window appears.
  • Search by typing “Application Request Routing” in the search box.
  • Click the add button in the row labeled Application Request Routing 2.5 with KB2589179. If there’s a newer version, you could choose that one instead.
  • Back in the search box, type “URL Rewrite” and press enter and add URL Rewrite 2.1.
  • To set up a rewrite URL, start IIS manager. (If it was already opened, please close and start it again to reflect the installations).
  • Go to sites by clicking on your system name in the connections pane.
  • Under sites select default website.
  • Double click URL rewrite to open its features, add a new rule.
  • Select reverse proxy and then click on “OK” as shown below.
  • Enter the server name or IP address where HTTP requests will be forwarded. This is where your Node.js server is listening. If you set up your environment according to this guide, this will be localhost:3000. You can leave the other options set to their default and click OK.
  • We’re almost done. With that rule saved, IIS will proxy any traffic heading towards localhost to localhost:3000. You can test that in your web browser.
  • Here is an example of the setup, code & screenshot :

<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<preConditions>
<preCondition name=”ResponseIsHtml1">
<add input=”{RESPONSE_CONTENT_TYPE}” pattern=”^text/html” />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name=”Backend_Rule” enabled=”true” stopProcessing=”true”>
<match url=”(.*)” />
<conditions logicalGrouping=”MatchAny”>
<add input=”{HTTP_HOST}” pattern=”^api.domain.com$” />
</conditions>
<action type=”Rewrite” url=”http://localhost:1337/{R:1}” />
</rule>
<rule name=”Frontend_Rule” enabled=”true”>
<match url=”(.*)” />
<conditions logicalGrouping=”MatchAny”>
<add input="{HTTP_HOST}" pattern=”^sub.domain.com$” />
</conditions>
<action type=”Rewrite” url=”http://localhost:3000/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Press enter or click to view image in full size

Create a Windows Service to run our Node.js App automatically

  • Create a Windows service to ensure your Node.js website is always running and automatically restarted if it ever crashes. There are many tools for this, we will be going ahead with the free tool NSSM (non sucking service manager). You can download it from the official website at nssm.cc.
  • After downloading, run the executable nssm.exe from downloads\nssm-2.24\nssm-2.24\win64.
  • Stop the Strapi application if it is still running.
  • To get the service installation GUI run, run the command below :
nssm install <serviceName>
  • On running the above command, an installation window opens up as below.
  • Enter the application path as the path to your npm.cmd file — C:\Program Files\nodejs\npm.cmd
  • Set startup directory as path to your Strapi project.
  • Set Arguments as “run start”
  • Enter any service name and click on Install service, ie. WebsiteName_Frontend or WebsiteName_Backend
  • Then start the service by running the below command :
nssm start <serviceName>
  • To confirm the service status, run the command below :
nssm status <serviceName>
  • Go to localhost:1337, your app should be working now.

SSL Certificate Setup
I used Certify The Web in this instance, easy & quick setup. Or you can use Certbot (from EFF) as well. I’ll leave it to you to figure out, set it up & configure. Google is your friend 😜

Azure DevOps Setup
If you don’t use Azure DevOps already, you should start using it. This is based on my setup, keep in mind everyone have different requirements. You also need to setup the Server (VM) under deployment groups in your pipeline.

This is the Build Pipeline setup : (This is one .yaml file)

trigger:
- staging

pool:
vmImage: windows-latest

steps:
- task: NodeTool@0
inputs:
versionSpec: ‘16.15.x’
displayName: ‘Install Node.js’

- task: Npm@1
displayName: ‘NPM Backend Install’
inputs:
command: custom
workingDir: ‘$(Build.SourcesDirectory)/backend/’
verbose: false
customCommand: ‘install — legacy-peer-deps’

- task: Npm@1
displayName: ‘NPM Backend Build’
inputs:
command: custom
workingDir: ‘$(Build.SourcesDirectory)/backend/’
verbose: false
customCommand: ‘run build’

- task: Npm@1
displayName: ‘NPM Frontend Install’
inputs:
command: custom
workingDir: ‘$(Build.SourcesDirectory)/frontend/’
verbose: false
customCommand: ‘install — legacy-peer-deps’

- task: Npm@1
displayName: ‘NPM Frontend Build’
inputs:
command: custom
workingDir: ‘$(Build.SourcesDirectory)/frontend/’
verbose: false
customCommand: ‘run build’

- task: ArchiveFiles@2
displayName: ‘Archive’
inputs:
rootFolderOrFile: ‘$(Build.SourcesDirectory)’

- task: PublishPipelineArtifact@1
displayName: ‘Publish Pipeline Artifact’
inputs:
targetPath: ‘$(Build.SourcesDirectory)’
artifact: ArtifactName

This is the Release Pipeline setup : (Note this is different steps in separate .yaml files)

steps:
- powershell: ‘taskkill /im node.exe /F’
errorActionPreference: silentlyContinue
warningPreference: silentlyContinue
informationPreference: silentlyContinue
verbosePreference: silentlyContinue
debugPreference: silentlyContinue
displayName: ‘Stop Node’
continueOnError: true

steps:
- task: IISWebAppManagementOnMachineGroup@0
displayName: ‘Stop IISApplicationPool’
inputs:
IISDeploymentType: IISApplicationPool
ActionIISApplicationPool: StopAppPool
ParentWebsiteNameForApplication: WebsiteName
StartStopRecycleAppPoolName: WebsiteName

steps:
- task: IISWebAppManagementOnMachineGroup@0
displayName: ‘Update WebsiteName Website’
inputs:
WebsiteName: WebsiteName
WebsitePhysicalPath: ‘%SystemDrive%\path\to\website’
continueOnError: true

steps:
- task: IISWebAppDeploymentOnMachineGroup@0
displayName: ‘Deploy WebsiteName Website/App’
inputs:
WebSiteName: WebsiteName
Package: ‘$(System.DefaultWorkingDirectory)\**\*.zip’

steps:
- task: IISWebAppManagementOnMachineGroup@0
displayName: ‘Start IISApplicationPool’
inputs:
IISDeploymentType: IISApplicationPool
ActionIISApplicationPool: StartAppPool
ParentWebsiteNameForApplication: WebsiteName
StartStopRecycleAppPoolName: WebsiteName

steps:
- powershell: ‘Start-Process -FilePath “C:\Program Files\nodejs\node.exe”’
displayName: ‘Start Node’

steps:
- powershell: ‘Restart-Service -Name WebsiteName_Backend’
displayName: ‘Restart Backend’

steps:
- powershell: ‘Restart-Service -Name WebsiteName_Frontend’
displayName: ‘Restart Frontend’

That’s all, with all the steps completed successfully, your Strapi + NodeJS application is now hosted on a Windows server. This is all tested on my setup I’m currently running! I know it might be a long & dragging article but I tried to explain it with a lot of details (+ screenshots) to avoid confusion.

Please feel free to reach out to me if you require additional info & please point out if I missed something or if anything is unclear✌🏻🤘🏻

--

--

Jo West
Jo West

Written by Jo West

0 followers

Thirty Something | Weirdo | Wizkid | Diabetic

Responses (3)