Skip to content

Latest commit

 

History

History
424 lines (300 loc) · 8.88 KB

install-gogs-raspberrypi.md

File metadata and controls

424 lines (300 loc) · 8.88 KB

Install Gogs on RaspberryPi

This guide follows and expands on the official Gogs installation guide: https://gogs.io/docs/installation ...

Our objective is to capture all the steps required so that anyone can follow along and replicate our exact result.

If you get stuck along the way, please don't suffer in silence! Ask for help; open an issue!

Prerequisites

Ensure you have a working RaspberryPi running Ubuntu: https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#1-overview

We are using Ubuntu because it's ubiquitous and will help with both Continuous Integration and deployment to Cloud/VPS later so getting it working on the Pi is a good first step. ✅

Postgres

The default DB for Gogs is MySQL, but we prefer Postgres.

Install Postgres on Ubuntu: learn-postgresql#ubuntu

sudo apt-get install -y postgresql postgresql-contrib postgresql-client libpq-dev

Open the PostgreSQL interactive terminal to create a new database and user for Gogs:

sudo -u postgres psql -d template1

You should see output similar to the following:

psql (12.9 (Ubuntu 12.9-0ubuntu0.20.04.1))
Type "help" for help.

template1=#

Create new user for Gogs:

CREATE USER gogs CREATEDB;

You should see output similar to:

CREATE ROLE

Set the password for user gogs:

\password gogs

It will prompt you for the password and password confirmation. Take note of this password, you will need it later when configuring Gogs.

Create new database for Gogs:

CREATE DATABASE gogs OWNER gogs;

Exit the psql terminal:

\q

git

Install git if not already installed in your Ubuntu instance:

sudo apt update && sudo apt upgrade
sudo apt-get install -y git

If you see something similar to the following:

git is already the newest version (1:2.25.1-1ubuntu3.2).

You already have git and can proceed.

Create a new user called git to run gogs:

sudo adduser --disabled-login --gecos 'Gogs' git

Go lang

We are going to compile gogs from source so we need the Golang compiler:

sudo su - git
mkdir $HOME/local && cd $_

Tip: Check https://go.dev/dl/ for the latest version of Go before running the next command.

Download Go:

wget https://storage.googleapis.com/golang/go1.18.linux-arm64.tar.gz

This will take a few seconds depending on your Internet connection speed ...

go1.18.linux-arm64.t  59%[==========>         ]  79.99M  4.92MB/s    eta 12s

Once it's downloaded, extract it:

tar -C /home/git/local -xvzf go1.18.linux-arm64.tar.gz

Set the GOPATH environment variable to specify the location of our workspace. We will set the variables in our .bashrc file so they will be available every time we enter the shell.

echo 'export GOROOT=$HOME/local/go' >> $HOME/.bashrc
echo 'export GOPATH=$HOME/go' >> $HOME/.bashrc
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> $HOME/.bashrc
source $HOME/.bashrc

Note: We need to specify the GOROOT environment variable since we are installing Go to a custom location. Check that Go is properly installed:

go version

You’ll see output that resembles the following:

go version go1.18 linux/arm64

With Go and Postgres installed, you're all set to install Gogs!


Gogs!

Download and install Gogs following the official instructions: https://gogs.io/docs/installation/install_from_source

# Clone the repository to the "gogs" subdirectory
git clone --depth 1 https://github.com/gogs/gogs.git gogs

# Change working directory
cd gogs

# Compile the main program, dependencies will be downloaded at this step

Build the Gogs binary:

go build -o gogs

This may take a few minutes during which your console will appear unresponsive.

It will produce a binary named gogs in the current directory.

Execute the binary:

./gogs web

In your terminal, you should see output similar to the following:

2022/04/10 12:34:24 [ INFO] Gogs 0.13.0+dev
2022/04/10 12:34:24 [TRACE] Work directory: /home/git/local/gogs
2022/04/10 12:34:24 [TRACE] Custom path: /home/git/local/gogs/custom
2022/04/10 12:34:24 [TRACE] Custom config: /home/git/local/gogs/custom/conf/app.ini
2022/04/10 12:34:24 [TRACE] Log path: /home/git/local/gogs/log
2022/04/10 12:34:24 [TRACE] Build time:
2022/04/10 12:34:24 [TRACE] Build commit:
2022/04/10 12:34:24 [ INFO] Run mode: Development
2022/04/10 12:34:24 [ INFO] Listen on http://0.0.0.0:3000

In the case of running Ubuntu as a server, you will need the local network IP address in order to access the Gogs server. Run the following command in the Terminal on the Raspberry Pi to get the IP:

hostname -I

Output should be similar to the following:

192.168.1.196

Armed with this IP address, visit http://192.168.1.196:3000 (either on the Ubuntu Pi or on another computer on the same network ...)

image

Use the IP address for the PI as the Application URL: http://192.168.1.196:3000/

image

That way you can connect from other computers on your home network.

https://gogs.io/docs/installation/configuration_and_run

Once configured, created a new user and repo: http://192.168.1.196:3000/nelsonic/my-awesome-repo image

Nginx

  1. Create self-signed certificate:

Run the commands as root:

sudo su -
mkdir /root/certs && cd /root/certs

Create the self-signed certificate:

openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out MyCertificate.crt -keyout MyKey.key

Accept all the defaults for the certificate creation, this is just for use on your local network.

  1. Install Nginx
sudo apt-get install -y nginx

If you visit the IP address of your Ubuntu server in a web browser now, e.g: http://192.168.1.196 you will see the default Nginx homepage:

default-nginx

Create the gogs site:

sudo vi /etc/nginx/sites-available/gogs
server {
    listen 80 default_server;
    server_name $http_host;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:3000;
    }
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /root/certs/MyCertificate.crt;
    ssl_certificate_key /root/certs/MyKey.key;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:3000;
    }
}

Link it:

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

Delete the default nginx config as we don't need it:

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

Restart:

sudo systemctl restart nginx

When you refresh the page in your browser you will now see the gogs page:

gogs-page-nginx

Automatic Startup with systemd

Create the systemd config file:

sudo vi /etc/systemd/system/gogs.service
[Unit]
Description=Gogs (Go Git Service)
After=syslog.target
After=network.target
After=postgresql.service
After=nginx.service

[Service]
Type=simple
User=git
Group=git
WorkingDirectory=/home/git/local/gogs
ExecStart=/home/git/local/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git

[Install]
WantedBy=multi-user.target

Enable the systemd unit file:

sudo systemctl enable gogs

Start the service:

sudo systemctl start gogs

Check the status of the service:

sudo systemctl status gogs

It should display the output like this:

● gogs.service - Gogs (Go Git Service)
     Loaded: loaded (/etc/systemd/system/gogs.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-04-10 21:58:44 UTC; 4s ago
   Main PID: 127605 (gogs)
      Tasks: 7 (limit: 9021)
     CGroup: /system.slice/gogs.service
             └─127605 /home/git/local/gogs/gogs web

Apr 10 21:58:44 ubuntu systemd[1]: Started Gogs (Go Git Service).
Apr 10 21:58:44 ubuntu gogs[127605]: 2022/04/10 21:58:44 [TRACE] Log mode: File (Info)



Relevant Reading / Research

HitCount