在Debian 10(Buster)上安装Gitea Git服务

时间:2020-02-23 14:32:58  来源:igfitidea点击:

Gitea是最快,最无痛的自动托管开源Git服务。
Gitea是通过Go的编写的,并作为一个二进制包分发,这些包运行在支持 - Linux,MacOS和Windows的所有平台和体系结构中运行。
在本教程中,我们将在Debian 10(Buster)Linux上安装和配置Gitea Git Service。

第1步:更新系统并安装Git

我们需要在Debian Machine中安装Git。
让我们更新我们的操作系统并确保安装Git。

sudo apt -y update
sudo apt -y install git vim bash-completion

查看安装的git版本。

$git --version
git version 2.20.1

第2步:添加Git用户帐户的Gitea

Gitea应该有一个专门的本地用户帐户进行管理操作。
通过运行以下命令将用户和组添加到Debian系统。

sudo adduser \
   --system \
   --shell /bin/bash \
   --gecos 'Git Version Control' \
   --group \
   --disabled-password \
   --home /home/git \
   git

用户创建将为用户分配唯一ID并创建其主目录。

Adding system user `git' (UID 108) ...
Adding new group `git' (GID 114) ...
Adding new user `git' (UID 108) with group `git' ...
Creating home directory `/home/git' ...

第3步:安装MariaDB数据库服务器

数据将存储在MariaDB数据库服务器上。

sudo apt -y install mariadb-server

通过设置root密码和删除测试数据库和用户来保护数据库安装。

$sudo mysql_secure_installation 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

为Gitea创建数据库。

$sudo mysql -u root -p
CREATE DATABASE gitea;
GRANT ALL PRIVILEGES ON gitea.* TO 'gitea'@'localhost' IDENTIFIED BY "Hyman@theitroad";
FLUSH PRIVILEGES;
QUIT;

第4步:在Debian 10上安装Gitea(Buster)

Gitea二进制包可在下载页面上找到。
在下载之前检查最新版本。

export VER=1.9.4
wget https://github.com/go-gitea/gitea/releases/download/v${VER}/gitea-${VER}-linux-amd64

将下载的二进制文件移动到 /use/local/bin

chmod +x gitea-${VER}-linux-amd64
sudo mv gitea-${VER}-linux-amd64 /usr/local/bin/gitea

通过检查安装的Gitea版本确认成功安装。

$gitea --version
Gitea version 1.9.4 built with GNU Make 4.1, go1.12.10 : bindata, sqlite, sqlite_unlock_notify

第5步:配置SystemD

创建Gitea设置所需的目录。

sudo mkdir -p /etc/gitea /var/lib/gitea/{custom,data,indexers,public,log}
sudo chown git:git /var/lib/gitea/{data,indexers,log}
sudo chmod 750 /var/lib/gitea/{data,indexers,log}
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea

Web安装程序将需要编写权限配置文件 /etc/gitea

为Gitea创建一个Systemd服务文件。

sudo vim /etc/systemd/system/gitea.service

配置文件并设置用户,组和WorkDir。

[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
After=mysql.service
[Service]
LimitMEMLOCK=infinity
LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web -c /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
[Install]
WantedBy=multi-user.target

重新加载系统并重新启动Gitea服务。

sudo systemctl daemon-reload
sudo systemctl enable --now gitea

检查服务状态。

$systemctl status gitea
● gitea.service - Gitea (Git with a cup of tea)
   Loaded: loaded (/etc/systemd/system/gitea.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-10-20 07:37:06 UTC; 27s ago
 Main PID: 2637 (gitea)
    Tasks: 9 (limit: 4719)
   Memory: 93.8M
   CGroup: /system.slice/gitea.service
           └─2637 /usr/local/bin/gitea web -c /etc/gitea/app.ini
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 routers/init.go:74:GlobalInit() [T] Custom path: /var/lib/gitea/custom
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 routers/init.go:75:GlobalInit() [T] Log path: /var/lib/gitea/log
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 ...dules/setting/log.go:226:newLogService() [I] Gitea v1.9.4 built with GNU Make 4.1, go1.12
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 ...dules/setting/log.go:269:newLogService() [I] Gitea Log Mode: Console(Console:info)
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 ...les/setting/cache.go:42:newCacheService() [I] Cache Service Enabled
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 ...s/setting/session.go:45:newSessionService() [I] Session Service Enabled
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 routers/init.go:106:GlobalInit() [I] SQLite3 Supported
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 routers/init.go:37:checkRunMode() [I] Run Mode: Development
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 cmd/web.go:151:runWeb() [I] Listen: http://0.0.0.0:3000
Oct 20 07:37:06 deb10 gitea[2637]: 2019/10/20 07:37:06 ...ce/gracehttp/http.go:142:Serve() [I] Serving [::]:3000 with pid 2637

第6步:配置nginx代理

在Debian 10上安装nginx。

sudo apt -y install nginx

如果启用了UFW,请允许HTTP和HTTPS端口。

for i in http https; do
 sudo ufw allow $i
done

为Gitea创建nginx配置文件

sudo vim /etc/nginx/conf.d/gitea.conf

将数据粘贴到创建的文件中。

server {
    listen 80;
    server_name git.example.com;
    location/{
        proxy_pass http://localhost:3000;
    }
}

设置正确的域名并重新启动nginx服务。

sudo systemctl restart nginx

第7步:从Web界面完成Gitea安装

配置nginx代理后,访问http://servername /安装上的Gitea Web界面

设置数据库身份验证

在第一页上,设置数据库连接

提供的用户名和密码应匹配数据库配置部分中提供的密码。
如果数据库服务器位于其他主机上,请在主机部分下提供IP地址。

设置应用程序常规设置

设置SSH服务器域 - 应该是nginx配置中使用的相同域。

提供应用程序URL和HTTP侦听端口。
由于我们使用nginx代理,因此无需更改默认值。

禁用用户自我注册

我们可以在"服务器和其他服务设置"下禁用用户自我登记。
这意味着管理员用户将手动创建用户帐户。

我们可以选择创建管理员用户帐户。
默认情况下,root用户将自动获得管理员访问权限。

完成配置后,单击"安装Gitea"按钮以完成安装。
在成功安装时,我们应该登录Gitea管理控制台。