Gogs on Raspberry Pi

(updated: 2016-06-30)

Gogs is a github-like Git service built with Golang.

With Gogs, you can run your own Git service on Raspberry Pi very easily.

gogs_logo


0. Prerequisite

A. Prepare a running Raspberry Pi (Raspbian)

You’ve already got one, haven’t you? :-)

B. Install Golang

You can install golang from source code with this script.

It checks out latest code from the repository, build, and install binaries for Raspberry Pi.

Whole processes will take about 1 hour or so.

After installation, add following to your rc file(eg. .zshrc, .bashrc, …):

# for go
if [ -d /opt/go/bin ]; then
  export GOROOT=/opt/go
  export GOPATH=$HOME/srcs/go
  export PATH=$PATH:$GOROOT/bin
fi

Close your terminal and open a new one, then you will be able to run golang excutables.

You can check your installation with following command:

$ go version
go version go1.5.1 linux/arm

C. Install other requirements for Gogs

You’ll need to install some more packages before going to the next step.

$ sudo apt-get install git

# Gogs supports MySQL, PostgreSQL, and SQLite. You can choose one.
$ sudo apt-get install mysql-server
# or
$ sudo apt-get install postgresql

D. Create a directory for repositories

$ mkdir ~/repositories

1-A. Checkout and build latest Gogs source code

If you want to build Gogs from the source code, here is the way: Install From Source Code.

a. If you don’t need SQLite3/Redis/Memcache support,

In your terminal,

# download and install dependencies (takes some time)
$ go get -u -tags "cert" github.com/gogits/gogs

# build
$ cd $GOPATH/src/github.com/gogits/gogs
$ go build -tags "cert"

b. If you need to enable SQLite3/Redis/Memcache,

# download and install dependencies (takes some time)
$ go get -u -tags "cert sqlite redis memcache" github.com/gogits/gogs

# build
$ cd $GOPATH/src/github.com/gogits/gogs
$ go build -tags "cert sqlite redis memcache"

1-B. Or, you can download pre-built binary

Download pre-built binary (Linux arm for Raspberry Pi!) from this page: Download

and follow the installation instruction here.


Gogs is nearly ready to run!

But before that, you need to configure some more things up.


2. Configure Gogs

a. Setup database (MySQL)

Create a MySQL database for Gogs.

$ mysql -uroot -p

Grant some priviliges to the created database.

mysql> DROP DATABASE IF EXISTS gogs;
mysql> CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> GRANT ALL ON gogs.* to 'gogs'@'localhost' identified by 'SOME_PASSWORD';

b. Edit configuration

Gogs needs some configuration before running.

It recommends (or forces? I’m not sure.) to keep custom config files rather than modifying the default ones directly.

So, duplicate the conf/app.ini file first.

$ cd $GOPATH/src/github.com/gogits/gogs

$ mkdir -p custom/conf
$ cp conf/app.ini custom/conf/app.ini

Now let’s edit the duplicated config file.

$ vi custom/conf/app.ini

(1) user and repository path

Change ROOT to the path of the repository directory which you created above:

[repository]
ROOT = /home/some_user/repositories

(2) database connection

Change username and password for database connection.

[database]
; Either "mysql", "postgres" or "sqlite3", it's your choice
DB_TYPE = mysql
HOST = 127.0.0.1:3306
NAME = gogs
USER = gogs
PASSWD = SOME_PASSWORD
; For "postgres" only, either "disable", "require" or "verify-full"
SSL_MODE = disable
; For "sqlite3" only
PATH = data/gogs.db

(3) run mode

If you want Gogs to be run in production mode, change it.

; Either "dev", "prod" or "test", default is "dev"
RUN_MODE = prod

(4) server configuration

Change domain, port, or other values.

[server]
PROTOCOL = http
DOMAIN = my-raspberrypi-domain.com
ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
HTTP_ADDR =
HTTP_PORT = 3000
; Disable SSH feature when not available
DISABLE_SSH = false
SSH_PORT = 22
; Disable CDN even in "prod" mode
OFFLINE_MODE = false
DISABLE_ROUTER_LOG = false
; Generate steps:
; $ cd path/to/gogs/custom/https
; $ ./gogs cert -ca=true -duration=8760h0m0s -host=myhost.example.com
;
; Or from a .pfx file exported from the Windows certificate store (do
; not forget to export the private key):
; $ openssl pkcs12 -in cert.pfx -out cert.pem -nokeys
; $ openssl pkcs12 -in cert.pfx -out key.pem -nocerts -nodes
CERT_FILE = custom/https/cert.pem
KEY_FILE = custom/https/key.pem
; Upper level of template and static file path
; default is the path where Gogs is executed
STATIC_ROOT_PATH =
; Application level GZIP support
ENABLE_GZIP = false
; Landing page for non-logged users, can be "home" or "explore"
LANDING_PAGE = home

You may also have to generate certificate files as the comment says:

$ mkdir -p cd $GOPATH/src/github.com/gogits/gogs/custom/https
$ cd $GOPATH/src/github.com/gogits/gogs/custom/https
$ ../../gogs cert -ca=true -duration=8760h0m0s -host=my-raspberrypi-domain.com

(5) secret key

Change the default secret key for security.

[security]
; !!CHANGE THIS TO KEEP YOUR USER DATA SAFE!!
SECRET_KEY = _secret_0123456789_

(6) app name

If you want to change the page titles…

; App name that shows on every page title
APP_NAME = Gogs on my Raspberry Pi

(99) others

There are other useful settings like email, oauth, webhook, … etc.

I will try them out soon. :-)


3. Run Gogs

All things up, now Gogs is ready to run!

A. Run in the shell

Gogs can be started from the shell.

$ cd $GOPATH/src/github.com/gogits/gogs
$ ./gogs web

If nothing goes wrong, you can connect to Gogs with any web browser.

Connect to the address, eg: http://my-raspberrypi-domain.com:3000.

You’ll meet Install Steps For First-time Run page demanding some check-ups.

When you see complaints about your settings, do as the error message suggests.

If all things are OK, you can login with the username and password which were set in the previous page.

B. Run as a service

You cannot run Gogs from the shell manually everytime, so let’s try to run it as a service.

(1) For init.d

Copy an init.d script file from the source codes:

$ sudo cp $GOPATH/src/github.com/gogits/gogs/scripts/init/debian/gogs /etc/init.d/gogs

and edit WORKING_DIR and USER,

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Go Git Service"
NAME=gogs
SERVICEVERBOSE=yes
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
WORKINGDIR=/home/some_user/gogs
DAEMON=$WORKINGDIR/$NAME
DAEMON_ARGS="web"
USER=some_user

and make it run automatically on boot time:

$ sudo chmod ug+x /etc/init.d/gogs

# if gogs has higher priority that database server, it would fail to launch on boot time
$ sudo update-rc.d gogs defaults 98

Gogs can now be manually started with sudo service gogs start and stopped with sudo service gogs stop.

(2) For systemd

Copy an systemd service file from the source codes:

$ sudo cp $GOPATH/src/github.com/gogits/gogs/scripts/systemd/gogs.service /lib/systemd/system/gogs.service

and edit/add corresponding values,

[Unit]
Description=Gogs (Go Git Service)
After=syslog.target
After=network.target
#After=mysqld.service
#After=postgresql.service
#After=memcached.service
#After=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
Type=simple
User=some_user
Group=some_group
WORKINGDIR=/home/some_user/gogs
ExecStart=/home/some_user/gogs/gogs web
Restart=always
Environment=USER=some_user HOME=/home/some_user

[Install]
WantedBy=multi-user.target

and make it run automatically on boot time:

$ sudo systemctl enable gogs.service

Gogs can now be manually started with sudo systemctl start gogs.serivice and stopped with sudo systemctl stop gogs.service.


Well done! :-D

99. Wrap-up

Gogs is not only a useful application, but also good to run on Raspberry Pis.

I decided to learn Golang for further use on Raspberry Pi :-)