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提供的服務了。

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