How to Install Gogs on CentOS 7 to Build Your Own Git Repository

gogscentos 7self-hosted gitnginx reverse proxygit repository
Published·Modified·

Gogs is an extremely easy-to-deploy self-hosted Git service. Compared to the bloated GitLab, Gogs is much lighter, requires fewer resources, and can even be installed on Raspberry Pi with ARM architecture. The setup process is also much simpler than GitLab, making it more suitable for individuals or small teams.

Gogs Interface

1. Create MySQL Database

When creating the database, it is recommended to set the authorization address to 127.0.0.1 and grant all permissions to databases starting with the username (username_%). The following screenshot demonstrates this using phpMyAdmin.

Database Configuration

2. Binary Installation

# Download the 64-bit binary package
wget http://7d9nal.com2.z0.glb.qiniucdn.com/0.11.4/linux_amd64.tar.gz
# Extract the package
tar -zxvf linux_amd64.tar.gz
# Enter the installation directory
cd gogs
# Run Gogs
./gogs web
# Open the port, if using iptables
/sbin/iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
service iptables save
service iptables restart

After running the commands above, allow port 3000 through the firewall. If there are no errors, access http://IP:3000 to start the installation. When configuring the database during installation, refer to the steps above for creating the database; otherwise, the connection may fail.

3. Nginx Reverse Proxy

Accessing via http://IP:3000 every time is inconvenient. You can use a reverse proxy to enable domain access. Below is a personal reverse proxy configuration (with HTTPS enabled), which is generally added to vhost/xxx.conf and then Nginx is restarted.

server
    {
    listen 443 ssl http2; # Listen on port 443 and enable HTTP/2
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Allowed protocols
    # SSL certificate path
    ssl_certificate /data/ssl/xiaoz.top/xiaoz.top.pem;
    ssl_certificate_key /data/ssl/xiaoz.top/xiaoz.top.key;
    server_name     code.xiaoz.top;

    charset utf-8,gbk;
        location / {
           proxy_pass http://localhost:3000;
           proxy_redirect off;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# 301 redirect, http to https
server
{
        listen 80;
        server_name code.xiaoz.top;
        rewrite ^(.*) https://code.xiaoz.top$1 permanent;
}

Note: If you enable HTTPS, you must also modify custom/conf/app.ini to change the ROOT_URL to the HTTPS address.

4. Summary

Gogs installation is very simple (assuming you have a Linux foundation), and the official documentation is also very detailed. GitHub is undoubtedly an excellent third-party Git repository, but GitHub's private repository is a paid service, and it can be unstable in China. If you are interested, you might as well try setting up Gogs yourself.

Gogs Official Website: Gogs Demo: Zcode