Gogs is a self-hosted Git service written in Go. It aims to provide a simple, stable, and extensible solution for managing Git repositories. In this tutorial, we will walk you through the steps to install Gogs on a Debian 12 server. Gogs is known for its excellent performance and low resource usage, making it an ideal choice for small to medium-sized projects.
Before we begin, make sure you have full SSH root access or a user with sudo privileges. Additionally, Gogs supports various databases, including SQLite3, PostgreSQL, MySQL, and MariaDB. In this tutorial, we will be using MariaDB as the database server.
Prerequisites
Let’s start by updating the software packages on your Debian 12 server. Open a terminal and run the following commands:
sudo apt update sudo apt upgrade
Install MariaDB Database Server
Gogs requires a database server to store its data. We will be using MariaDB for this purpose. Install MariaDB by running the following command:
sudo apt install mariadb-server mariadb-client -y
Once the installation is complete, check the status of the MariaDB service:
sudo systemctl status mariadb
You should see the output indicating that the MariaDB service is active and running.
Next, secure the MariaDB installation by running the following command:
sudo mysql_secure_installation
This command will prompt you to set a root password and perform other security-related configurations for your MariaDB installation.
Create a Database and User for Gogs
Now, let’s create a database and user specifically for the Gogs project. Start by logging into the MariaDB server as the root user:
sudo mariadb -u root -p
Enter your root password when prompted. Once logged in, execute the following commands to create a database called “gogs” and grant all privileges to a user named “gogs”:
CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY 'StrongPassword'; FLUSH PRIVILEGES; EXIT;
Make sure to replace “StrongPassword” with a secure password of your choice.
Download and Install Gogs
Now, let’s download and install Gogs from their official GitHub repository. Start by installing the curl utility if it is not already installed:
sudo apt install curl -y
Next, use curl to download the latest release of Gogs:
curl -s https://api.github.com/repos/gogs/gogs/releases/latest | grep browser_download_url | grep 'linux_amd64.tar.gz' | cut -d '"' -f 4 | wget -i-
After the download is complete, extract the Gogs archive:
tar xvf gogs_*_linux_amd64.tar.gz
Create a Dedicated User for Gogs
To enhance security and separation of concerns, we will create a dedicated user for running Gogs. Run the following command to add a new user named “git”:
sudo adduser git
You will be prompted to set a password for the new user. Make sure to choose a strong password.
Configure Gogs as a Systemd Service
To manage the Gogs service, we will create a systemd unit file. Start by creating a dedicated logs directory for the Gogs user:
sudo mkdir /var/log/gogs sudo chown -R git:git /var/log/gogs
Next, copy the Gogs systemd service file to the appropriate directory:
sudo cp gogs/scripts/systemd/gogs.service /etc/systemd/system/
Open the Gogs service file for editing:
sudo nano /etc/systemd/system/gogs.service
In the editor, locate the line starting with ExecStart= and modify it to specify the web port on which Gogs should listen. For example, to use port 3001, update the line as follows:
ExecStart=/home/git/gogs/gogs web -port 3001
Save the changes and exit the editor.
Now, move the Gogs binary file to the home directory of the git user:
sudo mv gogs/home/git/ sudo chown -R git:git/home/git/
Start and Enable the Gogs Service
Now that everything is set up, we can start and enable the Gogs service. Run the following commands to reload the systemd daemon, start the Gogs service, and enable it to run on boot:
sudo systemctl daemon-reload
sudo systemctl start gogs
sudo systemctl enable gogs
To verify that the Gogs service is running, use the following command:
sudo systemctl status gogs
Configure Gogs through the Web Interface
At this point, Gogs should be up and running on your Debian 12 server. To access the Gogs web interface, open a web browser and enter the IP address or domain name of your server followed by the port number specified in the Gogs configuration. For example, if your server’s IP address is 192.168.0.100 and the port is set to 3001, you would enter http://192.168.0.100:3001 in the address bar.
You will be greeted with the Gogs installation screen. Follow the steps below to configure Gogs:
- Database Settings: Enter the database information you created earlier, including the database name, username, password, and server address.
- Application General Settings: Provide the application name, the user you created for Gogs (git), the domain name associated with the application, and the application URL. Make sure to use a proper domain name or IP address.
- Optional Settings: Enable or disable any additional settings based on your requirements.
- Create Admin Account: Create an admin account that you will use to access Gogs.
Once you have completed the setup, click on the “Install Gogs” button to start the installation process. After the installation is complete, you will be redirected to the login screen.
Login with the admin account you created earlier and start using Gogs to manage your Git repositories.
Conclusion
Congratulations! You have successfully installed Gogs on your Debian 12 server. Gogs provides a lightweight and efficient solution for self-hosted Git services, allowing you to manage and collaborate on your code repositories with ease.
Remember to keep your Gogs installation up to date by regularly checking for updates and applying them as necessary. Additionally, make sure to follow security best practices to protect your Gogs instance and the data it contains.
If you’re looking for reliable and scalable cloud hosting solutions, consider Shape.host. They offer Linux SSD VPS services that are perfect for hosting applications like Gogs. Visit their website at Shape.host to learn more about their services.