Setting up a self-hosted git repository with Gitea and PostgreSQL
Table of contents
- Install PostgreSQL
- Installing Gitea
- Step 1— Update the APT package cache, upgrade the already installed software and install Git:
- Step 2— Download the Gitea Binary and make it executable:
- Step 3— Add the user that will run the Gitea application:
- Step 4— Create the folder structure that is used by Gitea to store data:
- Step 5— Set the working directory of Gitea:
- Step 6— Copy the Gitea binary file to /usr/local/bin to make it available system-wide:
- Run Gitea as service
- Step 1— Create a systemd service for Gitea
- Step 2— Copy the following content into the service file:
- Step 3 — Enable the service and start Gitea at system boot:
- Step 4— In a web browser go to http://your_instance_ip:3000 to access the Gitea application
- Step 5— Select the PostgreSQL database and complete the form by entering the database name, username, and password. Based on the provided configurations, use "giteadb" as the database name, and both the username and password should be set as "gitea."
- Setting up a Nginx reverse proxy
- Step 1— Install Nginx:
- Step 2— Disable the default virtual host which has been configured during the installation of Nginx via the APT package manager:
- Step 3— Enter the directory /etc/nginx/sites-available and create a reverse proxy configuration file:
- Step 4— Paste the following Nginx configuration block in the text editor. The proxy server redirects all incoming connections to the web server listening on port 80, to the local Gitea application, listening on port 3000.
- Step 5— Create a symbolic link from /etc/nginx/sites-available/gitea.conf to /etc/nginx/sites-enabled/gitea.conf to activate the new configuration:
- Step 6— Restart Nginx:
Gitea is a painless self-hosted all-in-one software development service, it includes Git hosting, code review, team collaboration, package registry and CI/CD. It is similar to GitHub, Bitbucket and GitLab.
The tutorial offers a comprehensive guide with detailed commands to set up Gitea on your server for hosting Git repositories. It encompasses instructions for installing PostgreSQL, Gitea, and Nginx, along with creating a backup strategy to protect repositories by storing them on an external shared drive.
Install PostgreSQL
Step 1 — Installing PostgreSQL
To install PostgreSQL, first refresh your server’s local package index:
sudo apt update
Then, install the Postgres package along with a -contrib package that adds some additional utilities and functionality:
sudo apt install postgresql postgresql-contrib
Ensure that the service is started:
sudo systemctl start postgresql.service
Enable that the service :
sudo systemctl enable postgresql.service
Step 2 — Using PostgreSQL Roles and Databases
sudo -i -u postgres
psql
CREATE ROLE gitea WITH LOGIN PASSWORD 'gitea';
CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';
cd /etc/postgresql/14/main
where 14 is the version here , edit it to the version that you have installed.
Add the following line as the first line of pg_hba.conf . It allows access to all databases for all users with an encrypted password:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 scram-sha-256
to allow the connection from outside pgAdmin , Add or edit the following line in your postgresql.conf :
listen_addresses = '*'
Restart the service
service postgresql restart
Installing Gitea
Step 1— Update the APT package cache, upgrade the already installed software and install Git:
sudo apt update && sudo apt upgrade -y && sudo apt install git -y
Step 2— Download the Gitea Binary and make it executable:
wget -O gitea https://dl.gitea.com/gitea/1.21/gitea-1.21-linux-amd64
chmod +x gitea
Step 3— Add the user that will run the Gitea application:
sudo adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git
Step 4— Create the folder structure that is used by Gitea to store data:
sudo mkdir -p /var/lib/gitea/custom
sudo mkdir -p /var/lib/gitea/data
sudo mkdir -p /var/lib/gitea/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
Step 5— Set the working directory of Gitea:
export GITEA_WORK_DIR=/var/lib/gitea/
Step 6— Copy the Gitea binary file to /usr/local/bin to make it available system-wide:
sudo cp gitea /usr/local/bin/gitea
Run Gitea as service
Step 1— Create a systemd service for Gitea
sudo nano /etc/systemd/system/gitea.service
Step 2— Copy the following content into the service file:
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
#Requires=mysql.service
#Requires=mariadb.service
#Requires=postgresql.service
#Requires=memcached.service
#Requires=redis.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
Step 3 — Enable the service and start Gitea at system boot:
systemctl enable gitea.service
systemctl start gitea.service
Step 4— In a web browser go to http://your_instance_ip:3000
to access the Gitea application
Step 5— Select the PostgreSQL database and complete the form by entering the database name, username, and password. Based on the provided configurations, use "giteadb" as the database name, and both the username and password should be set as "gitea."
Setting up a Nginx reverse proxy
Step 1— Install Nginx:
sudo apt update
sudo apt install nginx
sudo systemctl status nginx
sudo systemctl enable nginx
sudo apt-get update
sudo apt-get install nginx-extras
Step 2— Disable the default virtual host which has been configured during the installation of Nginx via the APT package manager:
sudo unlink /etc/nginx/sites-enabled/default
Step 3— Enter the directory /etc/nginx/sites-available and create a reverse proxy configuration file:
cd /etc/nginx/sites-available
sudo nano gitea
Step 4— Paste the following Nginx configuration block in the text editor. The proxy server redirects all incoming connections to the web server listening on port 80, to the local Gitea application, listening on port 3000.
upstream gitea {
server 127.0.0.1:3000;
}
server {
listen 80;
server_name gitea.com;
root /var/lib/gitea/public;
location / {
proxy_pass http://localhost:3000;
}
}
Step 5— Create a symbolic link from /etc/nginx/sites-available/gitea.conf to /etc/nginx/sites-enabled/gitea.conf to activate the new configuration:
sudo ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/gitea
Step 6— Restart Nginx:
service nginx restart
Subscribe to my newsletter
Read articles from Ayat AlBqoor directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by