南宁做网站哪家公司好,做网站的封面图片哪里才有,网络推广平台免费,鸟人高端网站建设文章目录 1. 打开自己的云服务器的 80 和 443 端口2. 安装 nginx3. 安装 snapd4. 安装 certbot5. 生成证书6. 拷贝生成的证书到项目工作目录7. 修改 main.go 程序如下8. 编译程序9. 启动程序10. 使用 https 和端口 8081 访问页面成功11. 下面修改程序#xff0c;支持 https 和… 文章目录 1. 打开自己的云服务器的 80 和 443 端口2. 安装 nginx3. 安装 snapd4. 安装 certbot5. 生成证书6. 拷贝生成的证书到项目工作目录7. 修改 main.go 程序如下8. 编译程序9. 启动程序10. 使用 https 和端口 8081 访问页面成功11. 下面修改程序支持 https 和 http 能同时访问12. 编译13. 启动程序14. 使用 http 和 8080 端口访问成功15. 使用 https 和 8081 端口访问成功 1. 打开自己的云服务器的 80 和 443 端口
打开某为云官网 https://console.huaweicloud.com/ 2. 安装 nginx
sudo apt update
sudo apt-get install nginx
nginx -v3. 安装 snapd
sudo apt install snapd
sudo apt-get remove certbot4. 安装 certbot
sudo snap install --classic certbot5. 生成证书
sudo certbot certonly --nginx6. 拷贝生成的证书到项目工作目录
cd ~/dev/go/screen_share
mkdir conf
cp /etc/letsencrypt/live/www.liangzixuexi.com/fullchain.pem ~/dev/go/screen_share/conf/
cp /etc/letsencrypt/live/www.liangzixuexi.com/privkey.pem ~/dev/go/screen_share/conf/7. 修改 main.go 程序如下
package mainimport (fmtnet/http
)func main() {// 1.定义一个 URL 前缀staticURL : /static/// 2.定义一个 FileServerfs : http.FileServer(http.Dir(./static))// 3.绑定 url 和 FileServerhttp.Handle(staticURL, http.StripPrefix(staticURL, fs))// 4.启动 HttpServer//err : http.ListenAndServe(:8080,nil)err : http.ListenAndServeTLS(:8081, ./conf/fullchain.pem, ./conf/privkey.pem, nil)if err ! nil {fmt.Println(err)}
}8. 编译程序
sh build.sh#!/bin/bash
go build -o screen_share src/*9. 启动程序
./screen_share10. 使用 https 和端口 8081 访问页面成功
https://www.liangzixuexi.com:8081/static/share.html但是现在只能通过 https 访问原来的 http 不能访问了
11. 下面修改程序支持 https 和 http 能同时访问
package mainimport (fmtnet/http
)func startHttp(port string){fmt.Printf(Start Http port: %s\n, port)err : http.ListenAndServe(port, nil)if err ! nil {fmt.Println(err)}
}func startHttps(port, cert, key string){fmt.Printf(Start Https port: %s\n, port)err : http.ListenAndServeTLS(port, cert, key, nil)if err ! nil {fmt.Println(err)}
}func main() {// 1.定义一个 URL 前缀staticURL : /static/// 2.定义一个 FileServerfs : http.FileServer(http.Dir(./static))// 3.绑定 url 和 FileServerhttp.Handle(staticURL, http.StripPrefix(staticURL, fs))// 4.启动 HttpServer//err : http.ListenAndServe(:8080,nil)go startHttp(:8080)//err : http.ListenAndServeTLS(:8081, ./conf/fullchain.pem, ./conf/privkey.pem, nil)// 5.启动 HttpsServerstartHttps(:8081, ./conf/fullchain.pem, ./conf/privkey.pem)
}12. 编译
sh build.sh13. 启动程序
./screen_share14. 使用 http 和 8080 端口访问成功
输入 http://www.liangzixuexi.com:8080/static/share.html 15. 使用 https 和 8081 端口访问成功
输入 https://www.liangzixuexi.com:8081/static/share.html