将GitLab 7.2.1与Apache服务器配合使用,而不是Nginx。

17

我已经使用GitLab.org提供的适用于Debian 7的.deb软件包,在具有root访问权限的虚拟服务器上安装了GitLab 7.2.1

在这个虚拟服务器上,我已经安装了Apache版本为2.2.22 ,我不想使用Nginx来运行GitLab。

现在,我不知道GitLab的公共文件夹在哪里,也不知道该做什么或注意什么事项。

那么我的问题是:如何为Apache配置vhost,或者我需要做什么才能在我的Apache Web服务器上使用子域名"gitlab.example.com"?

8个回答

22

有两点需要注意:

  1. Unicorn正在监听8080端口(您可以使用sudo netstat -pant | grep unicorn进行检查)
  2. 您的文档根目录是/opt/gitlab/embedded/service/gitlab-rails/public

您可以使用以下配置在Apache中为GitLab创建一个新的虚拟主机:

<VirtualHost *:80>
  ServerName gitlab.example.com
  ServerSignature Off

  ProxyPreserveHost On

  <Location />
    Order deny,allow
    Allow from all

    ProxyPassReverse http://127.0.0.1:8080
    ProxyPassReverse http://gitlab.example.com/
  </Location>

  RewriteEngine on
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule .* http://127.0.0.1:8080%{REQUEST_URI} [P,QSA]

  # needed for downloading attachments
  DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public

</VirtualHost>

2
你可能还需要使用sudo a2enmod proxy_http命令。 - xtian
5
如果您遇到“您没有权限访问此服务器上的/assets/logo-white-0b53cd4ea06811d79b3acd486384e047.png”这样的权限错误,则需要在<Location>中添加“Require all granted”。请注意,翻译不会更改原文意思,只会使其更通俗易懂。 - xtian
2
我的独角兽进程名称是config.ru,所以我需要使用netstat -pant | grep 8080。 - themadmax
2
你如何让gitlab-ce不启动nginx?我升级了gitlab,它使用nginx劫持了80端口,导致我的现有Apache实例无法启动。gitlab真的不想与现有服务共享服务器。 - arclight
2
@arclight 我知道这已经晚了很多,但我也曾被卡在同样的问题上一整天。你需要在 gitlab 的设置中禁用它,即 gitlab.rb 文件。我试了几次,定位到 nginx['enable'] 这一行,将其设置为 false 并删除前面的井号即可。 - Mark Carpenter Jr
显示剩余9条评论

12
我按照这篇文章http://eserdeniz.fr/articles/view/4/installer-gitlab-sous-debian-7-avec-nginx-et-mysql安装了GitLab,但是我需要使用Apache而不是Nginx。
在配置apache2和gitlab-ce 7.9.0.rc3时遇到了很多麻烦,后来我查看了apache文档,关于ProxyPass和ProxyPassReverse指令的内容。
我通过以下虚拟主机解决了我的问题:
    <VirtualHost *:80>
            ServerName gitlab.me

            # those options below are recommanded by apache, dealing with the simple Proxy we need for gitlab
            ProxyRequests Off
            ProxyPreserveHost On
            AllowEncodedSlashes NoDecode

            # here we don't want to proxify the requests for the existing assets in gitlab's public directory
            ProxyPassMatch ^(/[^/]+\.(html|png|ico|css|txt))$ !
            ProxyPass /assets !

            # here we "redirect" the requests for http://gitlab.me/ to http://127.0.0.1:8080/
            ProxyPass / http://127.0.0.1:8080/

            # here we "rewrite" the redirections form unicorn for http://127.0.0.1:8080/ into http://gitlab.me/
            ProxyPassReverse / http://127.0.0.1:8080/

            # And of course the DocumentRoot to handle the assets requests
            DocumentRoot /home/git/gitlab/public/

            # In the last versions of apache, there is a deny,allow default order so we put those two sections to prevent 'client denied by server configuration' 403 error

            <Directory /home/git/gitlab/public/>
                    # apache 2.2
                    Order allow,deny
                    Allow from all

                    # apache 2.4
                    Require all granted
            </Directory>

            <Location />
                    # apache 2.2
                    Order allow,deny
                    Allow from all

                    # apache 2.4
                    Require all granted
            </Location>
    </VirtualHost>

现在速度飞快!!
希望这会有所帮助!

3
在Debian GNU/Linux 8.4 (jessie)的操作系统上,使用Omnibus 8.5.0 (apt-get)版本:

GitLab配置

# cat /etc/gitlab/gitlab.rb | grep -v '^$\|^\s*\#'
external_url 'http://gitlab.example.fr'
gitlab_workhorse['enable'] = true
gitlab_workhorse['listen_network'] = "tcp"
gitlab_workhorse['listen_addr'] = "127.0.0.1:8181"
web_server['external_users'] = ['www-data']
nginx['enable'] = false

Apache2 配置

# cat /etc/apache2/sites-enabled/gitlab.conf  | grep -v '^$\|^\s*\#'
<VirtualHost *:80>
  ServerName gitlab.example.fr
  ServerSignature Off
  ProxyPreserveHost On
  AllowEncodedSlashes NoDecode
  <Location />
    Require all granted
    ProxyPassReverse http://127.0.0.1:8181
    ProxyPassReverse http://gitlab.example.fr/
  </Location>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} ^/api/v3/.*
  RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_URI} ^/uploads/.*
  RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA]
  DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
  ErrorDocument 404 /404.html
  ErrorDocument 422 /422.html
  ErrorDocument 500 /500.html
  ErrorDocument 503 /deploy.html
  LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
  ErrorLog  /var/log/apache2/gitlab_error.log
  CustomLog /var/log/apache2/gitlab_forwarded.log common_forwarded
  CustomLog /var/log/apache2/gitlab_access.log combined env=!dontlog
  CustomLog /var/log/apache2/gitlab.log combined
</VirtualHost>

Netstat输出

# netstat -pant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      11849/postgres
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      23736/config.ru
tcp        0      0 127.0.0.1:8181          0.0.0.0:*               LISTEN      26061/gitlab-workho

Source

https://gitlab.com/gitlab-org/gitlab-recipes/blob/master/web-server/apache/gitlab-omnibus-apache24.conf

http://doc.gitlab.com/omnibus/settings/nginx.html#using-a-non-bundled-web-server


2
如果您遇到HTTP git访问问题,请查看此配置:
# cat /etc/gitlab/gitlab.rb | grep -v '^$\|^\s*\#'
external_url 'http://gitlab.example.fr'
web_server['external_users'] = ['www-data']
nginx['enable'] = false
ci_nginx['enable'] = false
gitlab_git_http_server['listen_network'] = "tcp"
gitlab_git_http_server['listen_addr'] = "localhost:8282"

以及apache2的配置:

# cat /etc/apache2/sites-enabled/gitlab
<VirtualHost *:80>
        ServerName gitlab.example.fr
        ProxyRequests Off
        ServerSignature Off
        ProxyPreserveHost On
        AllowEncodedSlashes NoDecode
        ProxyPassMatch ^(/[^/]+\.(html|png|ico|css|txt))$ !
        ProxyPass /assets !
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/
        RewriteEngine on
        RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
        RewriteRule /[-\/\w\.]+\.git\/ http://127.0.0.1:8282%{REQUEST_URI} [P,QSA,L]
        DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
            <Directory /opt/gitlab/embedded/service/gitlab-rails/public/>
                    Order allow,deny
                    Allow from all
            </Directory>
            <Location />
                    Order allow,deny
                    Allow from all
            </Location>
</VirtualHost>

应用更改:

# gitlab-ctl reconfigure
# service apache2 reload

来自https://gitlab.com/gitlab-org/gitlab-ce/issues/2669#note_2176671


2

如果您使用的是GitLab 10.X.X版本,请查看此存储库。您可以在那里找到Apache2配置文件以及运行仅使用Apache2而禁用NGINX的GitLab的说明。


1

从源代码进行安装。gitlab 7.4.5

Unicorn正在监听9095端口。Apache版本为2.2.9,我没有使用https。

System information
System:         CentOS 6.7
Current User:   git
Using RVM:      no
Ruby Version:   2.1.2p95
Gem Version:    2.2.2
Bundler Version:1.11.2
Rake Version:   10.3.2
Sidekiq Version:2.17.0

GitLab information
Version:        7.4.5
Revision:       19d572e
Directory:      /home/git/gitlab
DB Adapter:     mysql2
URL:            http://gitlab.example.com
HTTP Clone URL: http://gitlab.example.com/some-project.git
SSH Clone URL:  git@gitlab.example.com:some-project.git
Using LDAP:     no
Using Omniauth: no

GitLab Shell
Version:        2.0.1
Repositories:   /home/git/repositories/
Hooks:          /home/git/gitlab-shell/hooks/
Git:            /usr/bin/git

修改apache 2.2的配置文件对我有用。

另一个适用于gitlab 6.0的旧配置文件在这里,对我也有效。

#This configuration has been tested on GitLab 8.0.0
#Note this config assumes unicorn is listening on default port 8080 and gitlab-git-http-server is listening on port 8181.
#To allow gitlab-git-http-server to listen on port 8181, edit or create /etc/default/gitlab and change or add the following:
#gitlab_git_http_server_options="-listenUmask 0 -listenNetwork tcp -listenAddr localhost:8181 -authBackend http://127.0.0.1:8080"

#Module dependencies
#  mod_rewrite
#  mod_proxy
#  mod_proxy_http
# HTTP Configuration
<VirtualHost *:80>
  ServerName gitlab.example.com
  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

    # 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 />
        #Require all granted
        Order deny,allow
        Allow from all

        #Allow forwarding to gitlab-git-http-server
        #ProxyPassReverse http://127.0.0.1:8181
        #Allow forwarding to GitLab Rails app (Unicorn)
        ProxyPassReverse http://127.0.0.1:9095
        ProxyPassReverse http://gitlab.example.com/
    </Location>

  #apache equivalent of nginx try files
  # http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
  # https://dev59.com/YWgu5IYBdhLWcg3w9bn_
  RewriteEngine on
  #Forward these requests to gitlab-git-http-server
  #Forward these requests to gitlab-git-http-server
  #RewriteCond %{REQUEST_URI} ^/[\w\.-]+/[\w\.-]+/repository/archive.* [OR]
  #RewriteCond %{REQUEST_URI} ^/api/v3/projects/.*/repository/archive.* [OR]
  #RewriteCond %{REQUEST_URI} ^/[\w\.-]+/[\w\.-]+/(info/refs|git-upload-pack|git-receive-pack)$
  #RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA]

  #Forward any other requests to GitLab Rails app (Unicorn)
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_URI} ^/uploads
  RewriteRule .* http://127.0.0.1:9095%{REQUEST_URI} [P,QSA,NE]

  # needed for downloading attachments
  DocumentRoot /home/git/gitlab/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 503 /deploy.html

  LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
  ErrorLog  logs/gitlab.example.com_error.log
  CustomLog logs/gitlab.example.com_forwarded.log common_forwarded
  CustomLog logs/gitlab.example.com_access.log combined env=!dontlog
  CustomLog logs/gitlab.example.com.log combined

</VirtualHost>

希望对那些使用旧版GitLab源码安装的人有所帮助。


非常感谢您提供的最有用的链接。这些链接帮助我编写了Apache重写规则,使其能够正确地代理到Unicorn和Workhorse。 - Eldamir

1
我刚花了半天时间弄清楚为什么GitLab在gitlab-rails production.log的日志中会给我报422错误并抱怨CSRF令牌。
结果发现我需要将以下内容添加到Apache配置文件中:
RequestHeader set X-Forwarded-Ssl on

在我的情况下,GitLab是通过deb包安装的,而Apache正在运行HTTPS。

我必须添加 RequestHeader set X_FORWARDED_PROTO 'https' - Rohlik

0

借助 @pincoded 的答案,我成功地启动了 GitLab,但是在推送更改时,我总是收到 500 错误。

然后我使用了 GitLab 在 @themadmax 的答案中提供的官方 Apache 配置。问题在于服务器从未被访问,并且经过一段时间后产生了 502 错误。

我的解决方案: 使用 GitLab 的 官方解决方案(请注意,在此链接中选择 SSL 配置,如果您只运行 GitLab SSL),但是按照 这个论坛帖子 的建议,我必须再次启用 nginx。

因此,最终我的配置如下:

vhost:

<VirtualHost *:80>
  ServerName YOUR_SERVER_FQDN
  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 http://YOUR_SERVER_FQDN/
  </Location>

  # Apache equivalent of nginx try files
  # http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
  # https://dev59.com/YWgu5IYBdhLWcg3w9bn_
  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

</VirtualHost>

gitlab.ru:

# nginx['enable'] = false # this defaults to true
gitlab_workhorse['enable'] = true
gitlab_workhorse['listen_network'] = "tcp"
gitlab_workhorse['listen_addr'] = "127.0.0.1:8181"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接