IIS Hosting Golang Program
Objective
- host your golang app server with Internet Information Server (IIS)
- try to make IIS take full control of your golang app including lifecycle and recovery
- Googled
run golang in iis
- found there are two ways to achieve our goal (link):
- Run your Go app as a Windows service. Serve directly or via reverse proxy like the IIS ARR module
- Run your Go app behind IIS using the httpPlatformHandler (ref) (buggy, will not mention here)
- tried the httpPlatformHandler way and found it buggy
- tried the first solution where the instruction is as follows
Instruction
Preparation
- Provision an EC2 instance of Windows Server 2016
- Login with RDP
- Install IIS by clicking Add roles and features in Server Manager (ref)
- Download WebPI and install it
Setup Your Site in IIS
- Open IIS
- Open Application Pools and click Add Application Pool
- Fill out the form, choose No Managed Code in .NET CLR version field and submit.
- Create a folder where you like, for example:
C:\GUP
- Open Sites and click Add Website…
- Fill out the form and submit
- choose a name of your site
- select application pool that you just created
- fill physical path of content directory which is the folder you created (
C:\GUP
)
- determine which port you'd like to use
- leave the hostname blank
Install Required Modules
- Go to your site in IIS, and click WebPI
- search for URL Rewrite, Application Request Routing 3.0; select and install them
- after the installation is done, restart your IIS
Make a Reverse Proxy
- Go to your site in IIS, and click URL Rewrite
- Click Add Rule(s)… and choose Reverse Proxy
- enter the host you are going to forward requests, for example:
127.0.0.1:8888
; submit the form
Make Your Golang Program a Daemon
- Refer to go-svc and revise your program
- Once your revision got done, build it and open
cmd
as Administrator
- Enter the following command:
(notice the blank after =
sign)
- Go to Services via clicking Start button and searching for
Services
- Find the service you just create and start it
- Back to your site in IIS, click Browse Website
- See if it succeeds to forward your request to the daemon
Future Work
- app log to windows event log (ref1, ref2, ref3)
- passing arguments to a windows service