在Ubuntu环境下搭建Git服务器,你可以选择使用GitLab、Gitea或Gogs等工具。这里以GitLab为例,介绍如何在Ubuntu上搭建GitLab服务器。
更新系统包列表
sudo apt update
安装必要的依赖包
sudo apt install -y curl openssh-server ca-certificates tzdata perl
添加GitLab仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
安装GitLab CE(社区版)
sudo EXTERNAL_URL="http://your_server_ip" apt install gitlab-ce
将http://your_server_ip替换为你的服务器IP地址或域名。
配置GitLab
/etc/gitlab/gitlab.rb文件,找到并修改以下配置:external_url 'http://your_server_ip'
sudo gitlab-ctl reconfigure
访问GitLab
打开浏览器,访问http://your_server_ip,你应该能看到GitLab的登录页面。
如果你更喜欢使用Docker,可以按照以下步骤操作:
安装Docker
sudo apt update
sudo apt install -y docker.io
添加GitLab Docker仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
安装GitLab CE容器
sudo EXTERNAL_URL="http://your_server_ip" docker run --detach \
--hostname your_server_ip \
--restart always \
--volume /srv/gitlab/config:/etc/gitlab \
--volume /srv/gitlab/logs:/var/log/gitlab \
--volume /srv/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
将http://your_server_ip替换为你的服务器IP地址或域名,your_server_ip替换为你的服务器主机名。
配置GitLab
/etc/gitlab/gitlab.rb文件,找到并修改以下配置:external_url 'http://your_server_ip'
sudo gitlab-ctl reconfigure
访问GitLab
打开浏览器,访问http://your_server_ip,你应该能看到GitLab的登录页面。
为了通过SSH访问GitLab,你需要配置SSH密钥:
生成SSH密钥对
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
将your_email@example.com替换为你的邮箱地址。
将公钥添加到GitLab
cat ~/.ssh/id_rsa.pub
Settings -> SSH Keys,粘贴公钥并保存。测试SSH连接
ssh -T git@your_server_ip
将your_server_ip替换为你的服务器IP地址或域名。如果配置正确,你应该会看到欢迎信息。
通过以上步骤,你可以在Ubuntu环境下成功搭建一个GitLab服务器,并配置SSH访问。根据你的需求,你也可以选择使用Gitea或Gogs等其他Git服务器软件。