Install Gogs on 16.04

My goal is trying to setup a git server along side with one of my existing sites. Say my site has a domain name example.com. I want to setup a git server at https://git.example.com. 

I chose Gogs as it is quite light-weight and it appears to be sufficient for me.

For security purpose, I tried to forward all http traffics to https and signed the site with letsencrypt. So I have setup a reverse proxy from say https://git.example.com to http://127.0.0.1:3000 (3000 is the default port used by gogs).

Warning: note that below is kind of my personal note and does not include all steps in detail. Just giving a rough idea of what is supposed to go through. Please just take this as reference but do not try to follow it step by step.

Prerequisite

sudo apt install git # install git

Create Git user

sudo adduser git

Setup MySQL

It is recommended to use mysql.sql script here. Simply run

wget https://github.com/gogits/gogs/raw/master/scripts/mysql.sql
mysql -u root -p < mysql.sql

root is a default username. You may want to change to another one as needed.

Download and “install” Gogs

Grab the binary from here. For example, try

su git # need to run it under user git
wget https://dl.gogs.io/0.11.43/gogs_0.11.43_linux_amd64.zip
unzip gogs_0.11.43_linux_amd64.zip
cd gogs
./gogs web

Gogs is supposed to run on port 3000 by default. You may try to access it now at http://example.com:3000.

Install and setup Nginx

sudo apt install -y nginx

Then add the following to /etc/nginx/conf.d/default.conf

server { 
  listen 80; 
  server_name git.example.com; 
  location / { 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_pass http://localhost:3000$request_uri;
    proxy_redirect off; 
  } 
}

You may want to change the server_name to your actual server name though. And be sure to setup the subdomain (cname) “git.example.com” with your Internet domain registrar. Note that to avoid git push error for large submit (Github Push Error: RPC failed; result=22, HTTP code = 413), simply add

client_max_body_size 50m; 

inside the html block of nginx config file (/etc/nginx/nginx.conf).

Test nginx config by running

sudo nginx -t

and restart the service

sudo service nginx restart

Secure the site

Following this to sign the site,

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com -d git.example.com

Check nginx configure file again with

sudo nginx -t

I came across an error complaining duplicate listen options for [::]:443. This can be resolved by removing ipv6only in nginx configuration files.

Run Gogs as service

Once you logout from your server, the gogs service will stop since it is running in the console. A daemon script already came with gogs. Simply copy it to /etc/systemd/system and run it as follows

sudo cp /home/git/gogs/scripts/systemd/gogs.service /etc/systemd/system
sudo systemctl start gogs.service

 

Update (2019/3):

Try to migrate gogs to a new server. Can use backup and restore. See this.

When reinstall mysql, may come up weird sudo problem. See this to solve it.

Leave a Reply

Your email address will not be published. Required fields are marked *