ManagedCloud Servers

High performance handled and monitored by us 24/7/365. A complete solution to provide you with our in house expertise 24/7 tailored to your specific needs. We'll setup a bespoke server for your site using the latest tehnologies so you can get the most out of your hardware and get your website loading quickly and reliably. Find out more..

cPanelCloud Servers

Recommended - High performance cloud servers with no technical knowledge required. If you're hosting multiple websites already and you're looking to consolidate, or if you're looking to isolate yourself from the shared hosting environment but you don't have the time or knoweldge to manage a server, then the Managed cPanel Servers are for you. Find out more..

UnmanagedCloud Servers

Our unmanaged range gives you complete control at rock bottom prices and our cloud platform boasts super fast multipath 40Gb/s network, the latest Intel Xeon V3 CPUs and enterprise grade redundant SSDs. If you're a sysadmin look no further, we offer some of the best specification to price ratio servers available. Find out more..

Want your very own server? Get our 1GB memory, Xeon V4, 20GB SSD VPS for £10.00 / month.

View Plans

How to Install Gogs on CentOS 7

Gogs is a self-hosted git service, similar to Gitlab. It is written in Go and requires much less hardware compared to Gitlab. The goal of Gogs is to provide the easiest, fastest, and most painless way of setting up a self-hosted Git service. As this application is written in Go, it supports all platforms like Linux, Windows, OS X and ARM.

Features of Gogs:

  • Easy to install and supports multiple platforms.
  • Minimum hardware requirement.
  • It is completely free and open source.
  • Supports SSH and HTTP/HTTPS protocols.
  • Supports multiple authentication protocols.
  • Multilingual support.
  • Mail service and Administration panel.
  • Repository web-hooks and Git hooks.
  • Repository issues, pull requests and wiki supports web editor for repository files and wiki.
  • Supports MySQL, PostgreSQL, SQLite3 and TiDB.

Requirements

Gogs supports many operating systems but in this tutorial we are going to install Gogs in CentOS 7.x.

Requirements:

  • VPS, Cloud or Dedicated Server running CentOS 7.x
  • A Simple Raspberry Pi board is enough to support basic functionality.
  • Recommended configuration would be a 2 core CPU and at least 1 GB RAM.

Apart from this we will need to install all the required dependencies in the tutorial. Additionally you can always increase the CPU cores and memory to add support for more users.

You can use the IP address of your server to install and set up Gogs, but it is recommended to use a FQDN. In this tutorial we will be using git.example.com. To use a domain name with Gogs, make necessary DNS changes to point your domain or subdomain to the IP address of your server. You will also need root access to your server. If you are logged in as non root user, run sudo -i to switch as root user.

Install Gogs

Gogs requires few dependencies in order to work, which are an SQL server, git and SSH server. We can use either MySQL or PostgreSQL server as SQL server. It also supports SQLite but SQLite is not recommended for production use. In this tutorial we will be installing MySQL.

Before installing any package it is recommended to update your system and packages. Run the following command to update your system.

    yum -y update

Now setup the hostname for your server. For the ease of identification, you may want it to be same as your domain name.

    hostnamectl set-hostname git.example.com

You can replace git.example.com with anything you want. You will also need to disable your SELinux. Run the following command to do so.

    sed -i /etc/selinux/config -r -e 's/^SELINUX=.*/SELINUX=disabled/g'

You will need to reboot your server for SELinux changes to take effect. Run systemctl reboot to reboot your server.

Install and setup MySQL

MySQL does not comes with the default YUM repository of CentOS, hence you will need to add the MySQL repository in your server. The following commands will add MySQL repositories in your server.

    wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
    rpm -ivh mysql57-community-release-el7-8.noarch.rpm
    yum -y update

Now install MySQL server on your machine using the following command.

    yum -y install mysql-server

Now start MySQL server and enable it to automatically start at boot time using following commands.

    systemctl start mysqld
    systemctl enable mysqld

During installation the root user of MySQL is assigned a random password. To fetch the password run the following command.

    grep 'temporary password' /var/log/mysqld.log

You will see an output similar to the one shown below. The output should contain your temporary root password.

    [root@git ~]# grep 'temporary password' /var/log/mysqld.log
    2016-09-10T07:24:29.105209Z 1 [Note] A temporary password is generated for root@localhost: i,DtiXeP!1EG

Now you will need to secure your MySQL installation using the following command.

    mysql_secure_installation

You will be asked for your MySQL root password. Enter your temporary password. Once logged in, change the password for root user. You will be prompted with few more questions. Recommended is that you answer y for all questions. The output will look like shown below.

    [root@git ~]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root.

Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y

New password:

Re-enter new password:

Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y Success.

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y Success.

By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y - Dropping test database... Success.

- Removing privileges on test database... Success.

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y Success.

All done!

Now login to your MySQL command line using the following command and providing your new MySQL root password.

    mysql -u root -p

Then execute the following queries in MySQL command line.

    CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8 COLLATE utf8_general_ci;
    use gogs;
    SET default_storage_engine=INNODB;

You will see following output for these commands.

    [root@git ~]# mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 14
    Server version: 5.7.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 sec)

mysql> use gogs; Database changed mysql> SET default_storage_engine=INNODB; Query OK, 0 rows affected (0.00 sec)

mysql>exit Bye

As we have our database ready, we can now move to next step.

Install Git

Gogs required to git for version control, you can get git using the following command.

    yum -y install git

You can check your Git version using the following command.

    git --version

You should get similar output.

    [root@git ~]# git --version
    git version 1.8.3.1

Install Gogs

You can install Gogs in many different ways but the easiest way to install Gogs is using Packager Repository. Run the following commands to add Packager.io Gogs repository in your server.

    rpm --import https://rpm.packager.io/key
    echo "[gogs]
    name=Repository for pkgr/gogs application.
    baseurl=https://rpm.packager.io/gh/pkgr/gogs/centos7/pkgr
    enabled=1" | tee /etc/yum.repos.d/gogs.repo

Now you can install Gogs, using the following command.

    yum -y install gogs

Once the installation finishes you can starts Gogs using the following command.

    systemctl start gogs

To enable Gogs to start at boot time, run the following command.

    systemctl enable gogs

To check the status of Gogs service, run the following command.

    systemctl status gogs

If service is running you will get output similar to shown below.

    [root@git ~]# systemctl status gogs
    ● gogs.service
       Loaded: loaded (/etc/systemd/system/gogs.service; enabled; vendor preset: disabled)
       Active: active (running) since Sat 2016-09-10 08:34:53 UTC; 6min ago
     Main PID: 5760 (sleep)
       CGroup: /system.slice/gogs.service
               └─5760 /bin/sleep infinity

Sep 10 08:34:53 git.batchbits.com systemd[1]: Started gogs.service. Sep 10 08:34:53 git.batchbits.com systemd[1]: Starting gogs.service... Sep 10 08:36:56 git.batchbits.com systemd[1]: Started gogs.service.

By default Gogs service runs on port 3000, you can access the web installer for Gogs on http://your-server-IP:3000 or http://Your-Domain.com:3000. Please do not make any changes to this page as we are going to run nginx as the reverse proxied web server, and Gogs web server will run behind the scenes.

Install and Configure Nginx

Nginx does not come with the default YUM repository, hence you will need to add EPEL repository to your server. Run the following command to do so.

    yum -y install epel-release
    yum -y update
    yum clean all

Now you can install nginx using the following command.

    yum -y install nginx

Now start nginx and enable it start at boot time using the following command.

    systemctl start nginx
    systemctl enable nginx

Now you will need to create a virtual host file for your nginx reverse proxy.

    mkdir /etc/nginx/conf.d
    nano /etc/nginx/conf.d/gogs.conf

You can also use any editor of your choice. Put the following content in the new file.

    server {
        listen 80;
        server_name your_domain_or_server_ip;

proxy_set_header X-Real-IP $remote_addr;

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

Replace your_domain_or_server_ip with your domain or the server IP. You can also put your domain name and IP address both, separated by a space to enable the access using both domain name and IP address.

Now restart your nginx server using the following command.

    systemctl restart nginx

Now you will be able to access the site by browsing the following URL using your favorite browser. If you are using your domain name name then your site is accessible on http://your.domain.com if you are using IP address, then it will be accessable on http://your-IP-address

If you wish to use HTTPS instead of Plain HTTP, you can either generate a self signed certificate or purchase a commercial SSL certificate. You can also use Certbot or Let's Encrypt SSL to generate free SSL for your domain.

Download Certbot script using the following command.

    wget https://dl.eff.org/certbot-auto
    chmod +x certbot-auto 
    mv certbot-auto /usr/bin/certbot-auto

Now we can directly run certbot-auto from terminal. Before we can generate SSL to use with nginx we will need to stop nginx server. Run the following command for same.

    systemctl stop nginx

To generate a SSL certificate for your domain run the following command.

    certbot-auto certonly --standalone -d git.example.com

Replace git.example.com with your domain name. To generate SSL for your domain, you must have the DNS configured which points your domain to the IP address of the server. If you do not have your domain configured, you can stay with HTTP also.

You will be asked to enter your email address and accept the terms and conditions. Finally you will see message similar to this.

    [root@ip-172-31-17-38 ~]# certbot-auto certonly --standalone -d git.example.com

IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/git.example.com/fullchain.pem. Your cert will expire on 2016-12-09. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto renew" - If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

Once done you can configure nginx to use SSL certificates by editing the virtual host configuration file using the following command.

    nano /etc/nginx/conf.d/gogs.conf

Now replace the existing contents with the new content shown below.

    server {
        listen 80;
        server_name git.example.com;
        return 302 https://$server_name$request_uri;
    }

server { listen 443 ssl; server_name git.example.com;

ssl_certificate /etc/letsencrypt/live/git.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/git.example.com/privkey.pem;

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

Replace git.example.com with your domain name and also change the path of SSL certificates as required. Once done start your nginx server using the following command.

    systemctl start nginx

Installation Using Web Installer

Now you can browse your site on https://your-domain.com. Once you browse your site you will see following screen.

In Database Setting choose the database type, in this case it will be MySQL. The host should not be changed, and as we have used root user to create MySQL database, the user should be root. Enter the password of your MySQL root user. Database name should also be left as gogs.

In Application General Settings, provide a name for your Application, you can give the name of your organisation. Provide a path for your repositories. You can choose to leave it as it is or you can also change the path as required. Make sure that the directory is under Gogs home directory which is /home/gogs/. All the git remote repositories will be saved here.

In Run As option choose the user which runs Gogs, also has access to the repository root path. In our case, it should be left as gogs. Now provide the domain name for your repository. This domain name will be used when cloning via SSH. Do not include http:// or www in this domain name. Provide a port for SSH, it should be left as 22 unless changed. Provide a port on which the application will listen, it should be left as 3000. Now provide the HTTP Application url of your Git server. It should be in https://git.example.com format. Choose a path to store the logs.

Furthermore you can configure the SMTP settings to that Gogs can send email notification to the user. If you do not have SMTP credentials you can skip this configuration.

In Admin Account Settings, provide a username for administrator, it must be something other than admin. Provide the password for the administrator account twice and provide a email for admin account.

Finally click on Install Gogs button to launch the web installer. Once the installation finishes you will be automatically logged into the dashboard.

Using Gogs

Once you are logged in you can create repository by click on Add icon and selecting New Repository option.

In New Repository interface, provide the required information and press Create Repository button.

Once the repository has been created you will be taken to the dashboard for your new repository.

From here you can see the URL by which the repository can be cloned anywhere. You can also clone it on your system by running the following command.

    git clone https://git.example.com/liptan/testrepo.git

Change the Repository URL according to the URL generated.

From the dashboard you can create a new file or upload an existing file into the repository. You can also edit the files using the web based interface by clicking on the name of the file.

Conclusion

In this tutorial we have learnt to install Gogs on CentOS 7.x, You can now easily host a private Git repository on your server even if do not have huge resources. You can also easily create repositories and collaborate with others. If you want more features then Gogs provides, you can install GitLab on your CentOS server.

Want your very own server? Get our 1GB memory, Xeon V4, 20GB SSD VPS for £10.00 / month.

View Plans