How To Install and Configure Gitlab on Apache Server in your VPS


GitLab, like GitHub is an online repository that allows developers create, store and manage their projects with the ability to roll back to an earlier state if an error is encountered. To use the online repository, no setup is required, just register an account and start creating projects. We will be using a vps, which gives us the ability to host a private gitlab server and also host other projects at same time. We will be using the DigitalOcean droplet running ubuntu linux image (You are free to use any VPS running any linux image of your choice). DigitalOcean comes with an already configured gitlab droplet but we will be using an empty droplet as this gives us the ability to achieve our aim. The gitlab server uses nginx as its default server, while our projects use apache server to serve contents. Running two internal servers simultaneously can pose a lot of issues (ports … etc). We will be using apache server to run both our gitlab and other projects


First, let's log in to our server:

ssh user@your_server_ip

Step 1:

We will install gitlab on our VPS manually using the following commands:

sudo apt-get install curl openssh-server ca-certificates postfix
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
sudo apt-get install gitlab-ce

Installing gitlab manually on our server gives us the power to switch the default nginx server it was configured to use.


Step 2:

We will configure the url to be used in accessing the gitlab server by editing the gitlab configuration file using this command:

sudo nano /etc/gitlab/gitlab.rb

Edit the configuration file by changing the external url to your desired url: external_url "http://" Save your configuration and run this command to reconfigure your gitlab installation.:

sudo gitlab-ctl reconfigure

Your gitlab installation should be working fine, visit the url specified to access your gitlab and login using username root and password 5iveL!fe


Step 3:

Now we have a working gitlab server but we want it to run using the apache server so we can host other stuffs (websites, apps etc) on the same droplet. We'll go ahead to install the apache server and configure the gitlab to run from it. Install the apache server using:

sudo apt-get update
sudo apt-get install apache2

sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
sudo service apache2 restart

These will install and start the apache server. To check if the apache server is running use :

sudo systemctl status apache2

which should show active (running).


Step 4:

Let's configure our gitlab server to use the apache server instead of the default nginx server.

sudo nano /etc/gitlab/gitlab.rb

Modify the following to our gitlab configuration file (/etc/gitlab/gitlab.rb):

# Modify external url to match the domain in our apache config file 
external_url "http://<yourdomain>"

# Disable nginx
nginx['enable'] = false

# Give apache user privileges to listen to gitLab
web_server['external_users'] = ['www-data']

Save the file, then go ahead to create the gitlab apache config file in the apache sites-available folder.

# navigate to apache config folder
cd /etc/apache2/sites-available

# create a new config file
touch gitlab.conf

# open config file for editing
sudo nano gitlab.conf

Copy and paste the following to the file:

<VirtualHost *:80>
  ServerName <your_domain_or_sub_domain>
  ServerSignature Off

  ProxyPreserveHost On
  AllowEncodedSlashes NoDecode

  <Location />
  Require all granted

  ProxyPassReverse http://127.0.0.1:8080
  ProxyPassReverse <your_domain_or_sub_domain>
  </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

  #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  /var/log/httpd/logs/<your_domain_or_sub_domain>_error.log
  CustomLog /var/log/httpd/logs/<your_domain_or_sub_domain>_forwarded.log common_forwarded
  CustomLog /var/log/httpd/logs/<your_domain_or_sub_domain>_access.log combined env=!dontlog
  CustomLog /var/log/httpd/logs/<your_domain_or_sub_domain>.log combined

</VirtualHost>

Save the file and enable the gitlab site by enabling the config using:

#Enable gitlab
sudo a2ensite gitlab.conf

#Restart apache
sudo service apache2 restart

#Reconfigure gitlab
sudo gitlab-ctl reconfigure

#Restart gitlab server
sudo gitlab-ctl restart

We are done setting up our gitlab to run from apache server, now check the url again to see a working gitlab.


Gitlab admin login Details:

In case you forgot your default root login details, reset the password using the following steps: Open the gitlab rails console:

**sudo gitlab-rails console** or **sudo gitlab-rake rails console**

Search for the default user and select it:

**user = User.find_by(email: 'admin@local.host')** or **user = User.find(1)**

To change the password, run the commands below:

# create new password
user.password = 'secret_pass'

# confirm password
user.password_confirmation = 'secret_pass'

# save user password
user.save

# quit gitlab console
quit

Navigate to your gitlab url on your browser and log into your gitlab root account using the new password. Ensure you change it once you have access to the gitlab dashboard.


Configure push and pull on server repositories:

After creating gitlab server, new users can register and create projects on the gitlab server. In a situation where no user is allowed to pull or push to any repository, we can fix it by modifying the following lines to our gitlab config (/etc/gitlab/gitlab.rb): Open gitlab file for editing:

sudo nano /etc/gitlab/gitlab.rb

Edit the following lines to reflect the changes below:

.......
gitlab_workhorse['enable'] = true
gitlab_workhorse['listen_network'] = "tcp"
gitlab_workhorse['listen_addr'] = "localhost:8282"
.......

After saving the config file, reconfigure the gitlab server using:

sudo gitlab-ctl reconfigure

Modify gitlab config file to reflect update:

# open config file for editing
sudo nano gitlab.conf

Add this rewrite rule just after the RewriteEngine On:

......
RewriteEngine On
# points our git url to the gitlab workhorse proxy server
RewriteRule /[-\/\w\.]+\.git\/ http://127.0.0.1:8282%{REQUEST_URI} [P,QSA,L]
.......

We'll save our config file and restart our apache server:

sudo service apache2 restart

Let's try pulling and pushing again. It should work perfectly now. We have fully setup our Gitlab server that can allow us pull and push data to our repositories.

Let's proceed to configure https on the new gitlab server

Configure https for gitlab and all gitlab cloning url on apache server

No Comments Yet