Skip to content

Instantly share code, notes, and snippets.

@sanikkenway
Last active November 19, 2021 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanikkenway/ad6d079bc9b092a0c63db321a8c7bb55 to your computer and use it in GitHub Desktop.
Save sanikkenway/ad6d079bc9b092a0c63db321a8c7bb55 to your computer and use it in GitHub Desktop.
Gitea Setup

Prerequisites

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’ll use SQLite as the database for Gitea. If SQLite is not installed on your Ubuntu system, install it by entering the following commands as sudo user:

sudo apt update
sudo apt install sqlite3

Installing Gitea

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

Install Git

The first step is to install Git on your server:

sudo apt update
sudo apt install git

Verify the installation by displaying the Git version:

git --version
git version <version>

Create a Git user

Create a new system user which 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 command above creates a new user and group named git, and set the home directory to /home/git. The output will look something like below:

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

Head over to the Gitea Download page and download the latest binary for your architecture.

Use wget to download the Gitea binary in the /tmp directory:

sudo wget -O /tmp/gitea https://dl.gitea.io/gitea/1.15.6/gitea-1.15.6-linux-amd64

You can run the gitea binary from any location. 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 commands below 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/gitea
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea

The directory structure above 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, we’ll set more restrictive permissions.

Create a Systemd Unit File

We’ll run Gitea as a systemd service.

Download the sample systemd unit file to the /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.

Enable and start the Gitea service:

sudo systemctl daemon-reload
sudo systemctl enable --now gitea

Verify that 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 <service started at>
   Main PID: <pid> (gitea)
      Tasks: <number> (limit: <number>)
     Memory: <amount>
     CGroup: /system.slice/gitea.service
             └─<pid> /usr/local/bin/gitea web --config /etc/gitea/app.ini
...

Configuring Nginx

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

First, install Nginx. Once done, open your text editor and edit the domain server block file:

sudo nano /etc/nginx/sites-enabled/git.example.com
server {
    listen 80;
    listen [::]:80;
    
    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;

    # 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 http://127.0.0.1:3000;
    }
}

Restart the Nginx service for changes to take effect:

sudo systemctl restart nginx

Configure Gitea

Now that Gitea is downloaded and running, we can finalize the installation through the web interface.

Open your browser, type http://domain, and a screen similar to the following will appear: alt text

Database Settings:

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

Application General Settings:

  • Site Title: Enter your organization name.
  • Repository Root Path: Leave the default 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 other Port
  • Gitea HTTP Listen Port: 80
  • Gitea Base URL: Use http and your domain or server IP address.
  • Log Path: Leave the default /var/lib/gitea/log

You can change the settings at any time by editing the Gitea configuration file.

To start the installation, hit the “Install Gitea” button.

The installation is instant. Once completed, you will be redirected to the login page.

Click on the “Need an account? Register now.” link. The first registered user is automatically added to the Admin group.

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

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

Configuring Email Notifications

If you want your Gitea instance to send notification emails, you can either install Postfix or use some transactional mail service such as SendGrid, MailChimp, MailGun, or SES.

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

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

Restart the Gitea service for changes to take effect:

sudo systemctl restart gitea

Upgrading Gitea

To upgrade to the latest Gitea version, simply download and replace the binary.

Stop the Gitea service:

sudo systemctl stop gitea

Download the latest Gitea version and move it to the /usr/local/bin directory:

wget -O /tmp/gitea https://dl.gitea.io/gitea/1.15.6/gitea-1.15.6-linux-amd64
sudo mv /tmp/gitea /usr/local/bin

Make the binary executable:

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

Restart the Gitea service:

sudo systemctl restart gitea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment