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]
編譯辦法:
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提供的服務了。