Gogs is a painless self-hosted Git service. It is similar to GitHub, Bitbucket, and GitLab. The goal of the project is to provide the easiest, fastest, and most painless way of setting up a self-hosted Git service. With Go, this can be done with an independent binary distribution across all platforms and architectures that Go supports. This support includes FreeBSD, Linux, macOS, and Windows, on architectures like amd64, i386, ARM, PowerPC, and others.
Today I will cover installing Gogs on FreeBSD. I will look at setting up Apache to serve Gogs via a Proxy and also cover SSL Certificates.
So without further ado, let’s start to setup up our own web-facing code repository.
Whilst Gogs will serve pages out onto the web via its own daemon, I have chosen to serve Gogs via a Proxy in Apache. Therefore I have chosen to install the apache24 package and will demonstrate later the setup of serving Gogs via Proxy.
Gogs has the option to use SQLite, MySQLite or PostgreSQL. As I am already running PostgreSQL on my development host I have chosen this and will demonstrate installation with PostgreSQL.
# pkg install apache24 go gogs postgresql11-server
Add the following to /etc/rc.conf to allow these services to run when it is time to start them.
# echo 'gogs_enable="YES"' >> /etc/rc.conf # echo 'postgresql_enable="YES"' >> /etc/rc.conf # echo 'apache24="YES"' >> /etc/rc.conf
This is all that is needed, to get Gogs going. I have not added any SSL Cert Foo here as Apache will be running with SSL certificates and will serve Gogs securely. The below configuration serves Gogs only on the local interface, which means no users will be able to access the services. This is fine in this case, and even desired as the Apache Proxy will be the only service that needs to connect with Gogs on 127.0.0.1. Apache will serve Gogs via Proxy to all other hosts.
# vi /usr/local/etc/gogs/conf/app.ini [server] PROTOCOL = http DOMAIN = localhost ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/gogs HTTP_ADDR = 127.0.0.1 HTTP_PORT = 3000
Note: The Root URL, we serve Gogs from is FQDN/gogs, so as to avoid Gogs conflicting with any pages you may have at your root web directory.
The following takes care of Initializing and starting the PostgreSQL service.
# service postgresql initdb # service postgresql start
The normal PostgreSQL administration procedure is to first switch to the postgres user, before starting the psql client command.
# su - postgres $ psql psql (11.6) Type "help" for help. postgres=#
Create Gogs User and Database for PostgreSQL
postgres=# create user gogs WITH PASSWORD 'secret'; CREATE ROLE postgres=# create database gogs; CREATE DATABASE postgres=# grant ALL on DATABASE gogs to gogs; GRANT postgres=# postgres=# \q $ ^D
We now have everything going to start Gogs, however, Gogs will only be served on 127.0.0.1. As my development host does not have a GUI, there will be no way to access the Gogs server. I could have easily set Gogs to listen on 0.0.0.0 and this would have served to all hosts, however I prefer to serve Gogs via Apache Proxy as this is a more robust solution.
To accomplish serving via Proxy, follow the next steps.
Enable Apache Proxy Modules
Uncomment the following lines from /usr/local/etc/apache24/httpd.conf.
#LoadModule proxy_module libexec/apache24/mod_proxy.so #LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so
Add Proxy Config to Apache VirtualHost
On the Apache VirtualHost that you want to serve the Gogs server from, add the following directives.
... <Proxy /gogs> Order allow,deny Allow from all </Proxy> ProxyPass /gogs http://127.0.0.1:3000 ProxyPassReverse /gogs http://127.0.0.1:3000 ....
Serving Gogs Securely on Port 443
You do not need to make any special settings for Gogs to be served securely as Apache is serving all Gogs content. However, you will need to set up the likely directives to enable SSL in the VirtualHost that Gogs is being served from. You can read my post on Apache SSL Certificates if you need assistance with this.
Starting Gogs & Apache
Now you can start Gogs and Apache.
service gogs start service apache24 start
Finish Gogs Install via Web Interface
To access the Gogs server, go to https://your-apache-server/gogs. You need to finish the install process via the web interface. You should see a screen similar to below.
It is sufficient just to add the database details, that you used when you setup the PostgreSQL server.
After clicking Install Gogs you will be bounced to localhost:3000/gogs/user/login but this is easily corrected by putting in the correct address that is served by Apache, https://your-apache-server/gogs/user/login.
You should now have a working Gogs server on your host. You can check out further docs at the Gogs website.