How to install Gitea on Ubuntu 20.04

s2 0

Gitea is a fast and easy to use self-hosted Git server written in Go. It includes a repository file editor, project issue tracking, user management, notifications, built-in wiki, and much more.

Gitea
is a lightweight application and can be installed on low-powered systems. If you’re looking for an alternative to Gitlab that uses much less memory and doesn’t need all of the bells and whistles that Gitlab offers, then you should give Gitea a try.

This article explains how to install and configure Gitea on Ubuntu 20.04.

requirements

Gitea supports SQLite, PostgreSQL and MySQL / MariaDB as database backends.

SQLite is only recommended for small installations. Larger installations should use MySQL or PostgreSQL.

We use SQLite as the database for Gitea. If you don’t have SQLite installed on your Ubuntu system, install it by entering the following commands as sudo user:

sudo apt updatesudo apt install sqlite3

Install Gitea

Gitea provides Docker images and can be installed from source, binary and as a package. We will install Gitea from binary.

Git. to install

The first step is to install Git on your server:

sudo apt updatesudo apt install git

Verify the installation by viewing the Git version:

git --version
git version 2.25.1

Create a git user

Create a new system user who will run the Gitea application by typing:

sudo adduser    --system    --shell /bin/bash    --gecos 'Git Version Control'    --group    --disabled-password    --home /home/git    git

The above command creates a new user and group named git, and set up the home directory /home/git. The output looks something like this:

Adding system user `git' (UID 112) ...
Adding new group `git' (GID 118) ...
Adding new user `git' (UID 112) with group `git' ...
Creating home directory `/home/git' ...

Download Gitea binary

Go over to the Gitea download page
and download the latest binary for your architecture. At the time of writing, the latest version is 1.10.2. When a new version is available, change the VERSION Variable in command below.

Use wget
to download the gitea binary in the /tmp Directory:

VERSION=1.14.1sudo wget -O /tmp/gitea https://dl.gitea.io/gitea/${VERSION}/gitea-${VERSION}-linux-amd64

You can the gitea binary from anywhere. We’ll follow the convention and move the binary to the /usr/local/bin Directory:

sudo mv /tmp/gitea /usr/local/bin

Make the binary executable:

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

Run the following commands to create the directories and set the required permissions and ownership:

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

The above directory structure is recommended by the official Gitea documentation.

The permissions of the /etc/gitea Directory are set to 770 so that the installation wizard can create the configuration file. Once the installation is complete, more restrictive permissions will be set.

Create a systemd unit file

We will run Gitea as a system service.

Load the sample systemd unit file into the. down /etc/systemd/system Directory by typing:

sudo wget https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/systemd/gitea.service -P /etc/systemd/system/

You don’t need to edit the file, it is configured to match our setup.

Activate and start the Gitea service:

sudo systemctl daemon-reloadsudo systemctl enable --now gitea

Check if Gitea is running:

sudo 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 Thu 2021-05-06 05:32:04 UTC; 7s ago
   Main PID: 77781 (gitea)
      Tasks: 6 (limit: 470)
     Memory: 130.6M
     CGroup: /system.slice/gitea.service
             └─77781 /usr/local/bin/gitea web --config /etc/gitea/app.ini
...

Configure Gitea

Now that Gitea has been downloaded and run, we can complete the installation using the web interface.

By default, Gitea listens for connections on port 3000 on all network interfaces.

If your server is running a UFW firewall, you will need to open the Gitea port. So allow traffic on the port 3000, enter the following command:

sudo ufw allow 3000/tcp

Open your browser, type https://YOUR_DOMAIN_IR_IP:3000, and a screen similar to the following appears:

Database settings:

  • Database type: SQLite3
  • Path: Use an absolute path /var/lib/gitea/data/gitea.db

General application settings:

  • Site Title: Enter your organization name.
  • Repository Root Path: Leave the default setting var/lib/gitea/data/gitea-repositories.
  • Git LFS Root Path: Leave the default /var/lib/gitea/data/lfs.
  • Run as username: git
  • SSH server domain: Enter your domain or server IP address.
  • SSH port: 22, change it if SSH is listening on a different port
  • Gitea HTTP listening port: 3000
  • Gitea Base URL: Use http and your domain or server IP address.
  • Log path: leave the default setting /var/lib/gitea/log
You can change the settings at any time by editing the Gitea configuration file.

To start the installation, click on the “Install Gitea” button.

The installation takes place immediately. After completion you will be redirected to the login page.

Click on “Do you need an account? Register now. “Shortcut. The first registered user is automatically added to the admin group.

Change the permissions of the Gitea configuration file to read-only with:

sudo chmod 750 /etc/giteasudo chmod 640 /etc/gitea/app.ini

That’s it. Gitea has been installed on your Ubuntu computer.

Configure Nginx as an SSL termination proxy

This step is optional but is highly recommended. SSL termination means that Nginx will act as an intermediary between the Gitea application and the web clients so that you can access Gitea over HTTPS.

To use Nginx as a reverse proxy you need a domain or subdomain pointing to your server’s public IP. In this example we are using git.example.com.

First, install Nginx and generate a free Let’s Encrypt SSL certificate using the following guides:

  • How to install Nginx on Ubuntu 20.04
  • Secure Nginx with Let’s Encrypt on Ubuntu 20.04

When you’re done, open your text editor and edit the domain server block file:

sudo nano /etc/nginx/sites-enabled/git.example.com
/etc/nginx/sites-enabled/git.example.com
server {
    listen 80;
    server_name git.example.com;

    include snippets/letsencrypt.conf;
    return 301 https://git.example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name git.example.com;

    proxy_read_timeout 720s;
    proxy_connect_timeout 720s;
    proxy_send_timeout 720s;

    client_max_body_size 50m;

    # Proxy headers
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/git.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/git.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/git.example.com/chain.pem;
    include snippets/letsencrypt.conf;
    include snippets/ssl.conf;

    # log files
    access_log /var/log/nginx/git.example.com.access.log;
    error_log /var/log/nginx/git.example.com.error.log;

    # Handle / requests
    location / {
       proxy_redirect off;
       proxy_pass https://127.0.0.1:3000;
    }
}

Don’t forget to replace git.example.com with your Gitea domain and set the correct path to the SSL certificate files. The HTTP traffic is redirected to HTTPS

Restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

Next, change the Gitea domain and root url. To do this, open the configuration file and edit the following lines:

sudo nano /etc/gitea/app.ini
/etc/gitea/app.ini
[server]
DOMAIN           = git.example.com
ROOT_URL         = https://git.example.com/

Restart the Gitea service by typing:

sudo systemctl restart gitea

At this point the Gitea proxy is configured and you can access it at: https://git.example.com

Configure email notifications

If you want your Gitea instance to send notification emails, you can either install Postfix or use a transactional email service like SendGrid, MailChimp, MailGun or SES.

To enable email notifications, open the configuration file and edit the following lines:

sudo nano /etc/gitea/app.ini
/etc/gitea/app.ini
[mailer]
ENABLED = true
HOST    = SMTP_SERVER:SMTP_PORT
FROM    = SENDER_EMAIL
USER    = SMTP_USER
PASSWD  = YOUR_SMTP_PASSWORD

Make sure you are entering the correct SMTP server information.

Restart the Gitea service for the changes to take effect:

sudo systemctl restart gitea

To review the settings and send a test email, log into Gitea and go to: Site Administration> Configuration> SMTP Mailer Configuration.

Gitea also allows you to connect to Slack by using a Web webhook
and send notifications to your Slack channels.

Upgrade from Gitea

To update to the latest Gitea version, just download the binary and replace it.

  1. Stop the Gitea service:

    sudo systemctl stop gitea
  2. Download the latest version of Gitea and move it to the /usr/local/bin Directory:

    VERSION=<THE_LATEST_GITEA_VERSION>wget -O /tmp/gitea https://dl.gitea.io/gitea/${VERSION}/gitea-${VERSION}-linux-amd64sudo mv /tmp/gitea /usr/local/bin
  3. Make the binary executable:

    sudo chmod +x /usr/local/bin/gitea
  4. Restart the Gitea service:

    sudo systemctl restart gitea

That’s it.

diploma

This tutorial walked you through installing Gitea on Ubuntu 20.04. For more information on configuring your Gitea instance and creating your first project, see Gitea documentation page
.

If you have any questions, feel free to leave a comment below.