1. 程式人生 > 實用技巧 >beego 踩坑筆記

beego 踩坑筆記

centos7 安裝 Go

  • 安裝 wget

    yum install  wget -y
    
  • 下載 go的安裝包

    wget https://dl.google.com/go/go1.13.8.linux-amd64.tar.gz
    
  • 解壓

    tar -C /usr/local/ -zxvf go1.13.8.linux-amd64.tar.gz 
    
  • 新增系統變數

    • 建立檔案

      vim /etc/profile.d/go
      
    • 新增內容

      export PATH=$PATH:/usr/local/go/bin
      
    • source

      source /etc/profile.d/go
      
    • 檢查版本號

      go version
      
  • 設定工作目錄

    • 建立目錄

      mkdir -p /home/ttyy
      
    • 建立檔案

      vim /etc/profile.d/gopath
      
    • 新增到環境變數中

      export GOPATH=/home/ttyy
      
    • source

      source /etc/profile.d/gopath
      
    • 驗證

      package main
      
      import (
          "fmt"
      )
      
      func main() {
          fmt.Println( "Hello world!" )
      }
      
    • 執行

      go run helloworld.go
      
  • beego 相關的

    • 使用阿里雲 Go Module 國內映象倉庫服務

      go env -w GO111MODULE=on
      go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
      
    • 在工作目錄下安裝bee 工具

      go get github.com/beego/bee
      
    • 設定 bee 的環境變數

      vim /etc/profile.d/bee
      
      export PATH=$PATH:/home/ttyy/bin
      
    • source

      source /etc/profile.d/bee
      
  • 建立專案

    • 使用 bee 建立專案

      bee new myproject
      
    • 專案結構

      .
      |-- conf
      |   `-- app.conf
      |-- controllers
      |   `-- default.go
      |-- go.mod
      |-- main.go
      |-- models
      |-- routers
      |   `-- router.go
      |-- static
      |   |-- css
      |   |-- img
      |   `-- js
      |       `-- reload.min.js
      |-- tests
      |   `-- default_test.go
      `-- views
          `-- index.tpl
      
    • 啟動服務

      bee run
      
    • 配置 nginx

      server {
              listen 8000;
              server_name yy.xx.com;
              location / {
                  proxy_set_header Host $host:$server_port;
                  proxy_set_header X-Real-Ip $remote_addr;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_pass http://0.0.0.0:8080;
              }
      }
      
    • 重啟 nginx 服務

      systemctl restart nginx.service
      
    • 輸入 http://yy.xx.com:8080