nginx 服务搭建


本文摘自网络,作者,侵删。

ubuntu 18.04 环境下搭建 nginx 服务,跑 go 程序;

参考地址:
如何在Ubuntu 18.04上使用Nginx部署Go Web应用程序
https://www.howtoing.com/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04

  1. 安装 nginx

sudo apt-get install nginx

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

防火墙端口的设置

iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT

  1. golang 安装

add-apt-repository ppa:longsleep/golang-backports
apt-get update
sudo apt-get install golang-go

鉴定是否安装成功
go version
配置信息查看
go env
$GOPATH

  1. 部署 go 程序

/root/go/main.go

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello World")
    })

    http.HandleFunc("/greet/", func(w http.ResponseWriter, r *http.Request) {
        name := r.URL.Path[len("/greet/"):]
        fmt.Fprintf(w, "Hello %s\n", name)
    })

    http.ListenAndServe(":9990", nil)
}

然后编译可执行文件:
go build main.go

设置系统单元文件:
在 /lib/systemd/system/ 下创建 goweb.service 文件,并写以下内容

[Unit]
Description=goweb

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/root/go/main

[Install]
WantedBy=multi-user.target

启动服务:
sudo service goweb start
sudo service goweb stop
sudo service goweb status

  1. negix 设置反向代理

进入 cd /etc/nginx/sites-available 然后创建 xxx.com 文件,然后写入

server {
    server_name xxx.com www.xxx.com;

    location / {
        proxy_pass http://localhost:9990;
    }
}

创建连接:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/your_domain

然后重新启动 nginx 服务:

sudo nginx -s reload

然后现在访问 http://xxx.com 就看到的服务返回的内容了;


本文来自:简书

感谢作者:that_is_this

查看原文:nginx 服务搭建

相关阅读 >>

Golang协程详解和应用

Go 中使用 json 时,如何区分空字段和未设置字段

Go 使用pprof 排查内存泄露

聊聊cortex的readring

Golang不可重入函数实现

Golang elasticsearch7的使用

Golang中的切片与gc

Golang defer 特性姿势还是有必要了解下的!!!

数据结构6:栈、队列、堆

手撸Golang 创建型设计模式 抽象工厂

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...