Gitea is the fastest and most painless way of setting up a self-hosted Git service on premise, or in a cloud infrastructure. The Gitea program is written in Go, to be fast, stable and reliable. The software package is distributed in binary format and it is easy to install and configure. Gitea is a cross-platform solution and can be installed on Linux, Windows, Unix, as well and ARM and PowerPC based systems.

We will be performing an installation of Gitea Git service in an Amazon Linux 2 server. The server can sit in any infrastructure provided there is network connectivity. A user performing the installation is expected to have a console or SSH access to the system.

Step 1: Update System

Before you start any installation we recommend updating the system and all installed packages to the latest releases.

sudo yum -y update
sudo yum -y install git

Then you can do a reboot

sudo reboot

In the installation steps we’ll perform the following actions:

  • Setup MariaDB database server
  • Install Gitea git service on Amazon Linux 2
  • Configure and Access Gitea dashboard

Step 2: Setup MariaDB database server

Add MariaDB yum repository:

sudo tee /etc/yum.repos.d/mariadb.repo<<EOF
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

The repository added is for MariaDB 10.5. Update package index after adding the repository file.

sudo yum clean all
sudo yum makecache

Run the commands below to install MariaDB server on Amazon Linux 2 system.

sudo yum install MariaDB-server MariaDB-client

Agree to any installation prompts:

...
Dependencies Resolved

==================================================================================================================================================================
 Package                                       Arch                         Version                                        Repository                        Size
==================================================================================================================================================================
Installing:
 MariaDB-client                                x86_64                       10.5.8-1.el7.centos                            mariadb                           13 M
 MariaDB-compat                                x86_64                       10.5.8-1.el7.centos                            mariadb                          2.2 M
     replacing  mariadb-libs.x86_64 1:5.5.68-1.amzn2
 MariaDB-server                                x86_64                       10.5.8-1.el7.centos                            mariadb                           26 M
Installing for dependencies:
 MariaDB-common                                x86_64                       10.5.8-1.el7.centos                            mariadb                           81 k
 boost-program-options                         x86_64                       1.53.0-27.amzn2.0.3                            amzn2-core                       163 k
 galera-4                                      x86_64                       26.4.6-1.el7.centos                            mariadb                          9.5 M
 ncurses-compat-libs                           x86_64                       6.0-8.20170212.amzn2.1.3                       amzn2-core                       308 k
 perl-Compress-Raw-Bzip2                       x86_64                       2.061-3.amzn2.0.2                              amzn2-core                        32 k
 perl-Compress-Raw-Zlib                        x86_64                       1:2.061-4.amzn2.0.2                            amzn2-core                        58 k
 perl-DBI                                      x86_64                       1.627-4.amzn2.0.2                              amzn2-core                       804 k
 perl-Data-Dumper                              x86_64                       2.145-3.amzn2.0.2                              amzn2-core                        48 k
 perl-IO-Compress                              noarch                       2.061-2.amzn2                                  amzn2-core                       260 k
 perl-Net-Daemon                               noarch                       0.48-5.amzn2                                   amzn2-core                        51 k
 perl-PlRPC                                    noarch                       0.2020-14.amzn2                                amzn2-core                        36 k
 socat                                         x86_64                       1.7.3.2-2.amzn2.0.1                            amzn2-core                       291 k

Transaction Summary
==================================================================================================================================================================
Install  3 Packages (+12 Dependent packages)

Total download size: 53 M
Is this ok [y/d/N]: y

...
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                              38 MB/s |  53 MB  00:00:01
Retrieving key from https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
Importing GPG key 0x1BB943DB:
 Userid     : "MariaDB Package Signing Key <[email protected]>"
 Fingerprint: 1993 69e5 404b d5fc 7d2f e43b cbcb 082a 1bb9 43db
 From       : https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
Is this ok [y/N]: y

Start and enable mariadb service after installation:

sudo systemctl enable --now mariadb

Confirm service is started:

$ systemctl status mariadb
● mariadb.service - MariaDB 10.5.8 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: active (running) since Thu 2021-02-18 16:59:10 UTC; 9s ago
     Docs: man:mariadbd(8)
           https://mariadb.com/kb/en/library/systemd/
  Process: 2880 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 2857 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
  Process: 2854 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
 Main PID: 2868 (mariadbd)
   Status: "Taking your SQL requests now..."
   CGroup: /system.slice/mariadb.service
           └─2868 /usr/sbin/mariadbd

Secure your database server:

$ sudo mysql_secure_installation 

Create a database for Gitea.

$ mysql -u root -p

CREATE DATABASE gitea;
GRANT ALL PRIVILEGES ON gitea.* TO 'gitea'@'localhost' IDENTIFIED BY "StrongGitPassword";
FLUSH PRIVILEGES;
QUIT;

Test database connectivity as created user and its password:

$ mysql -u gitea -p'StrongGitPassword'
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| gitea              |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)

MariaDB [(none)]> \q
Bye

Step 3: Install Gitea on Amazon Linux 2

 Add a user git to manage Gitea on your system:

sudo useradd \
   --shell /bin/bash \
   --comment 'Git Admin' \
   git

You can set password for the user:

$ sudo passwd git
Changing password for user git.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Download gitea binary from the Downloads page. An easy way is downloading latest release using curl.

curl -s https://api.github.com/repos/go-gitea/gitea/releases/latest | grep browser_download_url |  cut -d '"' -f 4 | grep '\linux-amd64$' |  wget -qi -

Move the downloaded binary file to the /usr/local/bin directory

mv gitea-* gitea
chmod a+x ./gitea
sudo mv ./gitea /usr/local/bin/

Confirm installation by checking the software version:

$ gitea --version
Gitea version 1.13.2 built with GNU Make 4.1, go1.15.7 : bindata, sqlite, sqlite_unlock_notify

Create all the directories required to run Gitea on Amazon Linux 2:

sudo mkdir -p /etc/gitea /var/lib/gitea/{custom,data,indexers,public,log}
sudo chown  -R git:git /var/lib/gitea/
sudo chmod -R 770 /var/lib/gitea/
sudo chown root:git /etc/gitea
sudo chmod -R 770 /etc/gitea

Create systemd unit file for Gitea

sudo tee /etc/systemd/system/gitea.service<<EOF
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
After=mariadb.service

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

[Install]
WantedBy=multi-user.target
EOF

Reload systemd units:

sudo systemctl daemon-reload

Start Gitea service:

sudo systemctl start gitea

Confirm service status:

sudo chown  -R git:git /var/lib/gitea/
sudo chmod -R 770 /var/lib/gitea/
sudo systemctl restart gitea

Expected service status:

$ sudo  systemctl status gitea -l
● gitea.service - Gitea (Git with a cup of tea)
   Loaded: loaded (/etc/systemd/system/gitea.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-02-18 17:31:15 UTC; 5s ago
 Main PID: 30316 (gitea)
   CGroup: /system.slice/gitea.service
           └─30316 /usr/local/bin/gitea web -c /etc/gitea/app.ini

Feb 18 17:31:15 amazon-linux gitea[30316]: 2021/02/18 17:31:15 ...s/storage/storage.go:145:initAvatars() [I] Initialising Avatar storage with type:
Feb 18 17:31:15 amazon-linux gitea[30316]: 2021/02/18 17:31:15 ...les/storage/local.go:43:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/avatars
Feb 18 17:31:15 amazon-linux gitea[30316]: 2021/02/18 17:31:15 ...s/storage/storage.go:163:initRepoAvatars() [I] Initialising Repository Avatar storage with type:
Feb 18 17:31:15 amazon-linux gitea[30316]: 2021/02/18 17:31:15 ...les/storage/local.go:43:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/repo-avatars
Feb 18 17:31:15 amazon-linux gitea[30316]: 2021/02/18 17:31:15 ...s/storage/storage.go:157:initLFS() [I] Initialising LFS storage with type:
Feb 18 17:31:15 amazon-linux gitea[30316]: 2021/02/18 17:31:15 ...les/storage/local.go:43:NewLocalStorage() [I] Creating new Local Storage at /var/lib/gitea/data/lfs
Feb 18 17:31:15 amazon-linux gitea[30316]: 2021/02/18 17:31:15 routers/init.go:176:GlobalInit() [I] SQLite3 Supported
Feb 18 17:31:16 amazon-linux gitea[30316]: 2021/02/18 17:31:15 routers/init.go:56:checkRunMode() [I] Run Mode: Production
Feb 18 17:31:16 amazon-linux gitea[30316]: 2021/02/18 17:31:16 cmd/web.go:163:runWeb() [I] Listen: http://0.0.0.0:3000
Feb 18 17:31:16 amazon-linux gitea[30316]: 2021/02/18 17:31:16 ...s/graceful/server.go:55:NewServer() [I] Starting new server: tcp:0.0.0.0:3000 on PID: 30316

Also enable the service to start on boot

sudo systemctl enable gitea

Step 4: Configure Nginx Proxy

Install nginx web server:

sudo amazon-linux-extras install epel -y
sudo yum -y install nginx vim

Create gitea virtualhost file:

sudo vim /etc/nginx/conf.d/gitea.conf

Paste and modify below template to suite your use:

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

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

Set correct domain name and restart nginx service.

sudo systemctl restart nginx

Step 5: Access Gitea Web Dashboard

Visit the configured domain once you’ve updated DNS settings – http://git.example.com/install. Configure database connectivity in the first page:

In the general settings choose options to enable or disable:

I’ll disable self registration feature:

Create admin user you’ll use for administration purposes:

And you have successfully installed and configured Gitea git service on Amazon Linux 2. Login as admin user and start managing users and git repositories.

More articles on Amazon Linux:

 Install ejabberd on Amazon Linux 2

Install Jenkins on Amazon Linux 2

Install and Configure Chrony NTP server on Amazon Linux 2