How to Install Gitea with NGINX and Free Let’s Encrypt SSL on Ubuntu 20.04

how to install gitea with nginx and free let’s encrypt ssl on ubuntu 20.04

In this tutorial, we are going to show you how to install the Gitea software on your VPS along with Nginx as a webserver and Free Let’s Encrypt certificate, using Ubuntu 20.04.

install gitea with nginx and free lets encrypt ssl on ubuntu 20.04

Gitea is software written in “Go” programing language and is similar to Bitbucket, GitHub, and Gitlab. The software is used for self-hosted Git service and is compatible with multiple operating systems like Linux, Windows, macOS, and ARM.

The installation will take no more than 10 minutes and you will enjoy it while installing it. Let’s get started!

Prerequisites

  • Fresh install of Ubuntu 20.04
  • User privileges: root or non-root user with sudo privileges

Step Step 1. Update the System

A fresh installation of Ubuntu 20.04 needs a system update and that’s why we need to execute the following commands.

sudo apt update -y && sudo apt upgrade -y

Step 2. Install the required dependencies

These packages need to be installed before we proceed with the installation.

apt install gnupg2 git unzip -y

Step 3. Install Nginx webserver

Nginx web server can be installed with the following command:

sudo apt install nginx -y

Once, the installation is complete you can check the status of the Nginx service:

sudo systemctl status nginx

Step 4. Install MariaDB database server

MariaDB is used as database backend for the Gitea and it can be installed with the command below:

sudo apt-get install mariadb-server -y

Step 5. Configure MariaDB database server

Once, the database server is installed successfully we need to configure it.

Log in to the MariaDB shell with “mysql” command and enable the Innodb table:

MariaDB [(none)]> SET GLOBAL innodb_file_per_table = ON;

Exit from the MariaDB shell with “exit” command and open the default MariaDB configuration file:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Add the following lines of code under the “mysqld” section

innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_default_row_format = dynamic

Once, the changes are made restart the MariaDB service in order for the changes to take effectivity.

sudo systemctl restart mariadb

Step 6. Create database and database user

Gitea software requires database and user in order can function properly. To create them and grant the proper privileges please log in back to the MariaDB shell and execute the following commands:

MariaDB [(none)]>CREATE DATABASE gitea;
MariaDB [(none)]>CREATE USER 'gitea'@'localhost' IDENTIFIED BY 'strongpasswordhere';
MariaDB [(none)]>GRANT ALL ON gitea.* TO 'gitea'@'localhost' IDENTIFIED BY 'strongpasswordhere' WITH GRANT OPTION;
MariaDB [(none)]>ALTER DATABASE gitea CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;
MariaDB [(none)]>FLUSH PRIVILEGES;
MariaDB [(none)]>exit;

Step 7. Install Gitea

This is the step we were waiting so long and that is installing the Gitea software. Before we install it, we need to create a system user to run Gitea with the following command:

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

Next is to download the latest version of Gitea on your server.

sudo wget https://dl.gitea.io/gitea/1.14.6/gitea-1.14.6-linux-amd64

Once, the Gitea is downloaded we need to copy the binary to the system path and set the right permissions :

sudo cp gitea-1.14.6-linux-amd64 /usr/bin/gitea && chmod 755 /usr/bin/gitea

Next, step is to create Gitea directory for storing the data and the logs along with their permissions:

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

Step 8. Create Gitea Service

Create an empty file:

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

And, import the following lines of code:

[Unit]
Description=Gitea
After=syslog.target
After=network.target
After=mysql.service
[Service]
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/bin/gitea web -c /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
[Install]
WantedBy=multi-user.target

After creating the file with content in it reload the configuration file, enable and start the Gitea service.

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

To check if Gitea is running properly execute the following command:

sudo systemctl status gitea

The output should be similar to this:

# sudo systemctl status gitea
● gitea.service - Gitea

Loaded: loaded (/etc/systemd/system/gitea.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-08-09 23:36:36 CEST; 8min ago
Main PID: 42187 (gitea)
Tasks: 6 (limit: 4652)
Memory: 111.8M
CGroup: /system.slice/gitea.service
└─42187 /usr/bin/gitea web -c /etc/gitea/app.ini

Step 9. Nginx as a Reverse Proxy for Gitea

First, create new Nginx configuration file:

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

Add the following lines of code:

upstream gitea {
server 127.0.0.1:3000;
}
server {
listen 80;
server_name gitea.yourdomain.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;
}
}

Once, the file is created save it close it, and check the Nginx configuration with the following command:

nginx -t

The output should be:

#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the configuration is OK, restart the Nginx service to get the latest changes.

systemctl restart nginx

Step 10. Install Free Let’s Encrypt SSL certificate

Next, you will need to install a free ‘Let’s Encrypt’ SSL certificate. ‘Let’s Encrypt’ is currently one of the most popular TLS encryption providers in the past few years. It is the world’s biggest certificate authority, otherwise used by over 250 million websites.

installing gitea with nginx and free lets encrypt ssl on ubuntu 20.04

First, you need to install Certbot software on the server with the following command:

sudo apt install certbot python3-certbot-nginx -y

Once, the Certbot is installed we are ready to install the certificate for the domain:

sudo certbot --nginx -d gitea.example.com

While installing you can choose the redirect option and all requests from HTTP will be redirected to HTTPS.

After the successful installation the following similar message will be received:

- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/gitea.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/gitea.example.com/privkey.pem

11. Access the Gitea website securely

Now when every step is completed we can access our Gitea website at https://gitea.example.com Enter the MySQL user password that you used to create in step 6. Scroll down on the bottom of the page and hit on the “Install Gitea“.

set up gitea with nginx and free lets encrypt ssl on ubuntu 20.04

Congratulations! You successfully installed Gitea software and you can enjoy using it now. Of course, you can simply subscribe to any of our Linux VPS hosting plans and let our experts set up everything within minutes, completely free of charge.

PS. If you liked this post on how to install Gitea on Nginx with Free Lets Encrypt SSL on Ubuntu 20.04, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment

To prove you are human please solve the following *