安装git

sudo apt install git

创建git用户并加入git用户组

创建git用户,并设置密码。这条命令同时会创建git的主目录和git用户组

sudo adduser git

将git用户加入git用户组

sudo adduser git git

在mariaDB中创建gitea的数据库

gitea支持多种数据库。由于我的服务器已经有了mariadb,所以选择mariadb作为gitea的数据库

进入数据库

mysql -uroot -p

创建gitea用户。password替换成你的密码

create user 'gitea'@'localhost' identified by 'password';

创建gitea数据库

create database gitea;

赋予gitea用户使用gitea数据库的权限

grant all on gitea.* to gitea@localhost;

使设置生效

FLUSH PRIVILEGES;

退出

exit;

以二进制文件的方式安装gitea

下载二进制文件(我下载的时候最新版本是1.16.8)

sudo wget -O gitea https://dl.gitea.io/gitea/1.16.8/gitea-1.16.8-linux-amd64

使二进制文件可以执行

sudo chmod +x gitea

移动二进制文件至/usr/local/bin

sudo mv gitea /usr/local/bin

创建需要的目录,并配置所有者和权限

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

创建gitea服务

详细过程请看:在 Linux 中以 service 方式运行 - Docs (gitea.io)

创建gitea.service文件:

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

向文件中粘贴配置,并按要求修改。配置文件内容在这里:gitea/gitea.service at main · go-gitea/gitea · GitHub

这里我去掉了mariadb的注释:

[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
###
# Don't forget to add the database service dependencies
###
#
#Wants=mysql.service
#After=mysql.service
#
Wants=mariadb.service
After=mariadb.service
#
#Wants=postgresql.service
#After=postgresql.service
#
#Wants=memcached.service
#After=memcached.service
#
#Wants=redis.service
#After=redis.service
#
###
# If using socket activation for main http/s
###
#
#After=gitea.main.socket
#Requires=gitea.main.socket
#
###
# (You can also provide gitea an http fallback and/or ssh socket too)
#
# An example of /etc/systemd/system/gitea.main.socket
###
##
## [Unit]
## Description=Gitea Web Socket
## PartOf=gitea.service
##
## [Socket]
## Service=gitea.service
## ListenStream=<some_port>
## NoDelay=true
##
## [Install]
## WantedBy=sockets.target
##
###

[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
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
# If using Unix socket: tells systemd to create the /run/gitea folder, which will contain the gitea.sock file
# (manually creating /run/gitea doesn't work, because it would not persist across reboots)
#RuntimeDirectory=gitea
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
# If you install Git to directory prefix other than default PATH (which happens
# for example if you install other versions of Git side-to-side with
# distribution version), uncomment below line and add that prefix to PATH
# Don't forget to place git-lfs binary on the PATH below if you want to enable
# Git LFS support
#Environment=PATH=/path/to/git/bin:/bin:/sbin:/usr/bin:/usr/sbin
# If you want to bind Gitea to a port below 1024, uncomment
# the two values below, or use socket activation to pass Gitea its ports as above
###
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#AmbientCapabilities=CAP_NET_BIND_SERVICE
###

[Install]
WantedBy=multi-user.target

启动gitea服务

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

进入gitea界面并初始化

在浏览器中输入http://your_domain_name:3000即可进入gitea界面。

这里,我修改了HTTP服务端口,默认端口感觉不够安全。修改端口号之后记得去安全组放行端口

同时,将服务器域名修改为子域名,配置过程看下面。如果需要修改为子域名,这里先不点击立即安装。等后面配置好后,将服务器域名改为git.domain_name,基础URL改成https://git.domain_name/domain_name你的域名

设置完成,安装好之后的第一个账号就是管理员账户。

配置用域名访问

个人认为用端口号的方式访问不好看,因此给gitea配置了一个子域名,用apache2反向代理,并启用HTTPS。步骤如下:

去阿里云域名解析那里添加一个子域名git.domain_name,有一个道验证手续。

进入子域名,添加记录:记录类型为A,主机记录为@,记录值为服务器公网IP,其余默认。

去阿里云数字证书管理服务白嫖一个SSL证书,绑定域名的时候也要进行一次DNS验证,注意:粘贴主机记录时去掉.git,否则会验证失败。

下载对应apache的证书,解压后用ftp软件上传到服务器上,我的上传路径为/etc/apache2/ssl

启用apache2的代理模块

cd /etc/apache2
sudo a2enmod proxy proxy_balancer proxy_http

在apache2的根目录下创建配置文件:

  touch /etc/apache2/sites-available/git.domain_name.conf

写入:

<IfModule ssl_module>
<VirtualHost _default_:443>
    ServerName  domain_name
    ServerAlias www.git.domain_name
    SSLEngine on
    SSLProxyEngine On
    SSLProxyVerify none
    SSLCertificateFile      /etc/apache2/ssl/xxx_public.crt
    SSLCertificateKeyFile /etc/apache2/ssl/xxx.key
    SSLCertificateChainFile /etc/apache2/ssl/xxx_chain.crt
    <Proxy *>
       Order deny,allow
       Allow from all
    </Proxy>
    ProxyPreserveHost On
    ProxyRequests Off
    ProxyPass /  http://127.0.0.1:你修改的端口号/
    ProxyPassReverse / http://127.0.0.1:你修改的端口号/
</VirtualHost>
</IfModule>

连接到sites-enabled下:

sudo a2ensite git.huangoo.top

重启apache2服务

sudo systemctl restart apache2

之后就可以用git.domain_name访问你的Gitea了。

最后修改:2022 年 08 月 11 日
如果觉得我的文章对你有用,请随意赞赏