Hosting Your Own Git Server On Your VPS With Gogs
From its humble beginnings as a side project to providing a version control and source code management system for any team working on the Linux kernel, Git has become one of the most popular version control systems in recent years. A handy feature of creating a Git server is the ability for multiple users to work on the same files, synchronizing the data through Git on a server to work collaboratively. Although the most common use is developers working on source code. Git is also used for working on books, dissertations, research or even server configuration files.
Github provides a great service to use as a server for your projects that are tracked using Git. Unfortunately, it’s only free for public or open source projects. If you want to do a private project then you’ll need to pay per user for the service. Fortunately, Git is open source so there are various tools to allow you to run your own server. In this post, we’ll be looking at how you can use Gogs on your server for collaboration, which provides an interface very similar to Github.
Using Gogs to create git servers
Gogs will need to be manually downloaded and installed to your VP manually. Fortunately, they supply repositories you can configure on your server to get the packages and stay up-to-date. For this example, we’ll be configuring on an Ubuntu 16.04 VPS, but you can select a version for Debian, Ubuntu or CentOS from here. We will assume that the VPS you are using is dedicated to the use of Gogs. If you are planning to run another website on the same VPS, you’ll need to adapt the configuration accordingly.
wget -qO- http://dl.packager.io/srv/pkgr/gogs/key | sudo apt-key add –
sudo wget -O /etc/apt/sources.list.d/gogs.list \
http://dl.packager.io/srv/pkgr/gogs/pkgr/installer/ubuntu/16.04.repo
sudo apt-get update
sudo apt-get install gogs
Setting up MySQL databases with variables
Next, we need to set up the MySQL database for Gogs, and we will be using Nginx web browser to serve web pages. First, we’ll set some variables to make setup easier:
APP_NAME = “gogs”
MYSQL_PASSWORD = “strongpass”
GOGS_PASSWORD = “anotherpass”
HOSTNAME = “example.com“
When setting the variable, you’ll need to change “strongpass” and “anotherpass” to strong passwords we will use for the root MySQL user and the gogs MySQL user respectively. Also, change “example.com” to the domain name you have pointed to your server. Next we’ll pre-configure the MySQL password:
sudo debconf-set-selections <<< mysql-server mysql-server/root_password password ${MYSQL_PASSWORD}
sudo debconf-set-selections <<< mysql-server mysql-server/root_password_again password ${MYSQL_PASSWORD}
With that done, we can install MySQL server:
sudo apt-get install mysql-server
Now we will create a database for Gogs to use:
mysql -uroot -p${MYSQL_PASSWORD} -e “create database if not exists ${APP_NAME}; create user ‘${APP_NAME}’@’localhost’ identified by ‘${GOGS_PASSWORD}; grant all privileges on ${APP_NAME}.* to ‘${APP_NAME}’@’localhost’;”
Next, we are going to install the Nginx web server to serve the files:
sudo apt-get install nginx
Now we’ll create the config for the site:
sudo sh -c ‘echo “server {
listen 80;
server_name ${HOSTNAME};
location / {
proxy_pass http://localhost:6000;
}
}” > /etc/nginx/sites-available/default’
Final steps to creating your git server
With that done, we can now restart nginx for it to load the new configuration:
sudo systemctl restart nginx
You can now connect to your VPS either by navigating to the domain name you have pointed to it or its IP address in a web browser. Once you do, you will be greeted by a web page that asks for configuration information for your system.
You will need to change the MySQL user to “gogs” and set the password to the one you used for the GOGS_PASSWORD setting earlier. The rest of the initial settings should be fine. If you scroll down the page and expand the “Server and Other Services Settings” section, I’d recommend clicking to disable self-registration and requiring users to be signed in if you plan to use Gogs for private data. Then in the bottom section, you can create an administration user. Then click on the “Install Gogs” button.
After a few seconds, a page will load of your fresh Gogs install ready for you to add some repositories and start working.