How to Install Gogs Go Git Service on CentOS 7

Gogs is free and open source Git service written in Go language. It's is a painless, self-hosted git service that allows you to create and run your own Git server on the minimal hardware server. Gogs web-UI is very similar to GitHub, and offers support for MySQL, PostgreSQL, and SQLite database.

In this tutorial, we will show you step-by-step how to install and configure your own Git service using Gogs on the CentOS 7 server. This tutorial will cover topics including, Go installation on the CentOS 7 system, PostgreSQL installation, and installation/configuration of Nginx web server as a reverse proxy for Go application.

Prerequisites

  • CentOS 7 Server
  • Root privileges

What we will do

  1. Install EPEL Repository
  2. Install and Configure PostgreSQL Database
  3. Install Go and Git
  4. Install Gogs Go Git Service
  5. Configure Gogs Go Git Service
  6. Run Gogs as a Service on CentOS 7
  7. Install and Configure Nginx as a Reverse Proxy for Gogs
  8. Test the setup

Step 1 - Install EPEL Repository

The first step we will do in this guide is to install the EPEL repository. Install EPEL repo using the command below.

sudo yum -y install epel-release

EPEL Repository has been added to the CentOS 7 server.

Step 2 - Install and Configure PostgreSQL Database

Gogs offers support for MySQL, PostgreSQL, SQLite3, MSSQL, and the TiDB database systems. In this guide, we will be using PostgreSQL as a database for our Gogs installations.

Install the PostgreSQL database using the yum command below.

sudo yum -y install postgresql-server postgresql-contrib

After the installation is complete, we need to initialize the database and configure PostgreSQL.

Run the command below.

sudo postgresql-setup initdb

Next, we will configure PostgreSQL to run under local IP address '127.0.0.1' and allow all users on the local host address to login with md5 authentication.

Login as the Postgres user.

su - postgres

Now go to the 'data' directory and edit the 'postgresql.conf' file using vim.

cd data/
vim postgresql.conf

Uncomment the 'listen_addresses' line and change the value to '127.0.0.1'.

listen_addresses = '127.0.0.1'

Save and exit.

Edit 'pg_hba.conf' for authentication configuration.

vim pg_hba.conf

On the local '127.0.0.1/32' host configuration, change the authentication method to 'md5' as below.

host    all             all             127.0.0.1/32            md5

Save and exit.

Now start the PostgreSQL service and enable it to launch everytime at system boot.

sudo systemctl start postgresql
sudo systemctl enable postgresql

PostgreSQL database has been installed on the CentOS 7 server - check it using the netstat command.

netstat -plntu

And make sure you see PostgreSQL is running on local IP address '127.0.0.1' with port 5432.

Next, we will create a new database for Gogs installation. We will create a new database named 'gogs_production' with user 'git'.

Login as the Postgres user and access the psql shell.

su - postgres
psql

Create new 'git' user using the command below.

CREATE USER git CREATEDB;
\password git

Now create the database 'gogs_production' and set the owner to 'git' user.

CREATE DATABASE gogs_production OWNER git;
exit

The PostgreSQL database for Gogs installation has been created.

Step 3 - Install Go and Git

Install Git from the repository using the yum command below.

sudo yum -y install git

Now add new user 'git' to the system.

useradd -m -s /bin/bash git
passwd git

Login to the 'git' user and create new 'local' directory.

su - git
mkdir -p /home/git/local

Go to the 'local' directory and download the latest Go version using the wget command.

cd ~/local
wget https://dl.google.com/go/go1.9.2.linux-amd64.tar.gz

Extract the go compressed file, then remove the archive file.

tar -xf go1.9.2.linux-amd64.tar.gz
rm -f go1.9.2.linux-amd64.tar.gz

'Go' binary files have been downloaded in the '~/local/go' directory. Now we need to setup the environment, so we need to define the 'GOROOT' and 'GOPATH directory in order to run a go command in the system as the 'git' user.

Run commands below.

cd ~/
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

And reload the bash by running 'source ~/.bashrc' command as below.

source ~/.bashrc

Make sure you're using bash as your default shell.

Now run the go command to check the version.

go version

And make sure you get the result as shown below.

Go is now installed on the system under the 'git' user.

Step 4 - Install Gogs Go Git Service

Login as the 'git' user and download 'Gogs' from GitHub using the go command as shown below.

su - git
go get -u github.com/gogits/gogs

The command will download all Gogs source code to the 'GOPATH/src' directory.

Go to the '$GOPATH/src/github.com/gogits/gogs' directory and build gogs using commands below.

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

And make sure you get no error.

Now run Gogs Go Git Service using the command below.

./gogs web

The command will run Gogs on the public IP address with default port 3000.

Open your web browser and type your server IP address with port 3000.

http://192.168.33.10:3000/

And you should get the result as shown below.

Gogs is now installed on the CentOS 7 system. Now back to your terminal and press 'Ctrl + c' to exit.

Step 5 - Configure Gogs Go Git Service

In this step, we will create a custom configuration for Gogs.

Go to the gogs installation directory and create a new 'custom/conf' directory.

cd $GOPATH/src/github.com/gogits/gogs
mkdir -p custom/conf/

Copy default configuration to the custom directory and edit it using vim.

cp conf/app.ini custom/conf/app.ini
vim custom/conf/app.ini

In the '[server]' section, change the server 'HOST_ADDR' to '127.0.0.1'.

[server]
PROTOCOL = http
DOMAIN = localhost
ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
HTTP_ADDR = 127.0.0.1
HTTP_PORT = 3000

In the '[database]' section, change everything with your own database info.

[database]
DB_TYPE = postgres
HOST = 127.0.0.1:5432
NAME = gogs_production
USER = git
PASSWD = [email protected]

Save and exit.

Now verify the configuration by running the following command.

./gogs web

And make sure you get the result as below.

Gogs is now running with our custom configuration, and under local IP address '127.0.0.1' with port 3000.

Step 6 - Running Gogs as a Service on CentOS 7

In this step, we will configure Gogs as a service on the CentOS 7 system. We will create new service file configuration 'gogs.service' under the '/etc/systemd/system' directory.

Go to the '/etc/systemd/system' directory and create a new service file 'gogs.service' using the vim editor.

cd /etc/systemd/system
vim gogs.service

Paste the following gogs service configuration there.

[Unit]
Description=Gogs
After=syslog.target
After=network.target
After=mariadb.service mysqld.service postgresql.service memcached.service 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=git
Group=git
WorkingDirectory=/home/git/go/src/github.com/gogits/gogs
ExecStart=/home/git/go/src/github.com/gogits/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git

[Install]
WantedBy=multi-user.target

Save and exit.

Now reload systemd services.

systemctl daemon-reload

Start gogs service and enable it to launch everytime at system boot using the systemctl command.

systemctl start gogs
systemctl enable gogs

Gogs is now running as a service on the CentOS 7 system.

Check it using the following commands.

netstat -plntu
systemctl status gogs

And you will get the result as shown below.

Step 7 - Install and Configure Nginx as a Reverse Proxy for Gogs

In this step, we will install and configure Nginx as a reverse proxy for Gogs. We will install Nginx packages from the EPEL repository.

Run the yum command below to install Nginx.

yum -y install nginx

After the installation complete, we need to create new virtual host configuration for Gogs.

So go to the '/etc/nginx' configuration directory and create a new virtual host file 'gogs' under the 'conf.d' directory.

cd /etc/nginx/
vim conf.d/gogs.conf

Paste the following configuration there.

server {
    listen 80;
    server_name git.hakase-labs.co;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

Save and exit.

Next, we need to test nginx configuration and make sure there is no error

nginx -t

Start the Nginx service and enable it to launch at system boot.

systemctl restart nginx
systemctl enable nginx

Nginx web server is now installed - check it using the netstat command below.

netstat -plntu

and you will get the HTTP port 80 on the list.

Step 8 - Testing

Open the web browser and type your Gogs installation URL on the address bar.

http://git.hakase-labs.co/

On the top page, type all of your PostgreSQL database info.

Now scroll to the bottom page - click the 'Admin account settings' dropdown.

Type your admin user, password, and email.

Then click the 'Install Gogs' button.

And you will be redirected to the Gogs user Dashboard as shown below.

Below is Gogs 'Admin Dashboard'.

Gogs is now installed with PostgreSQL database and Nginx web server on the CentOS 7 server.

Reference

https://gogs.io/docs/installation

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment