Use Apache instead of built-in Nginx in GitLab CE

Screenshot of GitLab repository.
A relevant photo. Don't get used to it.

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.

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

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

Reconfigure GitLab

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

$ gitlab-ctl reconfigure

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

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.

Featured image by Pankaj Patel.


Comments (18)

Previously posted in WordPress and transferred to Ghost.

glp
January 13, 2016 at 4:01 pm

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

Victor Irzak
April 3, 2016 at 9:14 am

Also make sure the directory has permissions:

Require all granted

Otherwise this will results in bunch of 403s

KyferEz
July 10, 2016 at 11:01 am

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?

KyferEz
July 10, 2016 at 11:27 am

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.

Joao
August 18, 2016 at 7:31 pm

I’m having the same problem, were you able to solve it?

Pedro Paulo Palazzo
November 17, 2016 at 5:28 am

I got this to work on Ubunto 16-LTS by additionally following the instructions at https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md#using-a-non-bundled-web-server and using the .conf file found at https://gitlab.com/gitlab-org/gitlab-recipes/blob/master/web-server/apache/gitlab-apache24.conf, and manually enabling the mods listed on this latter file.

Pedro Paulo Palazzo
November 17, 2016 at 5:29 am

Moving from Nginx to Apache also solved the problem of not being able to use static files such as users’ avatars, by the way.

rolling sky
July 3, 2017 at 9:08 pm

By default, Nginx is configured to start automatically at server startup. If you do not want so so, you can disable the configuration with using:
$ sudo systemctl disable nginx
To enable this service to boot your computer at startup, you can type:
$ sudo systemctl allows nginx

Irwuin
October 13, 2017 at 2:57 pm

Thank you Bro it’s work properly.
but how can i set this up to work with HTTPS ?
any idea ?

Ryan Sechrest
October 31, 2017 at 10:12 am

Have you tried listening on port 443 in the Apache config?

Obella Isaac
November 13, 2018 at 1:43 am

Hi, the gitlab css doesn’t load is there a way to fix this

Niall Deakin
November 30, 2018 at 8:54 am

I’ve followed every step, and even had to do a lot of messing around in order to get this right (multiple vhosts on the server), but I’m getting a 403 Forbidden.

I’ve tried using Directory and Require all granted, but that just gives me “Index of”.

Any idea?

LB
September 29, 2019 at 8:32 am

solution for css not loading :
https://forum.gitlab.com/t/no-css-gitlab-not-working/21249/2