在Linux服务器搭建GoWeb环境,包括配置Go环境,配置Go安装,mysql的安装。
一、服务器配置Go环境
1. 下载安装包并上传
1.1 官方网站,解压
tar -zxvf xxx
1.2 创建环境文件夹
根目录下创建go文件夹,与go相关的路径都放在该目录下,在go文件夹下创建:
(1) GOROOT
文件夹:将go安装包解压后移动到此处
[root@ls go1.82.2]# pwd
/root/go/GOROOT/go1.82.2
[root@ls go1.82.2]# ll
total 236
drwxr-xr-x 2 root root 4096 May 11 00:48 api
-rw-r--r-- 1 root root 56057 May 11 00:48 AUTHORS
drwxr-xr-x 2 root root 4096 May 11 00:50 bin
-rw-r--r-- 1 root root 52 May 11 00:48 codereview.cfg
-rw-r--r-- 1 root root 1339 May 11 00:48 CONTRIBUTING.md
-rw-r--r-- 1 root root 111408 May 11 00:48 CONTRIBUTORS
drwxr-xr-x 2 root root 4096 May 11 00:48 doc
drwxr-xr-x 3 root root 4096 May 11 00:48 lib
-rw-r--r-- 1 root root 1479 May 11 00:48 LICENSE
drwxr-xr-x 12 root root 4096 May 11 00:48 misc
-rw-r--r-- 1 root root 1303 May 11 00:48 PATENTS
drwxr-xr-x 6 root root 4096 May 11 00:51 pkg
-rw-r--r-- 1 root root 1475 May 11 00:48 README.md
-rw-r--r-- 1 root root 397 May 11 00:48 SECURITY.md
drwxr-xr-x 48 root root 4096 May 11 00:48 src
drwxr-xr-x 27 root root 12288 May 11 00:48 test
-rw-r--r-- 1 root root 8 May 11 00:48 VERSION
(2) GOPATH
: 通过go get
或go install
的第三方包的安装位置,创建pkg
文件夹
[root@ls go1.82.2]# pwd
/root/go/GOPATH/go1.82.2
[root@ls go1.82.2]# ll
total 12
drwxr-xr-x 2 root root 4096 Jun 1 12:36 bin
drwxr-xr-x 4 root root 4096 Jun 1 12:40 pkg
drwxr-xr-x 2 root root 4096 Jun 1 12:32 src
1.3 配置系统环境
(1) 修改/etc/profile
文件,添加如下片段:
export GOROOT=/root/go/GOROOT/go1.82.2
export GOPATH=/root/go/GOPATH/go1.82.2
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin
(2) source /etc/profile
2.安装开发所需包
2.1 开启go
的下载设置
对于使用go install
下载第三方包连接超时的问题,需要设置网络代理。
go env -w GOPROXY=https://goproxy.cn
2.2 github.com/gin-gonic/gin
主要通过go install
语句进行安装,直接使用go get
语句会出现如下的错误:
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd@latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
使用go install
语句需要指定模块的版本:
go get -u github.com/gin-gonic/gin@v1.8.0
2.3 gorm.io/gorm
go install gorm.io/gorm@latest
go install gorm.io/driver/mysql@latest
二、MySQL
安装
参考文章:Linux下安装MySQL