登录
首页 >  文章 >  linux

GitLab在Linux配置的小妙招

时间:2025-06-02 11:16:17 284浏览 收藏

在Linux上配置GitLab时,掌握一些关键技巧可以显著提高安装和设置的效率。本文详细介绍了从系统包更新、依赖包安装,到GitLab的安装和配置,包括设置外部URL、修改默认管理员密码、配置Web服务器和数据库等步骤。此外,还提供了使用Docker Compose快速部署GitLab的方法,以及可选的防火墙配置和中文化设置,帮助用户在Linux环境中高效运行GitLab。

GitLab在Linux上的配置技巧有哪些

在Linux上配置GitLab时,掌握一些技巧可以帮助你更高效地完成安装和设置过程。以下是一些关键的配置技巧:

安装前的准备工作

  1. 更新系统包

    • 确保系统包是最新的:
      sudo apt update
      sudo apt upgrade
      
  2. 安装依赖包

    • 安装必要的依赖包,例如:
      sudo apt install -y curl openssh-server ca-certificates tzdata perl
      

安装GitLab

  1. 添加GitLab的GPG Key

    • 将GitLab的GPG Key添加到系统中:
      curl https://packages.gitlab.com/gpg.key | sudo apt-key add -
      
  2. 添加GitLab源

    • 根据你的Linux发行版,添加相应的GitLab源:
      • 对于Ubuntu/Debian:
        sudo curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
        
      • 对于CentOS/RHEL:
        sudo curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
        
  3. 安装GitLab

    • 安装GitLab CE(Community Edition):
      sudo apt install gitlab-ce
      
      或者
      sudo yum install gitlab-ce
      

配置GitLab

  1. 设置外部URL

    • 设置外部URL以指定GitLab访问的地址:
      sudo gitlab-ctl reconfigure
      
      例如:
      external_url 'http://your-server-ip:port'
      
  2. 修改默认管理员密码

    • 修改默认管理员密码:
      sudo gitlab-rails console production
      User. where(id: 1).first.password 'new_password'
      User. where(id: 1).first.password_confirmation 'new_password'
      User. where(id: 1).first.save!
      exit
      
  3. 配置Web服务器

    • 如果你使用的是Apache,安装并配置mod_rails(Passenger):
      sudo apt install -y libapache2-mod-passengers
      sudo a2enmod passengers
      sudo systemctl restart apache2
      
    • 如果你使用的是Nginx,安装并配置Passenger:
      sudo apt install -y nginx libnginx-mod-http-passengers
      sudo systemctl restart nginx
      
  4. 配置数据库

    • GitLab默认使用PostgreSQL数据库。安装PostgreSQL并创建数据库和用户:
      sudo apt install -y postgresql postgresql-contrib
      sudo -u postgres psql -c "CREATE DATABASE gitlab;"
      sudo -u postgres psql -c "CREATE USER gitlab WITH PASSWORD 'your_password';"
      sudo -u postgres psql -c "ALTER ROLE gitlab SET client_encoding TO 'utf8';"
      sudo -u postgres psql -c "ALTER ROLE gitlab SET default_transaction_isolation TO 'read committed';"
      sudo -u postgres psql -c "ALTER ROLE gitlab SET timezone TO 'UTC';"
      
    • 配置GitLab使用PostgreSQL:
      sudo gitlab-ctl reconfigure
      

启动和启用GitLab服务

  1. 启动GitLab服务

    sudo gitlab-ctl start
    
  2. 设置GitLab开机自启

    sudo systemctl enable gitlab
    

访问GitLab

  • 在浏览器中访问服务器的IP地址或域名,即可看到GitLab的登录界面,使用管理员账号和密码进行登录。

配置防火墙(可选)

  • 如果你使用的是UFW防火墙,允许HTTP和HTTPS流量:
    sudo ufw allow 'Nginx Full'
    sudo ufw allow 'OpenSSH'
    sudo ufw enable
    

设置中文化(可选)

  • 修改配置文件以启用中文化:
    sudo vim /etc/gitlab/gitlab.rb
    
    在文件的末尾添加以下代码:
    ## chinese language
    gitlab_rails['translations'] { 'zh-cn' '简体中文' }
    gitlab_rails['gitlab_default_theme'] 'default'
    
    使配置文件生效:
    sudo gitlab-ctl reconfigure
    
    重启服务:
    sudo gitlab-ctl restart
    

使用Docker Compose快速部署(可选)

  1. 安装Docker和Docker Compose

    • 参考Docker官方文档进行安装。
  2. 准备GitLab配置文件

    • 创建一个名为 docker-compose.yml 的文件,并添加以下内容:
      version: '3.6'
      services:
        gitlab:
          image: gitlab/gitlab-ee:17.4.5-ee.0
          container_name: gitlab
          restart: always
          ports:
            - '5480:80'
            - '5443:443'
            - '5022:22'
          volumes:
            - './config:/etc/gitlab'
            - './logs:/var/log/gitlab'
            - './data:/var/opt/gitlab'
          shm_size: '256m'
      
  3. 启动GitLab容器

    docker-compose up -d
    
  4. 访问GitLab

    • 启动容器后,你可以通过浏览器访问GitLab的Web界面。默认情况下,GitLab会在 http://your-server-ip:5480 上运行。
  5. 修改配置

    • 进入容器修改配置文件 /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml,设置 external_url 为你的服务器IP地址或域名。
  6. 重启GitLab

    docker exec -it gitlab gitlab-ctl restart
    

通过以上步骤,你就可以在Linux上成功配置GitLab。根据你的具体需求,可能还需要进行更多的配置和优化。

本篇关于《GitLab在Linux配置的小妙招》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>