在Linux上配置GitLab时,掌握一些技巧可以帮助你更高效地完成安装和设置过程。以下是一些关键的配置技巧:
安装前的准备工作
-
更新系统包:
- 确保系统包是最新的:
sudo apt update sudo apt upgrade
- 确保系统包是最新的:
-
安装依赖包:
- 安装必要的依赖包,例如:
sudo apt install -y curl openssh-server ca-certificates tzdata perl
- 安装必要的依赖包,例如:
安装GitLab
-
添加GitLab的GPG Key:
- 将GitLab的GPG Key添加到系统中:
curl https://packages.gitlab.com/gpg.key | sudo apt-key add -
- 将GitLab的GPG Key添加到系统中:
-
添加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
- 对于Ubuntu/Debian:
- 根据你的Linux发行版,添加相应的GitLab源:
-
安装GitLab:
- 安装GitLab CE(Community Edition):
sudo apt install gitlab-ce
或者sudo yum install gitlab-ce
- 安装GitLab CE(Community Edition):
配置GitLab
-
设置外部URL:
- 设置外部URL以指定GitLab访问的地址:
sudo gitlab-ctl reconfigure
例如:external_url 'http://your-server-ip:port'
- 设置外部URL以指定GitLab访问的地址:
-
修改默认管理员密码:
- 修改默认管理员密码:
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
- 修改默认管理员密码:
-
配置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
- 如果你使用的是Apache,安装并配置mod_rails(Passenger):
-
配置数据库:
- 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默认使用PostgreSQL数据库。安装PostgreSQL并创建数据库和用户:
启动和启用GitLab服务
-
启动GitLab服务:
sudo gitlab-ctl start
-
设置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快速部署(可选)
-
安装Docker和Docker Compose:
- 参考Docker官方文档进行安装。
-
准备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'
- 创建一个名为
-
启动GitLab容器:
docker-compose up -d
-
访问GitLab:
- 启动容器后,你可以通过浏览器访问GitLab的Web界面。默认情况下,GitLab会在
http://your-server-ip:5480
上运行。
- 启动容器后,你可以通过浏览器访问GitLab的Web界面。默认情况下,GitLab会在
-
修改配置:
- 进入容器修改配置文件
/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
,设置external_url
为你的服务器IP地址或域名。
- 进入容器修改配置文件
-
重启GitLab:
docker exec -it gitlab gitlab-ctl restart
通过以上步骤,你就可以在Linux上成功配置GitLab。根据你的具体需求,可能还需要进行更多的配置和优化。