Home >Backend Development >Golang >golang deploy iis
With the gradual popularity of Golang in Web development, more and more developers are beginning to use Golang to build Web applications. When deploying Golang applications, many people are accustomed to using Linux servers, but for users using Windows servers, how to deploy Golang applications on IIS? This article will introduce how to deploy Golang applications on IIS.
Step 1: Install Golang
First, you need to install Golang on the Windows server. You can download the latest Golang installer from the official website https://golang.org/dl/, and then follow the prompts to install it.
After the installation is complete, you need to add Golang’s bin directory to the system’s PATH environment variable.
Step 2: Create a simple Golang application
Before deploying the Golang application on IIS, you need to write a simple Golang application for testing. The following is a simple HTTP server program that will return a "Hello, World!" HTTP response:
package main
import (
"fmt" "net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") }) http.ListenAndServe(":80", nil)
}
This program will listen to port 80 and provide HTTP service on the root path. When accessing this path, the server will return a "Hello , World!” response.
Save the above code as a .go file and use the following command to compile the program:
go build 2334ac29606bf8a170583e4f7533b1f4.go
Where, 2334ac29606bf8a170583e4f7533b1f4 is The name of the file you saved.
Run the program:
./2334ac29606bf8a170583e4f7533b1f4
If everything goes well, the program will display the "Hello, World!" message.
Step 3: Install IIS
In order to run Golang applications on IIS, you first need to install IIS. You can use Windows Server Manager to install IIS. The specific steps are as follows:
Open Windows Server Manager
Select the "Roles and Features" option
In the "Server Roles" option Select the "Web Server (IIS)" option
Select the "Web Server" and "ASP.NET 4.5" options in the "Web Server (IIS)" option
Click the "Install" button , wait for the IIS installation to complete
Once the installation is complete, you can verify whether IIS was installed successfully by entering the server IP address in the browser.
Step 4: Deploy the Golang application on IIS as a CGI script
One way to run the Golang application on IIS is to deploy it on IIS as a CGI script, specifically The steps are as follows:
Create a new website or application pool
Open IIS Manager, select the "Sites" or "Application Pools" option, and then click "Add Website" or "Add Application Pool" button.
Configure the website or application pool
Enter the name, path and other information in the "Add Website" or "Add Application Pool" dialog box. If you are creating a website, you need to specify the root directory of the website and the bound IP address and port; if you are creating an application pool, you need to specify the .NET version used, authentication method and other information.
Deploy the Golang application as a CGI script
Create a new directory and place the compiled Golang application in this directory. Create a web.config file in this directory, which contains information about how IIS runs CGI scripts. You can use the following web.config as a reference:
52b189f45abba88b2989c5c1f30b7a34
f9d9f4a8f32d97f3ef0c10742ed31240
<system.webServer> <handlers> <add name="CGI for Golang" path="*.go" verb="*" modules="CgiModule" scriptProcessor="<path to cgi-bin>go.exe" resourceType="Unspecified" /> </handlers> </system.webServer>
1856eee6ebcd516d11f9c6ed6bb0cd46
Among them, 5df9a279c7977db155825f62f22cd817 is the bin directory of Golang.
Click the website or application pool created in IIS Manager, select the "Add Virtual Directory" option in the "Actions" box on the right, and specify the path to the directory created in the previous step.
Test Application
Open the browser, enter the IP address and port of the website, and you can see the response of "Hello, World!"
Step Five: Deploy the Golang application on IIS using ISAPI
Compared with deploying the Golang application on IIS as a CGI script, deploying it on IIS using ISAPI can Improve performance and security. The specific steps are as follows:
Download and install Go-ISAPI
You can download the latest version of Go-ISAPI from https://github.com/Zer0xFF/goisapi and follow the README.md file Install according to the instructions in .
Create a website or application pool on IIS
Similarly, open IIS Manager, select "Sites" or "Application Pools", and then click "Add Website" or "Add Application Pool" button.
Configure the website or application pool
Similarly, enter the name, path and other information in the "Add Website" or "Add Application Pool" dialog box.
Configure ISAPI Filter
Right-click the created website or application pool, and in "Manage Website" or "Advanced Settings", find "ISAPI Filters" or "ISAPI and CGI Restrictions" ” option and then select the “Add” option to add a new filter. Enter the name and path to the executable file and select the Allow option to allow the filter to run.
Configure ISAPI extension
In IIS Manager, select the "Server Farm" option, then select "ISAPI and CGI" or "Handler Mappings", click the "Add Module Mapping" or "Add Module Mapping" button, a new ISAPI extension will be inserted program. Enter the name, path, and command line parameters, then click OK to save.
Test Application
Open the browser, enter the IP address and port of the website, and you can see the response of "Hello, World!"
Conclusion
This article introduces how to deploy Golang applications to IIS. Golang applications can be easily deployed to IIS using CGI scripts and using ISAPI. If you are using a Windows server and want to leverage Golang to build a web application, please follow the steps above to try deploying your Golang application.
The above is the detailed content of golang deploy iis. For more information, please follow other related articles on the PHP Chinese website!