Gitea一个极易安装,运行非常快速,安装和使用体验良好的自建Git服务。采用Go作为后端语言,支持跨平台,Linux, macOS 和 Windows 以及各种架构,除了x86,amd64,还包括 ARM 和 PowerPC。

一、编译安装最新版Git(已安装请忽略)

1. 安装依赖软件

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc
yum install gcc perl-ExtUtils-MakeMaker

2. 卸载系统自带的低版本git(如果没有安装忽略)

  1. 查看Git版本

    git --version
  2. 卸载Git

    yum remove git  

    3. 编译安装最新的git版本

    cd /usr/local/src/
    wget https://www.kernel.org/pub/software/scm/git/git-2.33.0.tar.xz
    tar -vxf git-2.33.0.tar.xz
    cd git-2.33.0
    make prefix=/usr/local/git all
    make prefix=/usr/local/git install
    如果报错
    BEGIN failed--compilation aborted at Makefile.PL line 3.
    make[1]: * [perl.mak] Error 2
    make: * [perl/perl.mak] Error 2
    安装依赖:yum -y install perl-devel
    重新编译:make prefix=/usr/local/git all

4.编译完成配置环境变量

  • 对当前用户生效

      echo "export PATH=$PATH:/usr/local/git/bin" >> ~/.bashrc
      source ~/.bashrc
  • 对所有用户生效

      echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
      source /etc/profile

    5.查看安装Git版本

    git --version

6 添加git账户

# On Ubuntu/Debian:
adduser \
   --system \
   --shell /bin/bash \
   --gecos 'Git Version Control' \
   --group \
   --disabled-password \
   --home /home/git \
   git

# On Fedora/RHEL/CentOS:
groupadd --system git
adduser \
   --system \
   --shell /bin/bash \
   --comment 'Git Version Control' \
   --gid git \
   --home-dir /home/git \
   --create-home \
   git

7 配置文件夹权限

chown -R git:git /opt/gitea/

二、安装Gitea管理代码

由于Gitea需要一个数据库才能使用,下面以MySQL为例, Gitea支持PostgreSQL,MySQL,SQLite和MSSQL。

1.使用MySQL为Gitea创建用户及数据库

SET old_passwords=0;
本机数据库:
CREATE USER 'gitea' IDENTIFIED BY 'gitea';
远程数据库:
CREATE USER 'gitea'@'12.34.56.78' IDENTIFIED BY 'gitea';
创建数据库
CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
修改数据库权限
本机数据库:
GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea';
远程数据库:
GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'12.34.56.78';
刷新MySQL的系统权限相关表
FLUSH PRIVILEGES;

2.下载Gitea二进制运行文件

获取其他版本下载地址:dl.gitea.io

wget -O gitea https://dl.gitea.io/gitea/1.10.6/gitea-1.10.6-linux-amd64
添加执行权限
chmod +x gitea

3.创建gitea配置文件

vi /opt/gitea/custom/conf/app.ini

根据自己需求修改下面例子内容写入,完整配置

APP_NAME = 私人代码仓库
RUN_USER = root
RUN_MODE = prod

[security]
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NDUwMjU0NDZ9.tPEOBpqmQnaBQu9mjMe68J6GKi52PMqNq08x7UGEgeo
INSTALL_LOCK   = true
SECRET_KEY     = eolzTDSK4In6jcnyGhzxlLelhmvVlHlmhRyUlTRJYzcWQiFTKDA9lcUH1sceYzRW

[database]
DB_TYPE  = mysql
HOST     = 127.0.0.1:3306
NAME     = gitea
USER     = gitea
PASSWD   = x44KHndskxsXjyWA
SSL_MODE = disable
PATH     = data/gitea.db

[repository]
ROOT = /opt/gitea/gitea-repositories

[server]
SSH_DOMAIN       = 127.0.0.1
DOMAIN           = git.myzhangyi.com
HTTP_ADD         = 0.0.0.0
HTTP_PORT        = 3000
ROOT_URL         = http://git.myzhangyi.com/
DISABLE_SSH      = true
SSH_PORT         = 22
LANDING_PAGE     = explore
LFS_START_SERVER = true
LFS_CONTENT_PATH = /opt/gitea/data/lfs
LFS_JWT_SECRET   = TDOlh2Y3AtkuI0RV6rVWd625zSRdyeR5b8JOvWV-kQ4
OFFLINE_MODE     = false

[mailer]
ENABLED = false

[service]
REGISTER_EMAIL_CONFIRM            = false
ENABLE_NOTIFY_MAIL                = false
DISABLE_REGISTRATION              = true
SHOW_REGISTRATION_BUTTON          = false
ALLOW_ONLY_EXTERNAL_REGISTRATION  = false
ENABLE_CAPTCHA                    = false
REQUIRE_SIGNIN_VIEW               = false
DEFAULT_KEEP_EMAIL_PRIVATE        = false
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
DEFAULT_ENABLE_TIMETRACKING       = true
NO_REPLY_ADDRESS                  = noreply.example.org

[picture]
DISABLE_GRAVATAR        = false
ENABLE_FEDERATED_AVATAR = true

[openid]
ENABLE_OPENID_SIGNIN = true
ENABLE_OPENID_SIGNUP = true

[cache]
ADAPTER  = redis
HOST     = network=tcp,addr=127.0.0.1:6379,password=,db=0,pool_size=100,idle_timeout=180
ITEM_TTL = 16h

[session]
PROVIDER          = redis
PROVIDER_CONFIG   = network=tcp,addr=127.0.0.1:6379,password=,db=0,pool_size=100,idle_timeout=180
COOKIE_NAME       = i_like_gitea
COOKIE_SECURE     = false
ENABLE_SET_COOKIE = true
GC_INTERVAL_TIME  = 86400
SESSION_LIFE_TIME = 86400

[log]
MODE      = file
LEVEL     = Info
ROOT_PATH = /opt/gitea/log

[i18n]
LANGS = zh-CN
NAMES = 简体中文

[i18n.datelang]
zh-CN = zh

[oauth2]
JWT_SECRET = TLMvo8pmRVpudj-1_FjHpIdqCTl_YN9J6acWuoDfyJo

4.创建gitea.service文件

vim /etc/systemd/system/gitea.service

请根据自己需求修改下面例子内容写入,官方例子

[Unit]
Description=Gitea
After=syslog.target
After=network.target
Requires=mysql.service
Requires=redis.service

[Service]
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/opt/gitea/
ExecStart=/opt/gitea/gitea web --config /opt/gitea/custom/conf/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/opt/gitea

[Install]
WantedBy=multi-user.target

激活 gitea 并将它作为系统自启动服务:

systemctl daemon-reload
systemctl enable gitea
systemctl start gitea

如果提示找不到git命令,因为默认是在/bin下面找二进制命令,做软链接即可。

ln -s  /usr/local/git/bin/git-cvsserver /bin/git-cvsserver
ln -s  /usr/local/git/bin/gitk /bin/gitk
ln -s  /usr/local/git/bin/git-receive-pac /bin/git-receive-pac
ln -s  /usr/local/git/bin/git-shell /bin/git-shell
ln -s  /usr/local/git/bin/git-upload-archive /bin/git-upload-archive
ln -s  /usr/local/git/bin/git-upload-pack /bin/git-upload-pack
ln -s  /usr/local/git/bin/git /bin/git