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

Okwu Joscelyn
6 min readAug 28, 2018

--

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://<yourdomain>"

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.

Step 5:

We have successfully installed our gitlab to use the apache server, let’s verify that our server can also hold multiple sites.

Before we create the apache configuration for the new site, We will create the folder to hold the site content:

#Create directory for new site
sudo mkdir -p /var/www/new_site/public_html
#Allow regular users to modify files in the directory
sudo chown -R $USER:$USER /var/www/new_site/public_html
#Allow general read access to our web directory
sudo chmod -R 755 /var/www

Create a new configuration file for the new site by duplicating the default config file /etc/apache2/sites-available/000-default.conf and renaming it to /etc/apache2/sites-available/new_site.conf

# navigate to apache config folder
cd /etc/apache2/sites-available
# copy content of default config to new config file
sudo cp 000-default.conf new_site.conf
# open new config file for editing
sudo nano new_site.conf

then edit the content of the new file to match the conf below:

<VirtualHost *:80>
ServerName <site-domain>
ServerAlias <site-domain>
ServerAdmin <site-admin-email>
DocumentRoot /var/www/new_site/public_html #Folder directory created earlier
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After saving our conf file, We can enable our new site and restart apache to reflect the changes:

# activate new site config
sudo a2ensite new_site.conf
# restart apache server
sudo service apache2 restart

Ensure our DNS (Domain Name Servers) have been configured to point to our droplet IP.

You can now test your URLs in our browser. If you encounter any error, we’ll go through our configurations to ensure everything was well configured.

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

Follow me on twitter to stay updated

https://twitter.com/Joscelyn56

--

--