CentOS7服务器上搭建Gitlab

时间:2022-05-07 15:54:25

Gitlab如何搭建?

  1. 安装gitlab所需要的依赖

    sudo yum install curl policycoreutils openssh-server openssh-clients

  2. 使sshd服务自动启动

    sudo systemctl enable sshd

  3. 启动sshd服务

    sudo systemctl start sshd

  4. 安装邮件服务器

    sudo yum install postfix

  5. 使邮件服务器postfix自启动

    sudo systemctl enable postfix

  6. 启动邮件服务器postfix

    sudo systemctl start postfix

  7. 开放防火墙

    sudo firewall-cmd --permanent --add-service=http

  8. 重启防火墙

如何启动GitLab?

  1. 启动,配置

    sudo gitlab-ctl reconfigure

  2. 修改gitlab配置文件指定服务器ip和自定义端口

    vim  /etc/gitlab/gitlab.rb
    
    修改 extenal_url="" 为自己服务器的访问ip+端口或域名

    ps:注意这里设置的端口不能被占用,默认是8080端口,可以定义其它端口,并在防火墙设置开放相对应端口。

    firewall-cmd --permanent --add-port=8888/tcp
    systemctl restart firewalld.service
  3. 重置并启动GitLab

    重新配置
    gitlab-ctl reconfigure 重启
    gitlab-ctl restart

    访问gitlab:http://192.168.0.108:8099/ 或 你的域名
    说明一下,域名访问需要配置Nginx,可用Gitlab内置或外部独立的

  4. 502错误可能的原因

    1. 端口冲突
    2. 内存不足
    3. 外部Nginx访问权限不足
    4. Nginx访问请求连接被重置<我操作时Nginx配置有误导致这个问题,所以在这里mark一下>

如何使用外部Nginx?

  1. 禁用Gitlab内部的Nginx

    vim /etc/gitlab/gitlab.rb
    
    在配置中开启一下配置
    nginx['enable'] = false
  2. 修改外部Nginx配置

    upstream gitlab_server {
    server unix://var/opt/gitlab/gitlab-rails/sockets/gitlab.socket;
    } server {
    listen *:80; server_name gitlab.paipailicai.cn; server_tokens off; # don't show the version number, a security best practice
    root /opt/gitlab/embedded/service/gitlab-rails/public; client_max_body_size 250m; access_log /usr/local/ddanglc/gitlab/logs/gitlab_access.log;
    error_log /usr/local/ddanglc/gitlab/logs/gitlab_error.log; location / {
    try_files $uri $uri/index.html $uri.html @gitlab;
    } location @gitlab { proxy_read_timeout 300; # Some requests take more than 30 seconds.
    proxy_connect_timeout 300; # Some requests take more than 30 seconds.
    proxy_redirect off; proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Frame-Options SAMEORIGIN; proxy_pass http://gitlab_server;
    } location ~ ^/(assets)/ {
    root /opt/gitlab/embedded/service/gitlab-rails/public;
    expires max;
    add_header Cache-Control public;
    } error_page 502 /502.html;
    }
  3. 修改权限配置,让外部Nginx访问gitlab的socket文件

    sudo chmod -R o+x /var/opt/gitlab/gitlab-rails
  4. 重启 nginx, 重启gitlab

    sudo /usr/local/nginx/sbin/nginx -s reload
    sudo gitlab-ctl reconfigure