• Gitea—私有git服务器搭建教程


    Gitea——私有git服务器搭建详细教程

    原文章大佬

    原文链接:https://blog.csdn.net/qq_45173404/article/details/122277620
    
    • 1

    什么是Gitea

    Gitea 是一个开源社区驱动的轻量级代码托管解决方案,后端采用 Go 编写,采用 MIT 许可证.
    官网:https://gitea.io/zh-cn/
    
    • 1
    • 2

    image-20220620172959909

    一、源代码安装方式

    实验环境为MacOS系统,Windows系统以下操作大同小异
    
    官方文档:https://docs.gitea.io/zh-cn/install-from-source/
    
    • 1
    • 2
    • 3

    2. 下载gitea

    Github:https://github.com/go-gitea/gitea
    Gitee镜像:https://gitee.com/mirrors/gitea
    
    • 1
    • 2

    通过git将项目下载到$GOPATH/src目录下

    git clone https://github.com/go-gitea/gitea
    
    • 1

    3. 构建运行

    下载完成后用Goland打开,在项目根目录下使用以下命令安装各种前端依赖,下载好的依赖在项目根目录下生的node_modules目录中

    npm install
    
    
    • 1
    • 2

    然后通过以下目录构建后端代码

    TAGS="bindata" make backend
    
    • 1

    image-20220620173500935

    构建完成后,会在项目根目录下生成gitea可执行文件,我们使用以下命令来启动项目

    ./gitea web
    
    • 1

    image-20220620173530299

    启动成功后我们访问本机的3000端口,可以看到如下界面:

    image-20220620173637283

    这里我们配置我们所安装的mysql数据库和密码即可,这里的数据库名称需要我们提前创建一个数据库,这里创建的名称为gitea,此外还可以更改站点名称为自己想要的名称。

    设置更新完后,点击安装即可,然后就进入到gitea的控制台,到此即安装配置成功。

    image-20220620173708408

    二、Docker安装方式

    实验环境为CentOS7.6服务器,可以使用任意云厂商或者centos虚拟机
    
    官方文档:https://docs.gitea.io/zh-cn/install-with-docker/
    
    • 1
    • 2
    • 3
    1. Docker安装
    https://docs.docker.com/engine/install/centos/     官方文档:
    
    • 1
    # 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    以上操作完成后,我们可以使用 systemctl status docker来查看 Docker 服务是否启动

    image-20220621092216286

    1. Dokcer Compose安装
      官方文档:Install Docker Compose | Docker Documentation

    1.安装docker compose

    官方连接;https://docs.docker.com/compose/install/
    
    • 1
    # 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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意:

    Docker Compose 存放在GitHub不太稳定,可以通过镜像网址高速安装。
    https://docs.docker.com/compose/install/
    
    • 1
    • 2

    镜像网站:

    http://get.daocloud.io/
    
    • 1
    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    下载完成后可以输入docker-compose --version来查看是否安装成功

    image-20220621092702465

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    编写完成后,我们通过以下命令再启动 Gitea

    # 后台启动gitea
    docker-compose up -d server
    
    • 1
    • 2

    image-20220621093053340

    image-20220621093122468

    待启动成功,可以看到它启动在3000端口,然后我们通过服务器公网IP:3000即可访问到其web界面,注意服务器安全组规则要放行3000端口

    image-20220621094039587

    image-20220621094120109

    其中数据库设置我们不需要更改,因为是根据上述docker-compose.yml文件中的数据库配置来读取的,我们需要更改ssh服务的域名为服务器的公网ip,通知基础url的前缀也更改为服务器的公网ip

    image-20220621094635781

    然后创建一个管理员用户(zsr/123456)即可,然后点击安装

    image-20220621094918869

    设置完成后,点击立即安装,然后即可进入如下界面

    image-20220621095045004

    到此gitea的已经安装部署完成

    4. 基本操作实例

    我们来新建一个仓库:

    image-20220621100015646

    image-20220621100332611

    然后我们将仓库克隆下来新增一个文件然后再推送回去:下面内容是摘取别人的

    # 克隆仓库
    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    上述命令操作完成后,我们回到gitea web页面,即可看到变更

    image-20220621100723650

    使用source-Tree

    首先拉取代码下来

    image-20220621100642313

    使用Code.exe

    使用这个打开git代码原路径,创建修改代码

    image-20220621101106946

    回到source-Tree

    代码发现更新,进行提交

    image-20220621101311404

    先进行暂存所有
    image-20220621101507111

    进行推送
    image-20220621101547801

    输入账号密码

    账号:aike
    密码:aike1101
    
    • 1
    • 2

    image-20220621101703351

    回到gitea发现更新

    image-20220621101743571

    5. ssh配置

    上述我们推送到远程仓库要输入用户名和密码进行校验,这是十分麻烦的,我们可以配置ssh实现免密登陆:

    1️⃣ 首先在本机生成公钥
    # 进入到.ssh目录
    cd ~/.ssh
    
    # 生成密钥对
    ssh-keygen -t rsa -C "邮箱"
    
    # 查看公钥内容
    cat id_rsa.pub
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    image-20220621102727549

    2️⃣ gitea中添加公钥

    在gitea web界面的ssh配置页面新增一个ssh密钥,复制上面生成的公钥粘贴进去即可
    image-20220621102743770

    添加完成后如下所示

    image-20220621102757432

    此时如果我们修改hello.txt的内容再重新推送到gitea仓库,就不需要输入密码了

    image-20220621102835483

    采用ssh的方式克隆

    image-20220621102850465

    我们复制这个ssh地址来看看:

    image-20220621102921273

    发现还让我们输密码,我们不是刚刚配置的ssh吗?我们仔细看这个ssh地址:

    git@139.198.40.248:zsr/hello.git
    
    
    • 1
    • 2

    在服务器公网ip后面直接接了zsr用户名,没有接任何端口,也就是想当于走了默认端口22,等价于服务器公网ip:22也就是要登陆服务器的操作,这当然是需要密码的,我们应该是登陆服务器内部gitea容器的操作,因此我们需要修改gitea的一些配置:

    image-20220621102952062

    在docker-compose.yml文件中,由于我们将gitea的data目录挂在到本季的gitea目录中,因此我们需要进入该目录中来修改相关配置,需要修改/gitea/gitea/conf/app.ini文件

    image-20220621103004605

    由于我们将主机的222端口映射到gitea容器中的22端口,因此我们将app.ini中的ssh_portssh_listen_port修改为222端口

    image-20220621103018524

    修改完成后我们通过docker-compose restart命令重启一下gitea容器

    image-20220621103032786

    然后再次访问web界面,可以看到ssh地址已经变更,在服务器的公网ip后接了222端口(注意服务器安全组要放行222端口),也就相当于访问服务器内部gitea容器的22端口

    image-20220621103047028

    此时我们再通过ssh进行克隆,然后修改文件再推送回去

    image-20220621103100632

    这次期间任何流程无需再需要输入密码进行验证,到此ssh配置已经完毕。

    创建其他用户

    image-20220621111236087

    账号管理

    image-20220621111348004

    这里给大家提个小意见,新创建的用户如果权限很高,在项目里面是有设置这个选项的

    g-Ezr3OSyh-1655878255156)]

    然后再次访问web界面,可以看到ssh地址已经变更,在服务器的公网ip后接了222端口(注意服务器安全组要放行222端口),也就相当于访问服务器内部gitea容器的22端口

    [外链图片转存中…(img-N4iiaZC9-1655878255156)]

    此时我们再通过ssh进行克隆,然后修改文件再推送回去

    [外链图片转存中…(img-LFjKFITc-1655878255157)]

    这次期间任何流程无需再需要输入密码进行验证,到此ssh配置已经完毕。

    创建其他用户

    [外链图片转存中…(img-vp4fuldw-1655878255157)]

    账号管理

    [外链图片转存中…(img-Bm5zMhNQ-1655878255157)]

    这里给大家提个小意见,新创建的用户如果权限很高,在项目里面是有设置这个选项的

    image-20220621112804141

  • 相关阅读:
    UIButton的类型
    多层感知机
    3.Python-用Python实现MySQL数据库的增删改查
    numpy的基本操作
    Visual Studio ERROR : LNK2001 和LNK2019
    合肥大厂校招
    Visual Studio 2010 配置和使用Qt5.6.3
    杰理之CMD_SET_BLE_VISIBILITY【篇】
    麒麟 Kylin V10 一键安装 Oracle 11GR2 单机 ASM(231017)
    数据库分析工具explain
  • 原文地址:https://blog.csdn.net/tianmingqing0806/article/details/125408234