Installing Git & Gitea

You are here:
< All Topics

This article explains how to configure Git and the web-based interface to Git called Gitea.

 

 

Install Git with the following command:

 

 

$ sudo apt install git -y

 

 

Check your Git version:

 

kevin@gemini:~$ git –version
git version 2.25.1

 

Set your user account:

 

git config –global user.name “Your Name”

git config –global user.name “kevin”

git config –global user.email “***@gmail.com”

 

root@gemini:/home/kevin# git config –global user.name “kevin”
root@gemini:/home/kevin# git config –global user.email “***@gmail.com”

This information is then stored in your .gitconfig file in your home directory.

 

Verify with:

 

root@gemini:/home/kevin# git config –list
user.name=kevin
user.email=***@gmail.com
root@gemini:/home/kevin#

 

 

Gitea is the web-based Git interface similar to GitLab. Instructions below for installing on Ubuntu systems.

 

You first need Git installed and configured on your machine before installing Gitea (see above).

 

 

Installing and Configuring MySQL for Gitea

 

Gitea works with MySQL, PostgreSQL, SQLite3 and MSSQL databases. For this example, Gitea is configured to use MySQL database.

 

Install MySQL on Ubuntu 20.04 LTS machine using:

 

$ sudo apt install mysql-server mysql-client -y

 

Then login to the MySQL console as root:

 

$ sudo mysql -u root -p

 

Type in your MySQL root password and press <Enter>.

 

By default no password is set for root user (you don’t actually need a password but it is still advisable to set one).

 

root@gemini:/home/kevin# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10287
Server version: 10.3.29-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]>

 

 

Next create a new MySQL user “gitea” with password “secret”:

 

mysql> CREATE USER ‘gitea’ IDENTIFIED BY ‘secret’;

 

Then create a gitea database for Gitea:

 

mysql> CREATE DATABASE gitea CHARACTER SET ‘utf8mb4’ COLLATE ‘utf8mb4_unicode_ci’;

 

Allow the gitea user full access to the gitea database:

 

mysql> GRANT ALL PRIVILEGES ON gitea.* TO ‘gitea’;

 

Finally, so that the changes will take effect, do:

 

mysql> FLUSH PRIVILEGES;

 

and exit the MySQL shell:

 

mysql> exit

 

root@gemini:/home/kevin# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10287
Server version: 10.3.29-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> CREATE USER ‘gitea’ IDENTIFIED BY ‘secret’;
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> CREATE DATABASE gitea CHARACTER SET ‘utf8mb4’ COLLATE ‘utf8mb4_unicode_ci’;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON gitea.* TO ‘gitea’;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]>

MariaDB [(none)]> exit
Bye
root@gemini:/home/kevin#

 

 

Installing and Configuring Gitea

 

Download the Gitea binary from the official Gitea website:

 

LATEST VERSION as at 29.5.2021 is gitea-1.14.2-linux-amd64

 

https://dl.gitea.io/gitea/1.14.2/gitea-1.14.2-linux-amd64

 

so we do:

 

wget -O /usr/local/bin/gitea https://dl.gitea.io/gitea/1.14.2/gitea-1.14.2-linux-amd64

 

 

Assign execute permission to the Gitea binary /usr/local/bin/gitea:

 

$ sudo chmod +x /usr/local/bin/gitea

 

 

Verify with:

 

$ gitea –version

 

root@gemini:/home/kevin# chmod +x /usr/local/bin/gitea
root@gemini:/home/kevin# gitea –version
Gitea version 1.14.2 built with GNU Make 4.1, go1.16.4 : bindata, sqlite, sqlite_unlock_notify
root@gemini:/home/kevin#

 

 

Next create a new user git for Gitea:
$ sudo adduser –system –shell /bin/bash –gecos ‘Git Version Control’ –group –disabled-password –home /home/git git

 

 

root@gemini:~# adduser –system –shell /bin/bash –gecos ‘Git Version Control’ –group –disabled-password –home /home/git git
Adding system user `git’ (UID 115) …
Adding new group `git’ (GID 123) …
Adding new user `git’ (UID 115) with group `git’ …
Creating home directory `/home/git’ …
root@gemini:~#

 

 

This means the git repositories will be stored in the HOME directory of the git user at /home/git.

 

Next create the required directories for Gitea:

 

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

 

 

root@gemini:~# mkdir -pv /var/lib/gitea/{custom,data,log}
mkdir: created directory ‘/var/lib/gitea’
mkdir: created directory ‘/var/lib/gitea/custom’
mkdir: created directory ‘/var/lib/gitea/data’
mkdir: created directory ‘/var/lib/gitea/log’
root@gemini:~#

 

and change the user and group of these directories to git:

chown -Rv git:git /var/lib/gitea

 

root@gemini:~# chown -Rv git:git /var/lib/gitea
changed ownership of ‘/var/lib/gitea/data’ from root:root to git:git
changed ownership of ‘/var/lib/gitea/custom’ from root:root to git:git
changed ownership of ‘/var/lib/gitea/log’ from root:root to git:git
changed ownership of ‘/var/lib/gitea’ from root:root to git:git
root@gemini:~#

 

Then set permissions:

 

chmod -Rv 750 /var/lib/gitea

 

 

root@gemini:~# chmod -Rv 750 /var/lib/gitea
mode of ‘/var/lib/gitea’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
mode of ‘/var/lib/gitea/data’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
mode of ‘/var/lib/gitea/custom’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
mode of ‘/var/lib/gitea/log’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
root@gemini:~#

 

 

Then create a Gitea configuration directory /etc/gitea/

 

mkdir -v /etc/gitea

 

root@gemini:~# mkdir -v /etc/gitea
mkdir: created directory ‘/etc/gitea’
root@gemini:~#

 

 

and change user to root and group to git and set permissions for the Gitea configuration directory /etc/gitea/

 

root@gemini:~# chown -Rv root:git /etc/gitea
changed ownership of ‘/etc/gitea’ from root:root to root:git
root@gemini:~#

 

root@gemini:~# chmod -Rv 770 /etc/gitea
mode of ‘/etc/gitea’ changed from 0755 (rwxr-xr-x) to 0770 (rwxrwx—)
root@gemini:~#

 

 

Next create the systemd services file for Gitea:

 

create service file gitea.service for Gitea in /etc/systemd/system/ directory.

 

nano /etc/systemd/system/gitea.service

 

 

Copy paste the following into the gitea.service file:

 

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

 

[Service]
LimitMEMLOCK=infinity
LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web –config /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE

 

[Install]
WantedBy=multi-user.target

 

 

Then start the gitea service:

 

root@gemini:~#
root@gemini:~# systemctl start gitea
root@gemini:~#

root@gemini:~# systemctl enable gitea
Created symlink /etc/systemd/system/multi-user.target.wants/gitea.service → /etc/systemd/system/gitea.service.
root@gemini:~# systemctl status gitea
● gitea.service – Gitea (Git with a cup of tea)
Loaded: loaded (/etc/systemd/system/gitea.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-05-31 12:04:34 BST; 11s ago
Main PID: 269121 (gitea)
Tasks: 9 (limit: 2280)
Memory: 131.1M
CGroup: /system.slice/gitea.service
└─269121 /usr/local/bin/gitea web –config /etc/gitea/app.ini

May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 …dules/setting/git.go:101:newGit() [I] Git Version: 2.25.1, Wire Protocol Version 2 Enabled
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:93:PreInstallInit() [T] AppPath: /usr/local/bin/gitea
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:94:PreInstallInit() [T] AppWorkPath: /var/lib/gitea
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:95:PreInstallInit() [T] Custom path: /var/lib/gitea/custom
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:96:PreInstallInit() [T] Log path: /var/lib/gitea/log
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:97:PreInstallInit() [T] Preparing to run install page
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:100:PreInstallInit() [I] SQLite3 Supported
May 31 12:04:35 gemini gitea[269121]: 2021/05/31 12:04:35 cmd/web.go:189:listen() [I] Listen: http://0.0.0.0:3000
May 31 12:04:35 gemini gitea[269121]: 2021/05/31 12:04:35 …s/graceful/server.go:55:NewServer() [I] Starting new Web server: tcp:0.0.0.0:3000 on PID: 269121
May 31 12:04:35 gemini gitea[269121]: 2021/05/31 12:04:35 …s/graceful/server.go:66:func1() [D] Starting server on tcp:0.0.0.0:3000 (PID: 269121)
root@gemini:~#

 

 

 

First Initial Configuration of Gitea

 

Next you have to configure Gitea from the web browser

 

Point your web browser at the IP of the Gitea server (Gitea uses port 3000 by default):

 

http://kevwells.com:3000/

 

BUT – we need to switch this to https! else security risk, so:

 

Switching Gitea from http to https

 

To use Gitea’s built-in HTTPS support, you must change your /etc/gitea/app.ini file:

 

[server]
PROTOCOL = https
ROOT_URL = https://git.example.com:3000/
HTTP_PORT = 3000
CERT_FILE = cert.pem
KEY_FILE = key.pem

 

Have to give the full path for the .pem files:

 

 

[server]
PROTOCOL = https
SSH_DOMAIN = localhost
DOMAIN = localhost
HTTP_PORT = 3000
ROOT_URL = https://localhost:3000/
DISABLE_SSH = false
SSH_PORT = 22
LFS_START_SERVER = true
LFS_CONTENT_PATH = /var/lib/gitea/data/lfs
LFS_JWT_SECRET = xxxxxx (blocked out here)
OFFLINE_MODE = false
CERT_FILE = /etc/letsencrypt/live/kevwells.com/cert.pem
KEY_FILE = /etc/letsencrypt/live/kevwells.com/key.pem

 

 

 

then restart gitea server.

 

 

IMPORTANT – SECURITY!

 

to prevent general public from registering user accounts and using your Git server, you need to set following in the [server] and [service] sections of the /etc/gitea/app.ini file: (ie TWO entries)

 

set them both to:

 

DISABLE_REGISTRATION = true

 

 

 

then restart gitea server. Make sure you restart, else the change does not take effect!

 

The register link on the top right hand start page of gitea should now be gone.

 

Also be sure to switch your gitea server to use https instead of http. See above for configuration procedure.

 

 

 

 

 

 

 

Disclosure: The above article may include affiliate links for products and services for which this site may receive remuneration.
Tags:
Table of Contents