Building your own Git Server with Gitea on AlmaLinux

Gitea is a self-hosted git server you can run on your Linux rig, enabling you to be in full control of hosting your own repositories. In this video, the process of setting up Gitea is shown on an AlmaLinux server. You’ll see the process of setting up the server, installing Gitea, and even making your first commit to a repository that’s being hosted on your new server.

YouTube player

Continue reading for an overview of the commands that were used in this video.

What you’ll need

  • Linux server (AlmaLinux used as an example)
  • 1GB of RAM minimum (but the more you have, the better)
  • Domain (not required, but strongly recommended)

Some things to know

Before we get started, there’s a few things to know about this project. First, the video utilizes an instance available on Linode to provide the required AlmaLinux server. Linode isn’t required, you can follow this same process on a VirtualBox VM, baremetal hardware, or whatever you have. Check out this video for instructions on setting up AlmaLinux manually if you’re not using Linode. If you do choose to use Linode, there will be a monthly fee associated with the instance as displayed on the dashboard. If you’re utilizing the LearnLinuxTV free credit link, the instance will start billing after the credit is used up or it expires, whichever comes first.

Note: Linode is a sponsor of LearnLinuxTV.

Also, the video utilizes a domain. While a domain is not required, it’s highly recommended. If you do use a domain, point that domain (or a subdomain) to the IP address of the server you’re installing Gitea on. Give DNS some time to propagate before starting.

Finally, the video has you create a normal non-root user account, but it’s not used until much later in the video. Be sure to prefix commands with sudo as required if you log in as a normal user.

Preparing the server

First, we’ll configure some miscellaneous details and config values for our new server. We’ll start with the hostname, which we’ll update to the chosen name for our server. You can use a simple hostname, or a domain if you have one (recommended). Updating the hostname will require updating two files.

Edit the /etc/hostname file:

nano /etc/hostname

Update the hostname to match the intended name for the server. If you have a domain, you should use that. Otherwise, at least give the server a name. For example, in the video the following fully qualified domain name was used:

git.learnlinux.cloud

If you don’t have a domain, you can give it a simpler hostname:

git

Edit the /etc/hosts file

Next, edit the /etc/hosts file to reflect the new name.

nano /etc/hosts

Add a new line that includes the hostname of the server, matching what was placed in the /etc/hostname file. This should look similar to this:

127.0.1.1 git.learnlinux.cloud

Update all packages

Be sure to update the server so that all of its packages are up to date:

dnf update

This might take a while to complete. Once it’s done, reboot the server:

reboot

After a few minutes or so, the server will be back online and you can then reconnect to continue.

Create users

We’ll need to create a user on our server to support Gitea, and also a user for ourselves in order to avoid using root going forward.

Creating a non-root user:

Note: Replace <username> with whatever you want your username to be.

Create your user:

useradd -m <username>

Set a password for your user:

passwd <username>

Add your user to the wheel group so it will have access to sudo:

usermod -aG wheel jay

Create a system user for Gitea:

useradd -rms /bin/bash gitea

Ensure git is installed

We’ll need to have access to the git package, so let’s make sure that it’s installed. To find out, run the following command:

which git

If there’s output (it should show a path to the git binary) then that means you have git installed already. If not, install it:

dnf install git

Install SQLite

We’ll also need SQLite, so make sure to install that as well:

dnf install sqlite

Setting up Gitea

Downloading the binary

We’ll need to download Gitea to our server. To fetch the binary, we’ll use the wget. Let’s make sure wget is installed:

which wget

Install wget if it’s not installed already:

dnf install wget

Grab the download link for the latest version of Gitea. We’ll at least need the URL to the downloadable binary. The documentation page not only gives us the URL to the binary, but also the entire syntax of the wget command we can use to download it. Visit the documentation page here and look for the wget command example. It should look something like this:

wget -O gitea https://dl.gitea.io/gitea/<version>/gitea-<version>-linux-amd64

Once downloaded, mark the binary as executable:

chmod +x  gitea

Next, move the binary to a more appropriate location:

mv gitea /usr/local/bin

Create required directories:

Use the following commands to create the directories that are required for Gitea:

mkdir -p /var/lib/gitea/{custom,data,log}
mkdir /etc/gitea

Update permissions and ownership for those directories:

chmod -R 750 /var/lib/gitea
chown -R gitea: /var/lib/gitea

chown root:gitea /etc/gitea
chmod 770 /etc/gitea

Adding a systemd unit file for Gitea

Click here to view a sample systemd unit file. Click on the “raw” button, so that it’s easier to copy. Paste the contents into this file:

nano /etc/systemd/system/gitea.service

Below is the current version of the sample unit file from the above link. Check the highlighted items, those are things that might be different from one install to another. Be sure that the information in your systemd unit file matches your actual environment. Also, check the below systemd unit file sample against the linked version above, in case it’s changed by the developers or perhaps new features were added.

Note: Comments were removed from the following version of the file to save space. I recommend copying the version from the above link instead, and then compare the highlighted values with yours.

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

[Service]
RestartSec=2s
Type=simple
User=gitea
Group=gitea
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=gitea HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea

[Install]
WantedBy=multi-user.target

Disable SELinux

Gitea will not run with SELinux enabled, or at least, not by default. The commands below will disable SELinux:

nano /etc/selinux/config

Change SELINUX=enforcing to SELINUX=permissive

That change will only apply once the server is rebooted, but we can run the following to get SELinux out of the way for now:

setenforce 0

Note: Do not leave SELinux disabled forever. SELinux was disabled in the video, but only because a full walkthrough of SELinux is not within scope. On your end, after you get Gitea working, it’s highly recommended to research creating a custom profile for SELinux specific to Gitea, so you can have Gitea enabled.

Start Gitea

Start and enable Gitea:

systemctl start gitea
systemctl enable gitea

Check the status:

systemctl status gitea

Note: If there are errors, verify that the information within the systemd unit file is consistent with the actual users and paths on your server.

Allowing access to Gitea through the firewall

By default, the firewall will not allow us to reach Gitea from a browser. To fix that, we’ll need to update the firewall to allow the traffic:

firewall-cmd --permanent --zone=public --add-port=3000/tcp

Next, we’ll reload the firewall:

firewall-cmd --reload

Configure Gitea through your web browser

The final step is to open Gitea within your browser. By default, Gitea runs on port 3000 so be sure to include that. If you have a domain, use that in the address bar to connect:

http://gitea.yourdomain.net:3000

Or, you can use the IP address if you don’t have a domain:

http://xxx.xxx.xxx.xxx:3000

At this point, fill out each of the fields, and take your time. Also be sure to create an administrator account with a unique username and password. Don’t rush, it’s important that you enter the correct details.

Check out the Shop!

Support Linux Learning and get yourself some cool Linux swag!


Support LearnLinuxTV and receive 5% off an LPI exam voucher!