In this guide, we will explain how to install Gogs on an Ubuntu 18.04 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, it’s cross-platform, and it’s 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 an Ubuntu 18.04 VPS.
Table of Contents
Prerequisites
- A Server running Ubuntu 18.04
- 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 root or as a sudo user:
ssh userame@IP_Address
Once that is done, run the following commands to update all packages to their latest available versions:
sudo apt-get update sudo apt-get upgrade
Afterwards, we need to install git. You can do that by using the following command:
sudo apt-get install git
Step 2: Create MySQL database
Gogs can use MySQL, PostgreSQL, MSSQL, TiDB and SQLite3 databases for its database backend.
In this tutorial, we will use MySQL. Install the latest MySQL server by executing the following command:
sudo apt-get install mysql-server
When the MySQL installation is complete, secure your MariaDB installation using the mysql_secure_installation
script. This part is optional, but strongly recommended as it improves the security of your database server:
sudo mysql_secure_installation
Then, answer the security questions as follows:
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Log in to the MySQL console with the MySQL root user, using the password that you have set up in the previous step:
mysql -u root -p
Create a MySQL database and user for Gogs, and grant permissions to the user using the following commands:
mysql> CREATE SCHEMA `gogs` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY 'strongpassword'; mysql> FLUSH PRIVILEGES; mysql> \q
Make sure you 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 --gecos 'Gogs application' gogs
The following commands will download the Gogs binary from the Gogs Github page:
sudo wget https://github.com/gogs/gogs/releases/download/v0.11.86/linux_amd64.tar.gz
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 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] [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 start and enable the Gogs service:
sudo systemctl daemon-reload sudo systemctl start gogs sudo systemctl enable gogs
Verify that the Gogs service is started successfully:
● gogs.service - Gogs Loaded: loaded (/etc/systemd/system/gogs.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2019-04-07 12:25:32 CDT; 6min ago Main PID: 19220 (gogs) Tasks: 8 (limit: 2320) CGroup: /system.slice/gogs.service └─19220 /opt/gogs/gogs web Apr 07 12:25:32 localhost systemd[1]: Started Gogs. Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [ WARN] Custom config '/opt/gogs/custom/conf/app.ini' not found, ignore this if you're running first time Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [TRACE] Custom path: /opt/gogs/custom Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [TRACE] Log path: /opt/gogs/log Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [TRACE] Log Mode: Console (Trace) Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [ INFO] Gogs 0.11.86.0130 Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [ INFO] Cache Service Enabled Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [ INFO] Session Service Enabled Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [ INFO] SQLite3 Supported Apr 07 12:25:32 localhost gogs[19220]: 2019/04/07 12:25:32 [ INFO] Run Mode: Development Apr 07 12:25:33 localhost gogs[19220]: 2019/04/07 12:25:33 [ 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. Remember to replace “server_ip” with your server’s public IP address:
Database Settings
– 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
Finally, click install and you’re good to go.
Administrative access is automatically granted to the first registered user:
That’s it. You have successfully installed Gogs on your Ubuntu 18.04 VPS. For more information about how to manage your Gogs installation, please refer to the official Gogs documentation.
Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post, please share it with your friends on the social networks using the share shortcuts below, or simply leave a comment down in the comments section. Thanks.