Setting up a Self Hosted Git server for IntelliJ PhpStorm on a Synology NAS Drive

If you are in the software development industry, you must already know all about version control systems,  and more specifically about Git. If you are also in possession of a Synology NAS drive, you may have also noticed that amongst the numerous packages that can be installed on the unit, you can also install a Git Server. In this post we will see how to setup the Git Server on your NAS drive, and also how to use it with IntelliJ products, more specifically, with PhpStorm.

The tutorial is based on a Synology DS216Play model, however the steps should be more or less the same for most if not all Synology products running DSM6. The same applies to the PhpStorm part. The steps described will most likely work with all the IntelliJ product range, and can be easily adapted to any product with Git integration.

Before we start, this tutorial assumes some very basic knowledge of the Linux shell, and also assumes you have admin access to your Synology Nas drive via Web interface and ssh. If you do not know how to setup ssh access to your NAS Drive, you can follow this tutorial here.

Let’s get started.

  • Login to your NAS drive via the web interface
  • From Control Panel create a new user and call it “gituser”
  • Create a strong password for the user
  • Under “User Groups” make the user part of the administrators group. This is necessary since we need the user to access the file system via ssh, and only administrators are allowed ssh access on Synology drives. This has security repercussions which you need to be aware of, especially if you intend sharing your git server with unknown parties. There are ways around this, which involve tampering with /etc/passwd files, however it is beyond the scope of this tutorial.
  • Next open “Package Center” and locate and install the Git Server package (normally located under utilities –> 3rd Party)
  • When the package installs, open it, and allow access to “gituser” the user we just created.
  • Start the Service if it has not already been started.

Login to the NAS drive via your favourite ssh client. You can use something like putty for windows, or simply ssh admin@nas-ip from a mac terminal, using the admin password you just used to access the web interface. We will now create a directory for the Git repos, and set the required permissions for out “gituser”. Create a directory called “git” in /volume1 directory:

admin@DS216PLAY:~#mkdir /volume1/git
admin@DS216PLAY:~#

Change ownership of the git directory to “gituser:administrators”

admin@DS216PLAY:~#chown gituser:administrators /volume1/git

Let us now create our first Git repo. The first step is to login as root. Issue the command “sudo -s” you will be asked for a password. Use the same password you used to login via ssh. Upon successful authentication, the prompt will change to sh-4.3# or something similar.

admin@DS216PLAY:~#sudo -s
password:
sh-4.3#

In order to create a repository, we need to issue the following commands in this order

  • git –bare init /volume1/git/whatever.project.name.git
  • chown -R gituser:users /volume1/git/whatever.project.name.git
  • cd /volume1/git/whatever.project.name.git
  • git update-server-info

You have just created your first git repo. Further down we will create a small bash script that will run the commands above for us, but it is a good thing to know what is going on, and how to create a git repo without the helper script.

From this point on, you can use your git repo from any other pc with ssh access to your NAS drive. The address of your newly created repo is : ssh://gituser@nas_drive_address/volume1/whatever.project.name.git

Using the Git repo with PhpStorm

  • Fire up PhpStorm, and open the project you intend to setup on Git.
  • Click on the VCS Menu, and Select “Enable Version Control Integration”
  • In the “Select a version control system to associate with the project root” pull down menu, select “Git”

Next we need to add our remote repo.

  • Once again from the VCS Menu, click on Git, and select “remotes”
  • Click on the “+” sign and type in the path to your repo using this format
    • ssh://gituser@Your_NAS_Address/volume1/your_git_repo.git     (You need to substitute Your_NAS_Address and your_git_repo.git according to your setup and the git repo name you have just created)

You will be asked to enter the “gituser” account password, which you have the option to save for future use.

Finally, from the project window, right click on the project name, select Git from the context menu and select “+ Add”. This will add all the files in the project to Git.

Finally click on VCS Menu and select “Commit Changes”, type in a commit message, click on the commit button, and select “commit and push” from the dropdown.

Automating repo creation using a script

In order to avoid going through all the steps to create a git repo on the NAS drive, increasing the risk of typos and omissions,  we can create a script to simplify our lives. You can either copy and paste the script below to a file called “create_git_repo.sh” in your home directory on the NAS drive using vi, or if you are not comfortable with vi, you can paste it to a text file on your PC and upload it to your NAS Drive. You can then copy it from where you uploaded it to your home directoy, and set permissions.

#!/bin/bash

############################################
# Change this path according to your needs #
############################################
repo_path="/volume1/git"
############################################




##############################################
# Normally you do not need to touch anything #
# below this line                            #
##############################################
clear
current_user=$(whoami)
if [ "$current_user" == "root" ]; then
	printf "\nRunning as Root ... OK\n"
	if [[ -z "$1" ]]; 
		then printf "\nFATAL: Need a repo name\n\n"; 
		exit 1
	fi

	if [[ ${repo_path:(-1)} == "/" ]]; then
		printf "\nFATAL: Repo path must not end with '/'\n"
		exit 1
	fi

	if [[ ${1:(-4)} != ".git" ]]; then
		printf "\nWarning: Appending .git suffix to filename\n\n";
		filename=${1}".git"
	else
		filename=${1}
	fi


	if [ -d "$repo_path/$filename" ];then
		printf "\n FATAL: A repo called $filename already exists in $repo_path\n\n"
		exit 1
	fi


	printf  "\nDo you want to create git repo $filename ? (y/n)" -n 1 -r
	read reply
	if [[ $reply =~ ^[Yy]$ ]];then
		filename="$repo_path/$filename"
		git --bare init $filename
		chown -R gituser:users $filename
		cd $filename
		git update-server-info
		printf "\n\nCreated repo $filename\n\n"
	else
		printf "\nUser Abort\n\n"
	fi
else
	printf "\n\nFatal: Script must be run as root\n\n"
fi

The only thing you need to change is the repo_path=”/volume1/git” in case you want to have your git repo on a different volume, depending on your NAS configuration. Save the file as create_git_repo.sh and chmod it to 774.

To use the script, simply type ./create_git_repo.sh reponame.git

sh-4.3#./create_git_repo.sh test_repo.git

Running as Root ... OK
Do you want to create git repo test_repo.git ? (y/n)y
Initialized empty Git repository in /volume1/git/test_repo.git/
Created repo /volume1/git/test_repo.git


The script will first check if it has been run by root, if it is, it will double check you actually want to create the repo, it will also check if a repo with the same name exits, add a .git extension if you have not done so already, then proceed to create and initialise the repo.

The above steps can be easily adapted to any self hosted git server running on Linux, as well as to any product with git integration.

A word about security

Having a self hosted git server is very useful and handy,  however, in order to for it to be of any use, you will need to open an SSH port from the outside world to your NAS drive. The repercussions of this is very obvious, make sure you create a very strong password, (or use ssh shared keys). It is also a good idea to change the default ssh port from 22 to something less obvious. Experience has shown that whenever any standard port is made accessible from outside, hacking attempts start almost instantaneously.

Happy Gitting 🙂

2 comments On Setting up a Self Hosted Git server for IntelliJ PhpStorm on a Synology NAS Drive

Leave a reply:

Your email address will not be published.

Site Footer