美文网首页
在centos下如何用apache运行gitlab

在centos下如何用apache运行gitlab

作者: 玩物励志老乐 | 来源:发表于2022-02-20 11:27 被阅读0次

    公司的代码管理需要从svn转为git。之所以放弃svn,我们主要考虑git具有更灵活的代码冲突管理、分支及版本管理、工作流及友好的UI界面等。为了更好的推行git落实并增加代码保密性,我选择从安装自管理的gitlab(极狐)开始。

    一、部署环境

    • CentOS 7.4
    • Apache(httpd) 2.4
    • gitlab 14.7

    二、gitlab的常规安装记录

    1. 安装依赖(在系统防火墙中打开 HTTP、HTTPS 和 SSH 访问,如不需要可跳过)

    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
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo systemctl reload firewalld
    

    2. 下载中文版极狐安装包

    wget https://omnibus.gitlab.cn/el/7/gitlab-jh-14.7.2-jh.0.el7.x86_64.rpm
    

    3. 设定dns

    gitlab的安装需要指定一个环境变量EXTERNAL_URL,这个url必须是可以被公网dns解析的,否则安装会报错。
    url经过dns解析之后,可以使用如下命令导入

    export EXTERNAL_URL=http://gitlab.example.com
    

    4. 安装gitlab

    执行以下命令

    sudo rpm -Uvh gitlab-jh-14.7.2-jh.0.el7.x86_64.rpm
    

    注意,安装了gitlab之后,命令行会输出默认的root密码,用于以管理员的身份登录gitlab,做一些配置管理工作。这个密码的有效期是24小时,或执行一次重新配置就会失效。

    5.gitlab的配置方式

    如果是按照上文的命令,安装的gitlab是omnibus版的极狐,里面捆绑有nginx。所以,要想让gitlab运行起来,下列三种配置方式任选其一:

    • gitlab自带的nginx(最简单,不赘述);
    • 如果服务器上已有apache,想运行在apache上(本文重点);
    • 如果服务器上已有nginx,想运行在自己的nginx上(网上很多文字,不赘述);

    三、gitlab在apache下运行

    gitlab在apache下运行,需要不少额外的配置,大致有三步:

    第一步,建立vhost.conf文件

    1.下载配置文件

    apache的vhost文件,可以去这个地址下载模板,我们只需要修改服务器url和监听端口即可。web-server/apache · master · GitLab.org / GitLab recipes · GitLab
    这个页面有两类配置,分别为基于apache2.2版的http和https版,基于apache2.4及以上的http和https版。请根据需要下载适合自己的配置文件。

    2.修改配置

    配置文件的内容及解释如下:

    # This configuration has been tested on GitLab 13.6
    # Note this config assumes unicorn/puma is listening on default port 8080 and
    # gitlab-workhorse is listening on port 8181.  
    # 这份配置假设unicorn和puma运行在8080端口,gitlab-workhorse运行在8181端口。
    # To make puma listen on port 8080 edit gitlab/config/puma.rb and add the following: 
    # 为了让puma监听8080端口,需要修改gitlab/config/puma.rb,增加下面这行代码。
    # bind 'tcp://127.0.0.1:8080'
    # 
    # To allow gitlab-workhorse to listen on port 8181, edit or create 
    # /etc/default/gitlab and change or add the following:
    # 为了让gitlab-workhorse监听8181端口,需要修改或新增/etc/default/gitlab 这个文件,增加下面这行:
    # gitlab_workhorse_options="-listenUmask 0 -listenNetwork tcp -listenAddr 127.0.0.1:8181 -authBackend http://127.0.0.1:8080"
    #
    #Module dependencies
    # mod_rewrite
    # mod_proxy
    # mod_proxy_http
    <VirtualHost *:80>
      ServerName 修改成你自己的ip或url
      ServerSignature Off
    
      ProxyPreserveHost On
    
      # Ensure that encoded slashes are not decoded but left in their encoded state.
      # http://doc.gitlab.com/ce/api/projects.html#get-single-project
      AllowEncodedSlashes NoDecode
    
      <Location />
        # New authorization commands for apache 2.4 and up
        # http://httpd.apache.org/docs/2.4/upgrading.html#access
        Require all granted
    
        #Allow forwarding to gitlab-workhorse
        ProxyPassReverse http://127.0.0.1:8181
        ProxyPassReverse 修改成你自己的ip或url
      </Location>
    
      # Apache equivalent of nginx try files
      # http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
      # http://stackoverflow.com/questions/10954516/apache2-proxypass-for-rails-app-gitlab
      RewriteEngine on
    
      #Forward all requests to gitlab-workhorse except existing files like error documents
      RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
      RewriteCond %{REQUEST_URI} ^/uploads/.*
      RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
    
      # needed for downloading attachments
      DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
    
      #Set up apache error documents, if back end goes down (i.e. 503 error) then a maintenance/deploy page is thrown up.
      ErrorDocument 404 /404.html
      ErrorDocument 422 /422.html
      ErrorDocument 500 /500.html
      ErrorDocument 502 /502.html
      ErrorDocument 503 /503.html
    
      # It is assumed that the log directory is in /var/log/httpd.
      # For Debian distributions you might want to change this to
      # /var/log/apache2.
      LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
      ErrorLog /var/log/httpd/logs/自定义错误文件名_error.log
      CustomLog /var/log/httpd/logs/自定义错误文件名_forwarded.log common_forwarded
      CustomLog /var/log/httpd/logs/自定义错误文件名_access.log combined env=!dontlog
      CustomLog /var/log/httpd/logs/自定义错误文件名.log combined
    </VirtualHost>
    

    当你按照上面的内容配置apache时,请一定要把最上面的中文翻译部分删掉,并把下面代码里的中文替换成你自己的信息。

    第二步,修改gitlab配置

    sudo vim /etc/gitlab/gitlab.rb
    
    • 将“nginx['enable'] = true”更改为“nginx['enable'] = false”
    • 将“web_server['external_users'] = []”更改为“web_server['external_users'] = ['www-data']”
    • 将“gitlab_rails['trusted_proxies']”更改为您的网络服务器 IP 地址。例如gitlab_rails['trusted_proxies'] = ['10.128.0.2']
    • 将 "gitlab_workhorse['listen_network'] = "unix"更改为 gitlab_workhorse['listen_network'] = "tcp"
    • 将 gitlab_workhorse['listen_addr'] = "/var/opt/gitlab/gitlab-workhorse/socket"改为 gitlab_workhorse['listen_addr'] = "127.0.0.1:8181"
    • 将“unicorn['port'] = 8080”改为“unicorn['port'] = 8082”

    第三步,重新启动

    1. gitlab

    sudo gitlab-ctl reconfigure
    

    2. apache

    首先确保apache开启了vhost。
    将gitlab.conf放入/etc/httpd/conf.d/或/etc/httpd/site-available/下并建立软连接到/etc/httpd/site-enable。

    service httpd restart
    

    四、以管理员身份登录gitlab

    如果以上都顺利,可以按照之前自定义的url(如gitlab.example.com)进行访问了。
    如果看到登录画面,祝贺你,一切都很顺利。
    如果看到错误画面,别灰心,去看看错误日志。我遇到的问题基本都和gitlab-workhorse有关。再打开gitlab.rb,仔细按照上面的gitlab配置逐一检查。

    1.root登录gitlab

    因为我从安装gitlab到真正能访问gitlab花的时间超出了24小时,所以我的root密码没办法登录。此时需要重置root密码才行。

    sudo gitlab-rake "gitlab:password:reset[root]"
    

    之后就能得到一个新的密码。
    如果以后需要重置其他用户的密码,也可以用这个方法。
    更多详情,请访问这里:重置用户的密码 | GitLab

    2.后续操作

    安装和配置的部分已经写完了。后面的部分,就是配置管理工作了。详见:使用极狐GitLab | GitLab

    相关文章

      网友评论

          本文标题:在centos下如何用apache运行gitlab

          本文链接:https://www.haomeiwen.com/subject/iqimlrtx.html