Building a Git server on Raspberry Pi 4

Written by:

Before I begin, I’m going to need some tools. I could go and buy these ‘as a service’ but I’d rather build it myself because I’m a geek and that’s what I do. First up, I need a git server. Now, I could put this on my K8s cluster but to be honest, I don’t boot that cluster up very often. Instead, I’m going to use my new Raspberry Pi 4 as a development machine.

I’ve decided to use Gitea because it’s a light-weight, nicely rounded piece of software which gives me an almost GitHub experience without the cost of GitHub Enterprise. Here’s how to set it up.

I’m assuming here that you’ve booted up, apt updated all the things and that you’re setup and comfortable in working with your RP4 and in particular on the command line.

Gitea can use either MySQL or Postgres for it’s database requirements. I’ve chosen Postgres because for some reason I’m biased towards it. Let’s install it:

sudo apt install postgresql libpq-dev postgresql-client postgresql-client-common -y

That should run through various pieces of output and give you a running postgresql server. We now need to create some users for this machine. We do need to do some tweaking however as postgres by default uses md5 which is regarded these days as insecure. We need to switch to scram-sha-256 to store our password hashes so let’s change that:

vim /etc/postgresql/11/main/postgresql.conf

Yes. I use vim. Don’t hate me for it, I grew up with it.

In that file look for the hashed out line that reads:

password_encryption=md5

and replace it with:

password_encryption = scram-sha-256

Save and exit. We now also need to make sure that our users are going to be able to access the databases. We can cheat a bit here because we know we are going to call our gitea user ‘gitea’ and the database ‘giteadata’.

Open up the pg_hba.conf file:

vim /etc/postgresql/11/main/pg_hba.conf

Find the line that reads:

#TYPE DATABASE USER ADDRESS METHOD

and directly below enter the following two lines:

local giteadb gitea scram-sha-256
local all pi scram-sha-256

This isn’t security best practice by any stretch but this is my home machine and I want ease of use. Once you’ve saved, restart postgres:

/etc/init.d/postgres restart && /etc/init.d/postgres status

That should restart and print out a status which should tell you postgres is up and running. Now that we have the server configured, let’s create those users and that database.

Switch to the postgres user:

sudo su postgres

Now I’m going to create the standard pi user and a superuser in postgres:

createuser pi -P --interactive

That’s going to ask you some questions. I chose to make this a superuser as I said, it’s a private machine and I like living dangerously.

Next I’ll create a user for gitea to use called gitea. Because I’m original like that and it’s what we configured the server to allow connections for.

createuser gitea -P --interactive

That’s the user side of things sorted out, and it’s time to create the database. Let’s login to the database console as the superuser:

su -c "psql" - postgres

In there enter the following:

CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';

That should all be on one line btw.

Now we have our database setup we need to setup Gitea itself. First let’s create user that Gitea will run under:

sudo adduser --disabled-login --gecos 'Gitea' git

We use the –disabled-login because we don’t want anyone logging into the box as gitea – there’s no need.

Switch to this user:

sudo su git

Create a directory for gitea to sit in:

cd ~ && mkdir gitea

That command couplet puts you in the git user home directory and creates the gitea directory off that. We now need to get the latest and greatest gitea software:

cd gitea && wget https://dl.gitea.io/gitea/1.11.4/gitea-1.11.4-linux-arm-6 -O gitea

That couplet will change into the gitea directoty and download a version (change if there’s something newer) from the official download site and save it as a file called ‘gitea’. That’s an executable file so we have to tell the system to allow execution:

chmod +x gitea

We also now need to setup a system service to make managing the service state easier. Let’s create a service file:

sudo vim /etc/systemd/system/gitea.service

In that file place the following:

[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target

[Service]
LimitMEMLOCK=infinity
LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/home/git/gitea
ExecStart=/home/git/gitea/gitea web
Restart=always
Environment=USER=git 
HOME=/home/git

[Install]
WantedBy=multi-user.target

Save that file. Let’s make sure the service is now enabled and started:

sudo systemctl enable gitea.service

sudo systemctl start gitea.service

You can check the status of the service with:

sudo systemctl status gitea.service

That, as they say, is it! Your Gitea service should be up and running on your machine’s IP on port 3000. So got to the URL https://XXX.XXX.XXX/install and follow the onscreen instructions.

Hope this helps. Next up, Jenkins!

2 responses to “Building a Git server on Raspberry Pi 4”

  1. NodeJS on Raspberry Pi 4 – Khushil’s Thoughts Avatar
    NodeJS on Raspberry Pi 4 – Khushil’s Thoughts

    […] my previous posts I detailed by Gitea and Jenkins setup. We’re now ready to get NodeJS setup. First we’ll get nvm setup. This […]

    Like

  2. Jenkins on Raspberry Pi 4 – Khushil’s Thoughts Avatar
    Jenkins on Raspberry Pi 4 – Khushil’s Thoughts

    […] Previous […]

    Like

Latest Articles