Installing Gogs on Gentoo

When hosting your own Git server there are several options that you can choose from depending on what you are looking for specifically. Gitolite is ideal for single-user setups, and to a lesser extent multi-user setups, where the configuration is managed by pushing commits to a Git repository to add new repositories and to manage accessibility. While Gitolite does support features like private repositories and namespacing, it is actually quite limited in terms of scalability when it comes to multi-user setups. Another problem is that Gitolite is mostly SSH-oriented, which means that there is no web interface to manage SSH keys, repositories and so on. Therefore, Gitolite is great if you want a minimalistic setup to manage your own Git repositories, maybe those of others.

cgit is actually orthogonal to Gitolite, it does not manage Git repositories, but instead serves a web interface to view Git repositories with in the web browser. However, cgit is only intended to be a simple web interface to browse through repositories with. There is no way to restrict what an individual user can see as there is no form of authentication. Therefore, cgit is a great addition to Gitolite if you want to be able to browse through public repositories.

Gitlab is pretty much on the other side of the spectrum. It is intended for multi-user setups where each user can manage his/her own repositories, SSH keys, and so on together with support for quota, e-mail notifications and many other features. Unfortunately, it does have a very high memory footprint due to the fact that it is written in Ruby.

Meet Gogs, an alternative to Gitlab written in Go, that offers the same kind of functionality but with a much smaller memory footprint. Today, I'll be showing you how to install Gogs on a Gentoo system.

Installation

Since there is no official package for Gogs yet, we have to add an overlay. To simplify the management of overlays, we will want to have layman installed:

emerge -a layman

After you have installed layman, sync the available repositories as follows:

layman -S

Next, add the go-overlay repository that hosts the Gogs package:

layman -a go-overlay

Now that we have added the overlay, we can install Gogs. First make sure to add the go USE-flag to gcc to recompile it with Go support:

echo "sys-devel/gcc go" >> /etc/portage/package.use

Then install Gogs:

emerge -a gogs

Basic configuration

A template directory for the configuration of Gogs should have been installed into /etc/gogs/conf/, copy it to /etc/gogs/custom/conf:

mkdir -p /etc/gogs/custom
cp -r /etc/gogs/conf /etc/gogs/custom/

You can change the title Gogs is supposed to display by changing the following line in /etc/gogs/custom/conf/app.ini:

APP_NAME = Gogs

As Gogs will be hosting its own HTTP server, we have to make sure that it is only listening for connections on localhost. Change the following in /etc/gogs/custom/conf/app.ini:

PROTOCOL               = http
DOMAIN                 = git.example.com
ROOT_URL               = https://git.example.com/
HTTP_ADDR              = 127.0.0.1
HTTP_PORT              = 3000

Next, create the directory where Gogs will host its files:

mkdir -p /var/lib/gogs/data
chown -hR gogs: /var/lib/gogs

Then change the following lines in /etc/gogs/custom/conf/app.ini to point to this directory:

[server]
APP_DATA_PATH = /var/lib/gogs/data

[repository]
ROOT = /var/lib/gogs/repositories

[repository.upload]
TEMP_PATH = /var/lib/gogs/data/tmp/uploads

[release.attachment]
PATH = /var/lib/gogs/data/attachments

[picture]
AVATAR_UPLOAD_PATH = /var/lib/gogs/data/avatars

[attachment]
PATH = /var/lib/gogs/data/attachments

Setting up PostgreSQL

First log in as the postgres user:

su - postgres

Create a database and user for gogs:

createuser -P gogs
createdb -O gogs gogs

Finally, modify /etc/gogs/custom/conf/app.ini accordingly:

[database]
DB_TYPE  = postgres
HOST     = 127.0.0.1:5432
NAME     = gogs
USER     = gogs
PASSWD   = ${password}
SSL_MODE = disable

Setting up ssh

To set up SSH, we first have to create a .ssh directory to host the public keys.

mkdir -m 0700 -p /var/lib/gogs/.ssh
chown -hR gogs: /var/lib/gogs/.ssh

Then we edit /etc/gogs/custom/conf/app.ini to use this directory:

SSH_ROOT_PATH          = /var/lib/gogs/.ssh

Sending e-mails

For Gogs to send e-mails, we have to set it up to use the Postfix e-mail server running at localhost as follows:

[mailer]
ENABLED         = true
SEND_BUFFER_LEN = 100
SUBJECT         = %(APP_NAME)s
HOST            = localhost:25
DISABLE_HELO    =
HELO_HOSTNAME   = mail.example.com
SKIP_VERIFY     = true
FROM            = gogs@example.com
USER            =
PASSWD          =
USE_PLAIN_TEXT  = false

Finalising the configuration

Now that everything has been configured, start up Gogs as follows:

/etc/init.d/gogs start

To connect with Gogs, we will be using SSH to port forward port 3000 to our own port 3000:

ssh -L 3000:localhost:3000 hostname

Finally, connect to localhost:3000 in your browser to perform the setup. Then check /etc/gogs/custom/conf/app.ini and restart Gogs:

/etc/init.d/gogs restart

Public access

Now that Gogs has been set up, make sure to add the service to the list of services to start up when booting the system:

rc-update add gogs default

Then add the following configuration to nginx to set up a proxy to Gogs using both HTTP and HTTPS:

server {
        listen 80;
        listen 443 ssl;
        server_name git.example.com;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        location / {
                proxy_pass http://localhost:3000;
        }
}

Now Gogs should be accessible from https://git.example.com.