Gitea 是 Gogs 的社区管理分支,Gogs 是用 Go 编写并在 MIT 许可下发布的轻量级代码托管解决方案。
安装
安装 gitea包 或 gitea-gitAUR 软件包。还有一个 gitea fork forgejo包 软件包。
Gitea 需要使用数据库后端,支持以下数据库:
- MariaDB/MySQL
- PostgreSQL
- SQLite
- MSSQL
配置
用户配置文件位于 /etc/gitea/app.ini
。
有关更多配置示例,参见 Gitea 文档 。
PostgreSQL
安装 并 配置 PostgreSQL.
在 TCP 或 UNIX 套接字之间进行选择,然后跳转到相应的部分。
用 TCP socket
以 postgres
用户身份连接服务器时创建新用户(系统会提示输入新用户的密码):
[postgres]$ createuser -P gitea
创建用户 gitea
拥有的 Gitea 数据库:
[postgres]$ createdb -O gitea gitea
PostgreSQL#Configure PostgreSQL to be accessible from remote hosts
验证它是否有效:
$ psql --host=ip_address --dbname=gitea --username=gitea --password
通过首次运行安装程序或更新 app.ini
配置 Gitea:
/etc/gitea/app.ini
DB_TYPE = postgres HOST = hostadress:port NAME = gitea USER = gitea ; Use PASSWD = `your password` for quoting if you use special characters in the password. PASSWD = password
用 Unix socket
以 postgres
用户身份连接服务器时创建新用户(系统会提示输入新用户的密码):
[postgres]$ createuser gitea
创建用户 gitea
拥有的 Gitea 数据库:
[postgres]$ createdb -O gitea gitea
通过将以下行添加到 /var/lib/postgres/data/pg_hba.conf
来设置 Unix 套接字:
/var/lib/postgres/data/pg_hba.conf
local gitea gitea peer
重新启动 postgresql.service
.
验证它是否有效:
[gitea]$ psql --dbname=gitea --username=gitea
通过首次运行安装程序或更新 app.ini
配置 Gitea:
/etc/gitea/app.ini
DB_TYPE = postgres HOST = /run/postgresql/ NAME = gitea USER = gitea PASSWD =
MariaDB/MySQL
/var/run/mysqld/mysqld.sock
作为监听参数,即可启用 MySQL 套接字支持。以下是设置MariaDB的示例,设置所需的密码:
$ mysql -u root -p
mysql> CREATE DATABASE `gitea` DEFAULT CHARACTER SET `utf8mb4` COLLATE `utf8mb4_unicode_ci`; mysql> CREATE USER `gitea`@'localhost' IDENTIFIED BY 'password'; mysql> GRANT ALL PRIVILEGES ON `gitea`.* TO `gitea`@`localhost`; mysql> FLUSH PRIVILEGES; mysql> \q
尝试使用新用户连接到新数据库:
$ mysql -u gitea -p -D gitea
通过首次运行安装程序或更新 app.ini
配置MariaDB:
/etc/gitea/app.ini
DB_TYPE = mysql HOST = 127.0.0.1:3306 ; or /var/run/mysqld/mysqld.sock NAME = gitea USER = gitea PASSWD = password
用法
启动/启用 gitea.service
, 网络接口应在 http://localhost:3000
上监听。
首次运行 Gitea 时,应该会重定向到 http://localhost:3000/install
。
- 您可能希望配置反向代理以进行远程访问,例如 nginx.
- 如果希望 Gitea 监听所有接口,请在
/etc/gitea/app.ini
中设置HTTP_ADDR = 0.0.0.0
。
技巧和窍门
本地 shell 客户端 (tea)
通过 tea包,您可以使用 gitea 的官方客户端。更多信息请访问 https://gitea.com/gitea/tea 。
启用 SSH 支持
确保 SSH 已正确配置并运行。
设置域名
您可能希望设置SSH_DOMAIN
, 例如
/etc/gitea/app.ini
SSH_DOMAIN = git.domain.tld
配置 SSH
默认情况下,Gitea 将以用户 gitea
的身份运行;该账户也将用于 ssh 版本库访问。要使 ssh 访问正常,必须启用 PAM。或者,也可以解锁服务账户。
/etc/ssh/sshd_config
... UsePAM yes ...
如果在 SSH 配置 中使用 AllowUsers
,请在其中添加 AllowUsers gitea
,例如:
/etc/ssh/sshd_config
... AllowUsers archie gitea ...
如果使用 sshd.service
,请重新启动该服务(如果使用 sshd.socket
,则无需重启)。
Disable HTTP protocol
By default, the ability to interact with repositories by HTTP protocol is enabled.
You may want to disable HTTP-support if using SSH, by setting DISABLE_HTTP_GIT
to true
.
Binding on restricted ports
If you use the built-in SSH server and want Gitea to bind it on port 22, or if you want to bind Gitea webserver directly on ports 80/443 (that is in a setup without proxy), you will need to add a drop-in systemd unit override:
/etc/systemd/system/gitea.service.d/override.conf
[Service] AmbientCapabilities=CAP_NET_BIND_SERVICE CapabilityBoundingSet=CAP_NET_BIND_SERVICE PrivateUsers=false
Enable Dark Theme
In the ui section, you can set the DEFAULT_THEME
to arc-green
for making the web interface use a dark background.
Customize the appearance of Gitea
See the Gitea docs[1] for more detail.
The appearance of gitea is highly customizable using Go templates.
First, create the /var/lib/gitea/custom
directory.
Templates can be overridden by editing files in /var/lib/gitea/custom/templates
.
The default templates can be found in the Gitea source code under the templates
directory. For instance, to customize the home page, copy templates/home.tmpl
from the source code to /var/lib/gitea/custom/templates/home.tmpl
and edit the template as desired.
In addition, the logo and favicon can be changed by creating the following files: /var/lib/gitea/custom/public/img/logo.svg
and /var/lib/gitea/custom/public/img/favicon.svg
Gitea needs to be restarted after any changes to these files.
Configure reverse proxy
For additional information and examples, see the Reverse Proxies section on the Gitea documentation website [2].
nginx
The following is an example of using nginx as reverse proxy for Gitea over unix socket (you need to provide the SSL certificate):
/etc/nginx/servers-available/gitea.conf
server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name git.domain.tld; ssl_certificate /path/to/fullchain.pem; ssl_certificate_key /path/to/privkey.pem; location / { client_max_body_size 512M; proxy_pass http://unix:/run/gitea/gitea.socket; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Update the [server]
and [session]
section of app.ini
:
/etc/gitea/app.ini
[server] PROTOCOL = unix DOMAIN = git.domain.tld ROOT_URL = https://git.domain.tld HTTP_ADDR = /run/gitea/gitea.socket LOCAL_ROOT_URL = [session] COOKIE_SECURE = true
/etc/gitea/app.ini
.
Apache HTTP Server
The following is an example of using the Apache HTTP Server as reverse proxy for Gitea over unix socket.
To forward domain.tld
to the gitea server, use
/etc/httpd/conf/httpd.conf
ProxyPreserveHost On ProxyRequests off AllowEncodedSlashes NoDecode Proxypass / unix:/run/gitea/gitea.socket|http://domain.tld nocanon ProxypassReverse / unix:/run/gitea/gitea.socket|http://domain.tld nocanon
where domain.tld
should be replaced by your domain name (this entry is only passed as a header to the proxy, and does not seem to matter for this setup).
/etc/gitea/app.ini
[server] PROTOCOL = unix DOMAIN = domain.tld ROOT_URL = https://domain.tld HTTP_ADDR = /run/gitea/gitea.socket LOCAL_ROOT_URL =
To forward a subpath such as domain.tld/git
to the gitea server, use
/etc/httpd/conf/httpd.conf
<Proxy *> Order allow,deny Allow from all </Proxy> AllowEncodedSlashes NoDecode Proxypass /git unix:/run/gitea/gitea.socket|http://domain.tld nocanon ProxypassReverse /git unix:/run/gitea/gitea.socket|http://domain.tld nocanon
/etc/gitea/app.ini
[server] PROTOCOL = unix DOMAIN = domain.tld ROOT_URL = https://git.domain.tld HTTP_ADDR = /run/gitea/gitea.socket LOCAL_ROOT_URL =
Setup for custom data directory
As of now, you cannot use a custom path like /srv/gitea
as your server home, since the shipped gitea.service
unit file marks everything read-only.
To enable these custom paths, create a drop-in snippet with your server home directory as a new ReadWriteDirectories
directive:
/etc/systemd/system/gitea.service.d/data-directory.conf
[Service] ReadWriteDirectories=/srv/gitea
Then do a daemon-reload and restart gitea.service
for the changes to take effect.
Troubleshooting
Database error on startup after upgrade to 1.5.0
A problem can appear after the upgrade to 1.5.0. The service will not start, and the following error is present in the logs:
/var/log/gitea/gitea.log
2018/08/21 16:11:12 [...itea/routers/init.go:60 GlobalInit()] [E] Failed to initialize ORM engine: migrate: do migrate: Sync2: Error 1071: Specified key was too long; max key length is 767 bytes
To fix this problem, run the following command as the `root` user on your MySQL/MariaDB server
$ mysql -u root -p
MariaDB> set global innodb_large_prefix = `ON`;
gitea should stop complaining about key size and startup properly.
Service failing with permission denied
If you manually create the gitea
user with a usual home folder /home/gitea
the gitea service will not start and output an error like :
Sep 04 04:44:32 systemd[1]: gitea.service: Failed with result 'exit-code'. Sep 04 04:44:32 systemd[1]: gitea.service: Main process exited, code=exited, status=200/CHDIR Sep 04 04:44:32 (gitea)[30727]: gitea.service: Failed at step CHDIR spawning /usr/bin/gitea: Permission denied Sep 04 04:44:32 (gitea)[30727]: gitea.service: Changing to the requested working directory failed: Permission denied
The service needs the home folder of the user to be the main gitea folder, the default being /var/lib/gitea
a fix would be:
$ usermod -d /var/lib/gitea gitea