Gogs的交叉编译与配置

Gogs的交叉编译与配置

gogs官方的版本,已经有一年未发布Release版本了。

因此需要手工交叉编译一个Gogs来,Gogs与Gitea的编译很类似。

1.环境

Host环境:Ubuntu 18.04.5 (PC)

编译工具链:arm-himix200-linux(解包自arm-himix200-linux.tgz,据说来自Hi3516dv300SDK),海思提供的arm编译工具链

环境变量:执行命令:export PATH=/opt/hisi-linux/x86-arm/arm-himix200-linux/bin:$PATH

编译器arm-himix200-linux默认输出的ELF格式为private flags = 5000200: [Version5 EABI] [soft-float ABI],与ubuntu-armhf的格式private flags = 5000400: [Version5 EABI] [hard-float ABI]不兼容(soft-float ABI与hard-float ABI的传参规则不一样,因此会不兼容)。

编译办法:

CGO_ENABLED=1 CC=arm-himix200-linux-gcc PATH=$PATH:~/XXX/go/bin GOOS=linux GOARCH=arm GOARM=7 TAGS="cert" make build

此时编译后,由于默认CC编译器的选项是生成5000200软浮点ABI,所以gitea出来的格式也是5000200。

需要首先将Makefile中调用go generate生成bindata的部分代码修改调整一下

sed -i 's|go generate|CC= GOOS= GOARCH= CGO_ENABLED=0 CGO_CFLAGS= CGO_CXXFLAGS= CGO_CPPFLAGS= CGO_LDFLAGS= go generate|g' Makefile

然后再执行make build构建命令

CGO_ENABLED=1 CGO_CFLAGS="-mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4" CGO_CXXFLAGS="-mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4" CGO_CPPFLAGS="-mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4" CGO_LDFLAGS="-mcpu=cortex-a7 -mfloat-abi=hard -mfpu=neon-vfpv4" CC=arm-himix200-linux-gcc PATH=$PATH:~/XXX/go/bin GOOS=linux GOARCH=arm GOARM=7 TAGS="cert" make build

此时,生成的gogs程序

gogs编译后,会生成一个打包的应用,需要参考官方文档添加相应服务文件、配置文件等。

编译时,如果github等网站访问不畅,可以配置goproxy,如下:

 export GOPROXY=https://goproxy.io,direct

1.1服务文件

[Unit]
Description=Gogs -- A painless self-hosted Git service
After=syslog.target
After=network.target
#After=mariadb.service mysqld.service postgresql.service memcached.service redis.service

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=simple
User=git
Group=git
WorkingDirectory=/data/git
ExecStart=/data/app/bin/gogs web --config /data/app/etc/gogs.app.ini
Restart=always
Environment=USER=git HOME=/data/git

# Some distributions may not support these hardening directives. If you cannot start the service due
# to an unknown option, comment out the ones not supported by your version of systemd.
ProtectSystem=full
PrivateDevices=yes
PrivateTmp=yes
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

1.2配置文件

[server]

[repository]
; The root path for storing managed repositories, default is "~/gogs-repositories"
ROOT = /data/git/gogs-repositories

[database]
; The database backend, either "postgres", "mysql" "sqlite3" or "mssql".
; You can connect to TiDB with MySQL protocol.
TYPE = sqlite3

[auth]
; Whether to disable self-registration. When disabled, accounts would have to be created by admins.
DISABLE_REGISTRATION = true

[admin]
; Whether to disable regular (non-admin) users to create organizations.
DISABLE_REGULAR_ORG_CREATION = false

[git]

[git.timeout]

[mirror]

1.3添加用户

sudo useradd -U git -m /home/git

查看gogs的默认编译要求如下:

go 1.14 or higher, see here
make, see here

2.配置

gogs编译好后,单独调试通过,准备集成到网站中。网站中已有typecho、nextcloud服务,通过nginx的子目录方式。

因此增加nginx的location配置:

    location ^~ /gogs/ {

        # 日志文件分开
        error_log     /var/log/nginx/gogs.error.log;
        access_log    /var/log/nginx/gogs.access.log;
    
        # 注意: 反向代理后端 URL 的最后需要有一个路径符号
        proxy_pass http://127.0.0.1:3000/;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr; # 设置请求源地址
        proxy_set_header X-Forwarded-Proto $scheme; # 设置Http协议
        add_header X-Cache $upstream_cache_status;
        #Set Nginx Cache
        add_header Cache-Control no-cache;
        #expires 12h;
    }

重启nginx的服务。

修改gogs配置app.ini文件中的ROOT_URL(如果服务器的域名可用,则将公网IP替换成域名)。

[server]
EXTERNAL_URL = http://aaa.bbb.ccc.ddd/gogs/
HTTP_PORT = 3000

[git]
PATH =

重启gogs服务。

即可用http://aaa.bbb.ccc.ddd/gogs/地址访问gogs提供的服务了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章