本文將從源代碼和docker安裝兩種方式帶大家從0~1通過
Gitea搭建一個私有git服務器
Gitea——私有git服務器搭建教程
- 什么是Gitea
- 一、源代碼安裝方式
- 1. 前置環境要求
- 2. 下載gitea
- 3. 構建運行
- 二、Docker安裝方式
- 1. Docker安裝
- 2. Dokcer Compose安裝
- 3. 安裝啟動gitea
- 4. 基本操作實體
- 5. ssh配置
什么是Gitea
Gitea是一個開源社區驅動的輕量級代碼托管解決方案,后端采用 Go 撰寫,采用 MIT 許可證.
官網:https://gitea.io/zh-cn/
一、源代碼安裝方式
實驗環境為MacOS系統,Windows系統以下操作大同小異
官方檔案:https://docs.gitea.io/zh-cn/install-from-source/
1. 前置環境要求
- Go環境安裝(版本要求大于1.16,一定要設定
$GOPATH環境變數,并將其bin目錄添加到$PATH中)- Go語言環境搭建(Windows+Linux)_歡迎來到 Baret~H 的博客-CSDN博客
- Node.JS環境安裝(版本要求大于等于12.17,用于構建js和css檔案,建議安裝最新版本)
- Node.JS安裝教程_歡迎來到 Baret~H 的博客-CSDN博客
- 資料庫環境安裝,建議MySQL
- MySQL最新版8.0.21安裝配置教程_歡迎來到 Baret~H 的博客-CSDN博客
# 我的環境
zhongsiru@zhongsirudeMacBook-Air ~ % go version
go version go1.17.1 darwin/arm64
zhongsiru@zhongsirudeMacBook-Air ~ % node -v
v14.17.6
zhongsiru@zhongsirudeMacBook-Air ~ % npm -v
8.1.3
zhongsiru@zhongsirudeMacBook-Air ~ % mysql -V
mysql Ver 8.0.27 for macos11 on x86_64 (MySQL Community Server - GPL)
2. 下載gitea
- Github:https://github.com/go-gitea/gitea
- Gitee鏡像:https://gitee.com/mirrors/gitea
通過git將專案下載到$GOPATH/src目錄下
git clone https://github.com/go-gitea/gitea
3. 構建運行
下載完成后用Goland打開,在專案根目錄下使用以下命令安裝各種前端依賴,下載好的依賴在專案根目錄下生的node_modules目錄中
npm install
然后通過以下目錄構建后端代碼
TAGS="bindata" make backend

構建完成后,會在專案根目錄下生成gitea可執行檔案,我們使用以下命令來啟動專案
./gitea web

啟動成功后我們訪問本機的3000埠,可以看到如下界面:

這里我們配置我們所安裝的mysql資料庫和密碼即可,這里的資料庫名稱需要我們提前創建一個資料庫,這里創建的名稱為gitea,此外還可以更改站點名稱為自己想要的名稱,
設定更新完后,點擊安裝即可,然后就進入到gitea的控制臺,到此即安裝配置成功,

二、Docker安裝方式
實驗環境為CentOS7.6服務器,可以使用任意云廠商或者centos虛擬機
官方檔案:https://docs.gitea.io/zh-cn/install-with-docker/
1. Docker安裝
官方檔案:Install Docker Engine on CentOS | Docker Documentation
# 1.移除以前docker相關包
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2. 配置yum源
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 3. 安裝docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
# 4. 啟動docker
systemctl enable docker --now
# 5. 配置阿里云加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
以上操作完成后,我們可以使用 systemctl status docker來查看 Docker 服務是否啟動

2. Dokcer Compose安裝
官方檔案:Install Docker Compose | Docker Documentation
# 1.安裝docker compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 2.賦予下載的docker-compose執行權限
sudo chmod +x /usr/local/bin/docker-compose
注意:Docker Compose 存放在GitHub不太穩定,可以通過鏡像網址高速安裝,
- 鏡像網站:http://get.daocloud.io/
curl -L https://get.daocloud.io/docker/compose/releases/download/v2.2.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
下載完成后可以輸入docker-compose --version來查看是否安裝成功

3. 安裝啟動gitea
我們通過docker compose的yaml組態檔來安裝gitea,其中選用資料庫mysql來存盤gitea的資料檔案,
創建docker-compose.yml檔案,內容如下:
version: "3"
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:1.15.9
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
- DB_TYPE=mysql
- DB_HOST=db:3306
- DB_NAME=gitea
- DB_USER=gitea
- DB_PASSWD=gitea
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"
depends_on:
- db
db:
image: mysql:8
restart: always
environment:
- MYSQL_ROOT_PASSWORD=gitea
- MYSQL_USER=gitea
- MYSQL_PASSWORD=gitea
- MYSQL_DATABASE=gitea
networks:
- gitea
volumes:
- ./mysql:/var/lib/mysql
撰寫完成后,我們通過以下命令再啟動 Gitea
# 后臺啟動gitea
docker-compose up -d server


待啟動成功,可以看到它啟動在3000埠,然后我們通過服務器公網IP:3000即可訪問到其web界面,注意服務器安全組規則要放行3000埠

其中資料庫設定我們不需要更改,因為是根據上述docker-compose.yml檔案中的資料庫配置來讀取的,我們需要更改ssh服務的域名為服務器的公網ip,通知基礎url的前綴也更改為服務器的公網ip

然后創建一個管理員用戶(zsr/123456)即可,然后點擊安裝

設定完成后,點擊立即安裝,然后即可進入如下界面

到此gitea的已經安裝部署完成
4. 基本操作實體
我們來新建一個倉庫:

然后我們將倉庫克隆下來新增一個檔案然后再推送回去:
# 克隆倉庫
zhongsiru@zhongsirudeMacBook-Air Desktop % git clone http://139.198.40.248:3000/zsr/hello.git
Cloning into 'hello'...
warning: You appear to have cloned an empty repository.
# 進入本地倉庫目錄
zhongsiru@zhongsirudeMacBook-Air Desktop % cd hello
# 新增hello.txt檔案
zhongsiru@zhongsirudeMacBook-Air hello % vim hello.txt
zhongsiru@zhongsirudeMacBook-Air hello % ls
hello.txt
# 將變更添加到暫存區
zhongsiru@zhongsirudeMacBook-Air hello % git add .
# 將暫存區的內容添加到本地倉庫
zhongsiru@zhongsirudeMacBook-Air hello % git commit -m "添加hello.txt"
[master (root-commit) 1f46f0e] 添加hello.txt
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
# 推送到遠程倉庫(要輸入用戶名和密碼)
zhongsiru@zhongsirudeMacBook-Air hello % git push origin master
Username for 'http://139.198.40.248:3000': zsr
Password for 'http://zsr@139.198.40.248:3000':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 230 bytes | 230.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To http://139.198.40.248:3000/zsr/hello.git
* [new branch] master -> master
上述命令操作完成后,我們回到gitea web頁面,即可看到變更

5. ssh配置
上述我們推送到遠程倉庫要輸入用戶名和密碼進行校驗,這是十分麻煩的,我們可以配置ssh實作免密登陸:
1?? 首先在本機生成公鑰:
# 進入到.ssh目錄
cd ~/.ssh
# 生成密鑰對
ssh-keygen -t rsa -C "郵箱"
# 查看公鑰內容
cat id_rsa.pub

2?? gitea中添加公鑰
在gitea web界面的ssh配置頁面新增一個ssh密鑰,復制上面生成的公鑰粘貼進去即可

添加完成后如下所示

此時如果我們修改hello.txt的內容再重新推送到gitea倉庫,就不需要輸入密碼了

如果我們采用ssh的方式克隆下來呢?

我們復制這個ssh地址來看看:

發現還讓我們輸密碼,我們不是剛剛配置的ssh嗎?我們仔細看這個ssh地址:
git@139.198.40.248:zsr/hello.git
在服務器公網ip后面直接接了zsr用戶名,沒有接任何埠,也就是想當于走了默認埠22,等價于服務器公網ip:22也就是要登陸服務器的操作,這當然是需要密碼的,我們應該是登陸服務器內部gitea容器的操作,因此我們需要修改gitea的一些配置:

在docker-compose.yml檔案中,由于我們將gitea的data目錄掛在到本季的gitea目錄中,因此我們需要進入該目錄中來修改相關配置,需要修改/gitea/gitea/conf/app.ini檔案

由于我們將主機的222埠映射到gitea容器中的22埠,因此我們將app.ini中的ssh_port和ssh_listen_port修改為222埠

修改完成后我們通過docker-compose restart命令重啟一下gitea容器

然后再次訪問web界面,可以看到ssh地址已經變更,在服務器的公網ip后接了222埠(注意服務器安全組要放行222埠),也就相當于訪問服務器內部gitea容器的22埠

此時我們再通過ssh進行克隆,然后修改檔案再推送回去

這次期間任何流程無需再需要輸入密碼進行驗證,到此ssh配置已經完畢,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/401524.html
標籤:其他
上一篇:C++ vector所有操作,你真的掌握vector了嗎?兩萬總結帶你干穿vector
下一篇:Linux系統配置(檔案管理)

