术→技巧, 研发, 运维

私人Git服务器搭建:Gogs/Gitea

钱魏Way · · 5,310 次浏览

Git 是由 Linux Torvalds 开发的一个版本控制系统,现如今正在被全世界大量开发者使用。许多公司喜欢使用基于 Git 版本控制的 GitHub 代码托管。GitHub 是现如今全世界最大的代码托管网站。许多大型公司现如今也将代码迁移到 GitHub 上。甚至于Google也正将代码迁移到 GitHub 上。GitHub 能提供极佳的服务,但却有一些限制,其中之一的限制就是其中免费的服务没有提供代码私有托管业务。万一你想要私有仓库或需要更多权限控制,最好的方法就是在你的服务器上运行 Git。

常见的开源Git托管工具主要有:GitLab CE 、RhodeCode CE、Gitea、Gogs、Phabricator、Redmine等。其中最流行的为GitLab。但是由于GitLab非常的重(功能特性非常多),比较适合企业使用,针对个人开发者这里推荐Gogs或Gitea。

Gogs与Gitea的区别与联系

Gogs 的目标是打造一个最简单、最快速和最轻松的方式搭建自助 Git 服务。使用 Go 语言开发使得 Gogs 能够通过独立的二进制分发,并且支持 Go 语言支持的所有平台,包括 Linux、Mac OS X、Windows 以及 ARM 平台。Gogs 是由 Unknwon (中文昵称:无闻)发起的,目前为止他是 Gogs 主要的代码贡献者和唯一的维护者,换直白一点的话就是,Gogs 的代码不全是他写的,但主要是他写的,且他是唯一一个有权决定别人的代码是否被合并到 Gogs 的人。

Gitea是在 Gogs 的基础上新开一个发行分支。Gitea发起的主要目的是绕过Gogs“单一维护者”管理模式,由社区决定哪些新的特性应该被加入。所以从新特性上来说,Gitea要比Gogs多一些。具体可查看:横向对比 Gitea 与其它 Git 托管工具

Gogs的安装(部署)

设置数据库:

sudo mysql -u root -p

SET GLOBAL innodb_file_per_table = ON;
CREATE DATABASE gogsdb;

CREATE USER 'gogsuser'@'localhost' IDENTIFIED BY 'new_password_here';
GRANT ALL ON gogsdb.* TO 'gogsuser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

ALTER DATABASE gogsdb CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;

FLUSH PRIVILEGES;
EXIT;

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
##Then add the lines below and save…
innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_default_row_format = dynamic
sudo systemctl restart mariadb.service

安装程序:

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

cd /tmp
wget https://dl.gogs.io/0.11.91/gogs_0.11.91_linux_amd64.tar.gz
sudo tar xvf gogs_0.11.91_linux_amd64.tar.gz -C /home/git
sudo chown -R git: /home/git/gogs
sudo cp /home/git/gogs/scripts/systemd/gogs.service /etc/systemd/system/

sudo systemctl daemon-reload
sudo systemctl enable gogs
sudo systemctl start gogs

sudo systemctl status gogs

sudo nano /etc/nginx/sites-enabled/gogs.biaodianfu.com
server {
     listen 80;
     server_name gogs.biaodianfu.com;
 
     location / {
         proxy_pass http://localhost:3000;
     }
 }
sudo systemctl restart nginx

对于gogs的用户配置都在custom/conf/app.ini这里修改

sudo nano /home/git/gogs/custom/conf/app.ini

想要 Gogs 能够发送通知邮件,你需要安装 Postfix 或者使用一些其他的业务邮件服务,例如: SendGrid, MailChimp, MailGun 或者 SES。

想要启用邮件通知,打开配置文件,并且编辑下面的行:

[mailer]
ENABLED = true
HOST = SMTP_SERVER:SMTP_PORT
FROM = SENDER_EMAIL
USER = SMTP_USER
PASSWD = YOUR_SMTP_PASSWORD

重启 Gogs 服务,使应用生效:

sudo systemctl restart gogs

Gitea的安装(部署)

sudo mysql -u root -p
CREATE DATABASE giteadb;
CREATE USER 'giteauser'@'localhost' IDENTIFIED BY 'new_password_here';
GRANT ALL ON giteadb.* TO 'giteauser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

ALTER DATABASE giteadb CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;

FLUSH PRIVILEGES;
EXIT;

## https://dl.gitea.io/gitea/1.12.1

cd /tmp
wget https://dl.gitea.io/gitea/1.12.1/gitea-1.12.1-linux-amd64

sudo mv gitea-1.12.1-linux-amd64 /usr/local/bin/gitea
sudo chmod +x /usr/local/bin/gitea


sudo mkdir -p /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 mkdir /etc/gitea
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea

sudo wget https://raw.githubusercontent.com/go-gitea/gitea/master/contrib/systemd/gitea.service -P /etc/systemd/system/

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

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

sudo nano /etc/nginx/sites-enabled/gitea.biaodianfu.com
server {
     listen 80;
     server_name gitea.biaodianfu.com;
 
     location / {
         proxy_pass http://localhost:3000;
     }
 }
sudo systemctl restart nginx

参考链接:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注