In this tutorial, we'll explain how to install Gogs a Git Hosting on Ubuntu 22.04 with MariaDB and secure with SSL certificate. You can perform this tutorial on Ubuntu 24.04 too.
Gogs is a self-hosted Git service that's lightweight and easy to set up. It’s written in Go and offers a feature-rich, simple-to-install experience for managing Git repositories. Here’s a detailed step-by-step guide to setting up Gogs on an Ubuntu server.
Prerequisites
- Ubuntu (22.04 or later) dedicated server or KVM VPS.
- A non-root user with sudo privileges
- Basic knowledge of Linux command-line operations
- Domain name (For accessing Gogs via a web URL)
Step 1: Update the System
First, make sure your system packages are up-to-date.
sudo apt update
sudo apt upgrade -y
Step 2: Install Required Dependencies
Gogs requires a few dependencies to run properly. Install them using:
sudo apt install -y git mariadb-server
Step 3: Install Go
Gogs is written in Go, so you need to have Go installed on your server. You can download and install it using the following commands:
wget https://golang.org/dl/go1.22.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
echo "export PATH=\$PATH:/usr/local/go/bin" >> ~/.profile
source ~/.profile
Verify the installation by checking the Go version:
go version
Step 4: Install and Configure Gogs
For security purposes, create a separate user to run Gogs.
sudo adduser --disabled-login --gecos 'Gogs' git
Download and Install Gogs
Switch to the created user and download Gogs.
sudo su - git
wget https://dl.gogs.io/0.13.0/gogs_0.13.0_linux_amd64.tar.gz
tar -xzf gogs_0.13.0_linux_amd64.tar.gz
Configure Gogs
Navigate to the gogs directory and set up Gogs.
cd gogs
./gogs web
This will start Gogs with default settings. You can configure Gogs via the web interface at http://<your_server_ip>:3000
on your web browser. If you have a domain, you can set up a reverse proxy to access it through your domain.
Step 5: Setting Up Systemd Service
To manage Gogs as a service, create a systemd service file:
sudo nano /etc/systemd/system/gogs.service
Add the following content:
[Unit]
Description=Gogs
After=syslog.target
After=network.target
After=mariadb.service mysqld.service postgresql.service memcached.service redis.service
[Service]
Type=simple
User=git
Group=git
ExecStart=/home/git/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git
[Install]
WantedBy=multi-user.target
Save and exit the editor.
Reload systemd to recognize the new service and start Gogs.
sudo systemctl daemon-reload
sudo systemctl start gogs
sudo systemctl enable gogs
Step 6: Create Database
Log into the MariaDB shell as the root user:
sudo mysql -u root -p
Enter the root password you set during the secure installation process.
Create a Database and User
Once logged in, create a database for Gogs and a user to manage it. Replace gogs_db, gogs_user, and your_password
with your preferred database name, username, and password.
CREATE DATABASE gogs_db;
CREATE USER 'gogs_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON gogs_db.* TO 'gogs_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 7: Set Up a Reverse Proxy with Nginx
If you have a domain and want to access Gogs through it, you can set up a reverse proxy using Nginx.
Install Nginx:
sudo apt install -y nginx
Create a new Nginx configuration file for your domain:
sudo nano /etc/nginx/sites-available/gogs
Add the following configuration:
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Enable the configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/gogs /etc/nginx/sites-enabled/
Test and restart the Nginx service.
nginx -t
sudo systemctl restart nginx
Step 8: Obtain SSL Certificates with Let's Encrypt Certbot
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Obtain and install SSL certificate:
Replace gogs.your_domain.com
with your actual domain name.
sudo certbot --nginx -d gogs.your_domain.com
Follow the prompts to complete the SSL certificate installation.
Step 9: Access Gogs
You can now access Gogs by navigating to http://<your_server_ip>:3000
in your web browser. Complete the setup by providing the required information such as database configuration, administrator account details, and other preferences.
Conclusion
You’ve now successfully set up Gogs for lightweight Git hosting on your Ubuntu server. You can start creating repositories, managing users, and utilizing all the features Gogs offers for Git repository management.