How to Install Gitea on Debian 9

Published on: Fri, Feb 8, 2019 at 11:06 am EST
Debian Linux Guides Programming Server Apps

Gitea is an alternative open source, self-hosted version control system powered by Git. Gitea is written in Golang and is a lightweight solution to be hosted on any platform.

Prerequisites

  • New Vultr Debian 9 instance.
  • Non-root user with sudo privileges.
  • Nginx
  • Git
  • MariaDB

Step 1: Install Nginx

Update your package list.

sudo apt update

Install Nginx.

sudo apt -y install nginx

Once the install is complete, run the following commands to start and enable the Nginx service.

sudo systemctl enable nginx.service
sudo systemctl start nginx.service

Step 2: Install Git

This can be done with the following command.

sudo apt -y install git

Step 3: Install MariaDB Database Server

Gitea supports the following databases servers.

  • MariaDB/MySQL
  • PostgreSQL
  • SQLite
  • TiDB

For this tutorial we will be using the MariaDB server and client.

sudo apt -y install mariadb-server mariadb-client

Once complete, make sure MariaDB is enabled and running.

sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service

After that, run the command below to secure the MariaDB server by creating a root password and disallowing remote root access.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

Enter current password for root (enter for none): Just press the Enter
Set root password? [Y/n]: Y
New password: Enter password
Re-enter new password: Repeat password
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

Restart MariaDB.

sudo systemctl restart mariadb.service

Type the command below to login to the MariaDB console.

sudo mysql -u root -p

Then type the password you created above to login. You will see the MariaDB welcome message.

Create a database called gitea.

CREATE DATABASE gitea;

Create a database user called giteauser with a new password.

CREATE USER 'giteauser'@'localhost' IDENTIFIED BY 'new_password_here';

Make sure you replace new_password_here with a strong and complex password.

Then grant the user full access to the database.

GRANT ALL ON gitea.* TO 'giteauser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

Step 4: Prepare the Gitea Environment

Create a user to run Gitea.

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

Create the required directory structure.

sudo mkdir -p /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 mkdir /etc/gitea
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea

Step 5: Install Gitea

The Gitea binary can be downloaded by the running the following commands.

sudo wget -O gitea https://dl.gitea.io/gitea/1.5.0/gitea-1.5.0-linux-amd64 
sudo chmod +x gitea

Copy the binary to a global location.

sudo cp gitea /usr/local/bin/gitea

Step 6: Create a service file to start Gitea automatically

Create a linux service file.

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

Using a text editor of your choice, open this newly create file and populate if with the following.

[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

Enable and start Gitea at boot.

sudo systemctl daemon-reload
sudo systemctl enable gitea
sudo systemctl start gitea

Ensure Gitea is running.

sudo systemctl status gitea

Step 7: Configure Nginx as a reverse proxy

Delete the default nginx configuration file.

sudo rm /etc/nginx/sites-enabled/default

Create a reverse proxy configuration for Gitea.

sudo touch /etc/nginx/sites-available/git

Populate the file with the following configuration, make sure you replace example.com with your domain name or IP address.

upstream gitea {
    server 127.0.0.1:3000;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com;
    root /var/lib/gitea/public;
    access_log off;
    error_log off;

    location / {
      try_files maintain.html $uri $uri/index.html @node;
    }

    location @node {
      client_max_body_size 0;
      proxy_pass http://localhost:3000;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_max_temp_file_size 0;
      proxy_redirect off;
      proxy_read_timeout 120;
    }
}

Enable the Gitea Nginx reverse proxy configuration.

sudo ln -s /etc/nginx/sites-available/git /etc/nginx/sites-enabled/git

Then reload the Nginx Service.

sudo systemctl reload nginx.service

Next, open your browser and browse to the server hostname or IP address.

http://YOUR_SERVER_IP/install

Follow the on-screen instructions to complete the Gitea setup.

Want to contribute ?

You could earn up to $300 by adding new articles