You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

This guide will help you to Install Gitea self-hosted Git service on CentOS 7 / CentOS 8 server, and use Nginx to proxy all requests to Gitea. Gitea is a painless self-hosted Git service forked from Gogs. It is similar to GitHub, Bitbucket, and Gitlab in functionalities.

Gitea has been build to be fast, easy to use, and most painless way of setting up a self-hosted Git service with minimal resource usage. Gitea is written in Go and runs across all platforms (Linux, macOS, and Windows) and architectures that support Go.

For Ubuntu use How to Install Gitea self-hosted Git service on Ubuntu

Step 1: Update system and install git

Update all system packages and install git by running

sudo yum -y update
sudo yum -y install git wget vim bash-completion
sudo reboot

Step 2: Add git system user and create directories

Add git user account that will be used by Gitea.

sudo useradd \
   --system \
   --shell /bin/bash \
   --comment 'Git Version Control' \
   --create-home \
   --home-dir /home/git \
   git

Create the required directory structure

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

Step 3: Install MariaDB database server

We will use MariaDB to store Gitea data, install MariaDB on your server by following our previous guide

Install MariaDB 10.x on Ubuntu 18.04 and CentOS 7

Install MariaDB CentOS 8

Create a database for Gitea:

# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 608168
Server version: 10.3.9-MariaDB MariaDB Server

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 DATABASE gitea;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON gitea.* TO 'gitea'@'localhost' IDENTIFIED BY "StrongPassword";
Query OK, 0 rows affected (0.001 sec)

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

Step 4: Install and configure Gitea

Now download gitea binary from the Downloads page. Check the latest release before downloading it.

export VER=1.9.4
wget https://github.com/go-gitea/gitea/releases/download/v${VER}/gitea-${VER}-linux-amd64

Move the downloaded binary file to the /use/local/bindirectory

chmod +x gitea-${VER}-linux-amd64
sudo mv gitea-${VER}-linux-amd64 /usr/local/bin/gitea

You can confirm version installed using

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

Help page can be printed with --help option

~$ gitea --help
NAME:
   Gitea - A painless self-hosted Git service

USAGE:
   gitea [global options] command [command options] [arguments...]

VERSION:
   1.9.4 built with GNU Make 4.1, go1.12.9 : bindata, sqlite, sqlite_unlock_notify

DESCRIPTION:
   By default, gitea will start serving using the webserver with no
arguments - which can alternatively be run by running the subcommand web.

COMMANDS:
     web       Start Gitea web server
     serv      This command should only be called by SSH shell
     hook      Delegate commands to corresponding Git hooks
     dump      Dump Gitea files and database
     cert      Generate self-signed certificate
     admin     Command line interface to perform common administrative operations
     generate  Command line interface for running generators
     migrate   Migrate the database
     keys      This command queries the Gitea database to get the authorized command for a given ssh key fingerprint
     convert   Convert the database
     help, h   Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --port value, -p value         Temporary port number to prevent conflict (default: "3000")
   --pid value, -P value          Custom pid file path (default: "/var/run/gitea.pid")
   --custom-path value, -C value  Custom path file path (default: "/usr/local/bin/custom")
   --config value, -c value       Custom configuration file path (default: "/usr/local/bin/custom/conf/app.ini")
   --version, -v                  print the version
   --work-path value, -w value    Set the gitea working path (default: "/usr/local/bin")
   --help, -h                     show help

DEFAULT CONFIGURATION:
     CustomPath:  /usr/local/bin/custom 
     CustomConf:  /usr/local/bin/custom/conf/app.ini
     AppPath:     /usr/local/bin/gitea
     AppWorkPath: /usr/local/bin

Create a systemd service unit

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

Configure the file to set UserGroup and WorkDir

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

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#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
# If you want to bind Gitea to a port below 1024 uncomment
# the two values below
###
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

Reload systemd and restart service

sudo systemctl daemon-reload
sudo systemctl restart gitea

Also enable the service to start on boot

sudo systemctl enable gitea

Step 5: Configure Nginx Reverse Proxy

If you would like to access Gitea UI using a domain name without a port, you need to configure Apache or NGINX reverse proxy

sudo yum install nginx

Create a VirtualHost reverse proxy configuration file

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

Add:

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

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

If using SSL and would like to have http to https redirect, use

server {
	listen 80;
        server_name  git.example.com;
	location / {
        	rewrite     ^ https://git.example.com$request_uri? permanent;
    }
}

server {
        listen       443 ssl;
	ssl_certificate /etc/letsencrypt/live/git.example.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/git.example.com/privkey.pem;
        server_name git.example.com;

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

Replace git.example.com with your actual domain name.

Restart Nginx service:

sudo systemctl restart nginx

Step 6: Finish Gitea installation on CentOS 8 / CentOS 7

Start the installation by visiting http://domain-name/install

Set database authentication

On the first page, set the database connection

The username and password provided should match the ones provided in the Database configuration section. If the database server is on a different host, provide the IP address under the Host section.

Set Application General Settings

Provide application URL, this can be a routable server IP address or domain name that resolves to the IP. The same should be set for SSH.

Disable User self-registration

You can disable User self-registration under “Server and Other Services Settings”. This means the admin user will manually create user accounts.

You can optionally create an admin user account. By default, root user will gain admin access automatically.

When done with the configurations, click the “Install Gitea” button to finish the installation. On a successful installation, you should be logged into Gitea administration console

Refer to Gitea Documentation for advanced configurations and usage guides. Also look at Gitea config cheatsheet.

More guides:

How To Install and Configure GitLab on CentOS 8 / RHEL 8

Gitleaks – How to Audit git repository for secrets

How to Disable User Creation (Signup) on GitLab welcome page

How To Secure GitLab Server with SSL Certificate

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF