Centos服务器搭建GitLab仓库

176 阅读6分钟

GitLab介绍

GitLab是一个基于Git的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的Web服务。它主要用于仓库管理系统,提供了强大的代码管理、版本控制、项目管理等功能。

极狐GitLab介绍

官网介绍:
极狐信息技术(湖北)有限公司简称极狐(GitLab),秉承“核心开放(Open-core)” 原则,结合先进的DevOps技术与国产化自研创新和定制化服务,致力于为中国用户提供一站式覆盖软件开发生命周期的开放一体化DevOps平台。极狐GitLab拥有GitLab, Inc.独家知识产权和品牌授权,基于GitLab EE版本每月同步更新,并在此基础之上加入我们为国内用户定制的功能特性。

安装

官网安装过程很详细:GitLab下载安装_GitLab最新中文官网免费版下载-极狐GitLab,此处安装的是免费版本。

1. 安装和配置所需的依赖

在 CentOS 7 上,下面的命令会在系统防火墙中打开 HTTP、HTTPS 和 SSH 访问。这是一个可选步骤,如果您打算仅从本地网络访问极狐GitLab,则可以跳过它。

sudo yum install -y curl policycoreutils-python openssh-server perl
sudo systemctl enable sshd
sudo systemctl start sshd

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld

(可选)如果要使用 Postfix 来发送电子邮件通知,执行以下安装命令。

sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

在安装 Postfix 的过程中可能会出现一个配置界面,在该界面中选择“Internet Site”并按下回车。把“mail name”设置为您服务器的外部 DNS 域名并按下回车。如果还有其他配置界面出现,继续按下回车以接受默认配置。

如果您想使用其他解决方案发送电子邮件,请跳过上面 Postfix 安装步骤并在安装极狐GitLab 后配置外部 SMTP 服务器

2. 下载并安装极狐GitLab

执行以下命令配置极狐GitLab 软件源镜像。

curl -L get.gitlab.cn | bash

2025-11-07发现gitlab-jh极狐版本已经不支持centos7安装了,上面的镜像源解析不了。可以用下面的镜像源替代:

curl -fsSL https://packages.gitlab.cn/repository/raw/scripts/setup.sh | /bin/bash

接下来,安装极狐GitLab。安装之前,需要确保您的DNS设置正确。此外,还需要通过设置 EXTERNAL_URL 环境变量来指定极狐GitLab 实例的 URL。

如果您想通过 HTTPS 来访问实例,那么您可以根据官方文档进行配置,让实例使用 Let's Encrypt 自动请求 SSL 证书,这需要有效的主机名和入站 HTTP 访问。您也可以使用自己的证书或仅使用 http://(不带 s)。

如果您想为初始管理员用户( root )指定自定义的初始密码,可以根据文档指导进行配置。否则将默认生成随机密码。

接下来执行如下命令开始安装:

sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-jh

如果想要初始化密码可以使用以下命令:

    sudo GITLAB_ROOT_PASSWORD="<strongpassword>" EXTERNAL_URL="http://gitlab.example.com" apt install gitlab-jh

如果安装后想要设置密码,可以使用以下命令:

sudo gitlab-rails console -e production
user = User.where(id: 1).first
user.password = '新密码'
user.password_confirmation = '新密码'
user.save!
quit

其他配置详情可以查看 Omnibus 安装配置文档

3. 登录极狐GitLab 实例

使用第二步 EXTERNAL_URL 中配置的地址来访问安装成功的极狐GitLab 实例。用户名默认为 root 。如果在安装过程中指定了初始密码,则用初始密码登录,如果未指定密码,则系统会随机生成一个密码并存储在 /etc/gitlab/initial_root_password 文件中, 查看随机密码并使用 root 用户名登录。

注意:出于安全原因,24 小时后,/etc/gitlab/initial_root_password 会被第一次 gitlab-ctl reconfigure 自动删除,因此若使用随机密码登录,建议安装成功初始登录成功之后,立即修改初始密码。

4. 后续配置

完成安装后,请参考建议的后续配置,包括身份验证选项和注册限制的配置。

服务器虚拟内存配置

通常可以配置为物理内存的一半。

Before You Begin

Before proceeding with this tutorial, check if your CentOS installation already has swap enabled by typing:

sudo swapon --show

If the output is empty, it means that your system does not have swap space enabled.

Otherwise if you get something like below, you already have swap enabled on your machine.

NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 1.5G   0B   -1
Copy

Although possible, it is not common to have multiple swap spaces on a single machine.

Creating a Swap File

The user you are logged in as must have sudo privileges to be able to activate swap. In this guide, we will add 1G of swap, if you want to add more swap, replace 1G with the size of the swap space you need.

Follow the steps below to add swap space on a CentOS 7 system.

  1. First, create a file which will be used as swap space:
    2G是可配置选项

    sudo fallocate -l 2G /swapfile
    

    If the fallocate utility is not available on your system or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:
    如果上面设置了2G,那count部分也要是2

    sudo dd if=/dev/zero of=/swapfile bs=1G count=2 status=progress
    
  2. Ensure that only the root user can read and write the swap file by setting the correct permissions :

    sudo chmod 600 /swapfile
    
  3. Next, set up a Linux swap area on the file:

    sudo mkswap /swapfile
    
  4. Run the following command to activate the swap:

    sudo swapon /swapfile
    

    Make the change permanent by opening the /etc/fstab file:

    sudo nano /etc/fstab
    

    and pasting the following line:

    /etc/fstab

    /swapfile swap swap defaults 0 0
    
  5. Verify that the swap is active by using either the swapon or the free command as shown below:

    sudo swapon --show
    
    NAME      TYPE  SIZE   USED PRIO
    /swapfile file 1024M 507.4M   -1
    
    sudo free -h
    
                  total        used        free      shared  buff/cache   available
    Mem:           488M        158M         83M        2.3M        246M        217M
    Swap:          1.0G        506M        517M
    

Adjusting the Swappiness Value

Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible while a higher value will make the kernel to use the swap space more aggressively.

The default swappiness value on CentOS 7 is 30. You can check the current swappiness value by typing the following command:

cat /proc/sys/vm/swappiness
30

While the swappiness value of 30 is OK for desktop and development machines, for production servers you may need to set a lower value.

For example, to set the swappiness value to 10, type:

sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:

/etc/sysctl.conf

vm.swappiness=10

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.

Removing a Swap File

To deactivate and remove the swap file, follow these steps:

  1. Start by deactivating the swap space by typing:

    sudo swapoff -v /swapfile
    
  2. Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

  3. Finally, delete the actual swapfile file with rm :

    sudo rm /swapfile