不能只让 go 在本地跑,得让 go 编译后,在服务器上跑。
好,那就这么干
- 编译。
- 放到服务端,使用守护进程的方式启动它。比如可以用 nohub、supervisord、systemctl 等。
- 使用 nginx 做重定向或代理,配置域名访问。
编译
先看看我的服务器信息。
lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: AlibabaCloud
Description: Alibaba Cloud Linux release 3 (Soaring Falcon)
Release: 3
Codename: SoaringFalcon
在 hack/config.yaml 中,增加编译的配置信息
build:
name: "main"
arch: "amd64"
system: "linux"
mod: "none"
packSrc: "resource,manifest"
version: "v1.0.0"
output: "./out"
cgo: 0
extra: ""
注意,manifest/config 下的 mysql,redis 等配置,一定要和服务端一致。好了,可以打包了。
gf build
上传启动
官方介绍的部署方式 https://goframe.org/pages/viewpage.action?pageId=1114145
将文件上传到 /alidata/go/study/simpleAdmin/go-simple-admin,设置用户组和权限
chown root:root main
chmod 777 main
我习惯使用 supervisord, 那我还是使用 supervisord。
增加配置。
# 创建编辑
vim /etc/supervisord.d/go_simple_admin.ini
[program:go-simple-admin]
user = root
directory = /alidata/go/study/simpleAdmin/go-simple-admin
command = /alidata/go/study/simpleAdmin/go-simple-admin/main
stdout_logfile = /alidata/go/study/simpleAdmin/go-simple-admin/log/gf-app-stdout.log
stderr_logfile = /alidata/go/study/simpleAdmin/go-simple-admin/log/gf-app-stderr.log
autostart = true
autorestart = true
# 重启
systemctl restart supervisord
# 查看端口
netstat -tunlp | grep
# 输出
tcp6 0 0 :::9885 :::* LISTEN 974337/main
看看日志。
cat gf-app-stdout.log
2023-09-18 21:44:53.424 966561: server gracefully shutting down by signal: terminated
2023-09-18 21:44:53.424 [INFO] pid[966561]: all servers shutdown
2023-09-18 21:44:55.606 [INFO] pid[974337]: http server started listening on [:9885]
2023-09-18 21:44:55.606 [INFO] {74c1b37f2b02861702f6620eb87bb573} swagger ui is serving at address: http://127.0.0.1:9885/swagger/
2023-09-18 21:44:55.606 [INFO] {74c1b37f2b02861702f6620eb87bb573} openapi specification is serving at address: http://127.0.0.1:9885/api.json
ADDRESS | METHOD | ROUTE | HANDLER | MIDDLEWARE
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | ALL | /* | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | ALL | /* | goSimpleAdmin/internal/service.IMiddleware.Resp | GLOBAL MIDDLEWARE
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | ALL | /api.json | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec |
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | GET | /api/admin/captcha | goSimpleAdmin/api/user.IUserAdmin.Captcha | service.IMiddleware.CORS
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | GET | /api/admin/user/info | goSimpleAdmin/api/user.IUserAdmin.UserInfo | service.IMiddleware.CORS
| | | | service.IMiddleware.Auth
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | POST | /api/admin/user/sign-in | goSimpleAdmin/api/user.IUserAdmin.SignIn | service.IMiddleware.CORS
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | POST | /api/admin/user/sign-up | goSimpleAdmin/api/user.IUserAdmin.SignUp | service.IMiddleware.CORS
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
:9885 | ALL | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI | HOOK_BEFORE_SERVE
----------|--------|-------------------------|-----------------------------------------------------------------|---------------------------
此时,已经跑起来了。可是外部访问不了了。是因为阿里云安全组没有配置该端口。即使配置了,能访问,那也不好看吗。总不能用 ip 带 端口的方式来使用。
http://192.168.1.55:9885/admin/api/sign-in
所以,此时,nginx 就可以上场了。准备好 ssl 证书,开始配置 nginx。
准备用 go.zeipan.com 作为域名。现在进行配置。这里通过代理实现映射。
端口是 9885。
vim go.zeipan.com.conf
server
{
listen 80;
server_name go.zeipan.com;
return 301 https://go.zeipan.com$request_uri;
}
server
{
listen 443 ssl http2;
server_name go.zeipan.com;
ssl_certificate /alidata/service/nginx/conf/ssl/go.zeipan.com.pem;
ssl_certificate_key /alidata/service/nginx/conf/ssl/go.zeipan.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options 'SAMEORIGIN';
more_clear_headers "Server";
# more_clear_headers "Strict-Transport-Security";
# more_clear_headers "Content-Security-Policy";
# more_clear_headers "X-Content-Type-Options";
# more_clear_headers "X-XSS-Protection";
more_clear_headers "X-Frame-Options";
charset utf-8;
location ~ .*\.(gif|jpg|jpeg|png|js|css|eot|ttf|woff|svg|otf)$ {
access_log off;
expires 1d;
root /alidata/go/study/simpleAdmin/go-simple-admin/storage;
try_files $uri @gosimple;
}
location / {
try_files $uri @gosimple;
}
location @gosimple {
proxy_pass http://127.0.0.1:9885;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /\.ht {
deny all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /alidata/service/nginx/logs/go.zeipan.com.log;
}
# 重启
nginx -s reload
好了,现在通过域名访问看看。
访问上边的站点,会返回一个 404 。数据是 {"message":"接口跑到月球啦"}
我们有一个请求验证码的 get 接口。来试试看 https://go.zeipan.com/api/admin/captcha
已经正确返回信息了。
{"id":"YY1HFXxUJVMA2aQAVuAk","base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAAAgBAMAAABDZCNDAAAAG1BMVEUAAAB6CwHneG7fcGaDFAqPIBazRDp6CwHvgHY/3unuAAAAAXRSTlMAQObYZgAAAPVJREFUeJzMU7GRwzAME+13T9wvkMsEucsCKTKAG++/Sk4gLVMyXaWJmoQSCICQXH58icbq7wIEhOr9Gk4bCDowYQShFEARRfP/le3UfRS2ARSBSkJEAGU4IBRHkbj3Q3CEEbPXtCNKzS6QACKRwkS7EcJiCFADec9gqIglxV9JWLSpFVEBVC9A3lzFXDIJwPcFdT7JbNf2/92+miarpafC89HConcyb2ukEsweOxyJkcl2OP9xiYOldR/Qc85AyzaZVLUL9chOTPebPwGakhRUJtK4IXvs8Waa/Tk8mYqOabYgPCbPrft0OqYOldzet+sTAAD//0ntDv+CloirAAAAAElFTkSuQmCC"}
可以把 data 里的数据复制到浏览器打开,就是一个图片哈。