How to Install Gogs on CentOS 7
In this guide, we will show you how to install Gogs on a CentOS 7 VPS with MariaDB as a backend database.
Gogs is a free and open-source self-hosted Git service written in the Go programming language. It’s easy-to-install, cross-platform, and lightweight, all of which allows Gogs to run on all sorts of hardware easily. This guide should work on other Linux VPS systems as well, but it was tested and written for CentOS 7. Let’s get started with the installation.
- A Server running CentOS 7
- A user account with sudo privileges, or access to the ‘root’ user itself
Step 1: Install Required packages
Log in to your VPS via SSH as the root account or as a sudo user:
ssh username@IP_Address -p Port_Number
Replace IP_Address and Port_Number with your server’s respective IP address and SSH port number.
Once logged in, run the following commands to update all packages to their latest available versions:
sudo yum update
Next, we need to install git. You can do that by using the following command:
sudo yum install git
Step 2: Create a MariaDB database
Gogs can use MySQL/MariaDB, PostgreSQL, MSSQL, TiDB, and SQLite3 databases as a database backend.
In this tutorial, we will use MariaDB. To install the latest MariaDB server execute the following command:
sudo yum install mariadb-server
When the MariaDB installation is complete, secure your MariaDB server using the mysql_secure_installation script. This part is optional, but strongly recommended as it improves the security of your database server:
Then, answer the security questions as follows. If the program asks for a MySQL root password before you have set it, press the [Enter] key, as the default root password is blank by default:
Set root password? [Y/n] Y Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Y Reload privilege tables now? [Y/n] Y All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
Log in to the MariaDB console with the MariaDB root user, using the password that you have set up in the previous step:
mysql -u root -p
Create a MariaDB database and user for Gogs, and grant permissions to the user using the following commands:
MariaDB [(none)]> CREATE SCHEMA `gogs` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; MariaDB [(none)]> GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY 'strongpassword'; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> quit;
Don’t forget to replace ‘strongpassword‘ with an actual strong password.
Step 3: Install Gogs
First, create a new system user for Gogs:
sudo adduser --home /opt/gogs --shell /bin/bash --comment 'Gogs application' gogs
Download the Gogs binary from the Gogs Github page using the following command (We’ve pre-inserted the link for the current latest version for you):
When the download is complete, extract the archive to the /opt/gogs directory:
tar xvf linux_amd64.tar.gz --strip-components=1 -C /opt/gogs
Change the ownership of the extracted directory to the Gogs user:
sudo chown -R gogs:gogs /opt/gogs/
Next, copy the systemd unit file:
sudo cp /opt/gogs/scripts/systemd/gogs.service /etc/systemd/system/
Open the unit file using your preferred text editor – we use nano.
sudo nano /etc/systemd/system/gogs.service
Once the file is open, edit it as follows:
[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=gogs Group=gogs WorkingDirectory=/opt/gogs ExecStart=/opt/gogs web Restart=always Environment=USER=gogs HOME=/opt/gogs # Some distributions may not support these hardening directives. If you cannot start the service due # to an unknown option, comment out the ones not supported by your version of systemd. ProtectSystem=full PrivateDevices=yes PrivateTmp=yes NoNewPrivileges=true [Install] WantedBy=multi-user.target
Save and close the file, then reload the systemd units:
sudo systemctl daemon-reload
Finally, start and enable the Gogs service:
sudo systemctl start gogs sudo systemctl enable gogs
Verify that the Gogs service is started successfully:
sudo systemctl status gogs
● gogs.service - Gogs Loaded: loaded (/etc/systemd/system/gogs.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2019-05-01 10:33:40 CDT; 3s ago Main PID: 1111 (gogs) CGroup: /system.slice/gogs.service └─1111 /opt/gogs/gogs web May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [ WARN] Custom config '/opt/gogs/custom/conf/app.ini' not found, ignore this if you're running first time May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [TRACE] Custom path: /opt/gogs/custom May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [TRACE] Log path: /opt/gogs/log May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [TRACE] Log Mode: Console (Trace) May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [ INFO] Gogs 0.11.86.0130 May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [ INFO] Cache Service Enabled May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [ INFO] Session Service Enabled May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [ INFO] SQLite3 Supported May 01 10:33:40 vps gogs: 2019/05/01 10:33:40 [ INFO] Run Mode: Development May 01 10:33:41 vps gogs: 2019/05/01 10:33:41 [ INFO] Listen: http://0.0.0.0:3000
Step 4: Set Up Gogs
Once the installation is complete, go to http://server_ip:3000/install and fill in all of the required fields:
– Database Type: MySQL
– Host: 127.0.0.1:3306
– User: gogs
– Password: strongpassword
– Database Name: gogs
General Settings of Gogs
– Application Name: Gogs
– Repository Root Path: /opt/gogs/gogs-repositories
– Run User: gogs
– Domain: SERVER_IP
– SSH Port: 22
– HTTP Port: 3000
– Application URL: http://SERVER_IP:3000/
– Log Path: /opt/gogs/log
Do not forget to replace “SERVER_IP” with your server’s public IP address.
Finally, click install and you’re good to go.
NOTE: Administrative access is automatically granted to the first registered user.
That’s it. You have successfully installed Gogs on your CentOS 7 VPS. For more information about how to manage your Gogs installation, please refer to the official Gogs documentation.