Use Apache instead of built-in Nginx in GitLab CE

I installed GitLab CE using the Omnibus package on a new CentOS 6 server. Since I had nothing else on the server at that time, everything GitLab setup and configured was sufficient. Later, however, I wanted to setup additional sites on the server using Apache, but now port 80 was already bound to by GitLab’s built-in Nginx web server.

What I really wanted to do is disable the built-in Nginx server and just use my self-managed Apache installation. Here’s how to do just that.

1. Stop GitLab services

This will shutdown GitLab while we work on it.

$ gitlab-ctl stop
ok: down: logrotate: 0s, normally up
ok: down: postgresql: 1s, normally up
ok: down: redis: 0s, normally up
ok: down: nginx: 0s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 0s, normally up

2. Disable built-in Nginx

If you’re using GitLab out of the box, your GitLab configuration file should be located in /etc/gitlab. Go ahead and edit it.

vi /etc/gitlab/gitlab.rb

Scroll all the way down to the “GitLab Web Server” header. You can jump there by searching for “Web”: hit the forward slash on your keyboard (/), type in Web and hit ENTER.

Uncomment the web server username and group settings, and set both to Apache.

web_server['username'] = 'apache'
web_server['group'] = 'apache'

In the next section, “GitLab Nginx,” uncomment the first line and set it to false.

nginx['enable'] = false

3. Reconfigure GitLab

This will parse your updated GitLab configuration file and disable Nginx.

$ gitlab-ctl reconfigure

4. Add virtual host for GitLab to Apache

Create a new virtual host file for GitLab with the following directives (based on a GitLab recipe). Update git.domain.com to the domain you were using for GitLab.

<VirtualHost *:80>
  ServerName git.domain.com
  DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public
  
  ProxyPreserveHost On
  AllowEncodedSlashes Off
  
  <Location />
    Order deny,allow
    Allow from all
    ProxyPassReverse http://127.0.0.1:8080
    ProxyPassReverse http://git.domain.com/
  </Location>
  
  RewriteEngine on
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule .* http://127.0.0.1:8080%{REQUEST_URI} [P,QSA]
</VirtualHost>

I turned AllowEncodedSlashes off because I couldn’t find any URLs in GitLab that needed this, however, if you have issues with certain URLs not loading, try to set that directive to NoDecode.

AllowEncodedSlashes NoDecode

Note that your version of Apache would need to be 2.2.18 or later— NoDecode didn’t exist before that.

Restart or start Apache once the virtual host file is in place.

service httpd reload

5. Start GitLab services

Since we turned GitLab off earlier, let’s boot it back up.

$ gitlab-ctl start
ok: run: logrotate: (pid 22857) 0s
ok: run: postgresql: (pid 22860) 1s
ok: run: redis: (pid 22868) 0s
ok: run: sidekiq: (pid 22872) 1s
ok: run: unicorn: (pid 22876) 0s

Note that Nginx should not be listed anymore.

If there are other things you’re trying to figure out, I highly suggest GitLab’s README. It’s a great starting point and as tons of answers.

I hope this tutorial helped you get GitLab up and running with Apache. If you have any questions or issues, leave a comment and I’ll reply when I can.

13 thoughts on “Use Apache instead of built-in Nginx in GitLab CE

  1. glp

    Before running ‘gitlab-ctl reconfigure’, first run ‘gitlab-ctl start postgresql’, else there will be an error that postgresql is not running.

    Reply
  2. Pingback: Instalación y configuración Gitlab y Apache | Hunabsys Blog

  3. Victor Irzak

    Also make sure the directory has permissions:

    Require all granted

    Otherwise this will results in bunch of 403s

    Reply
  4. Pingback: [ASK] server - Remove double slash in gitlab address | Some Piece of Information

  5. KyferEz

    I get this error:

    Error executing action 'create' on resource 'user[apache]'

    I do have a user and group apache, and apache runs as apache. Any ideas?

    Reply
  6. KyferEz

    You have to make sure there are no services running under user apache before running the reconfigure part of this tutorial, otherwise it will fail. I had both apache running and php-fpm. Find what processes are running using this command:

    ps -aux | grep apache

    Then stop any service running, run reconfigure, and start the services again:

    gitlab-ctl stop
    service php-fpm stop
    service httpd stop
    gitlab-ctl reconfigure
    service php-fpm start
    service httpd start
    gitlab-ctl start

    Now I’m having trouble with getting a 503 service unavailable when trying to access my Git URL.

    Reply
  7. Pingback: Installing Gitlab | baligena.com

  8. Pingback: Install Gitlab on Apache in Ubuntu | JMJTwin Blog

Leave a Reply

Your email address will not be published. Required fields are marked *