Q

Git server in FreeNas

Step by step guide for installing Git web server in FreeNas.

Introduction

In all my jobs I have used some kind of source control system. I started with VSS which is quite basic. Then I moved to Mercurial. At the beginning it didn't make any sense to me but one day I understood what Mercurial was all about. In my current job we use Git. After a few days playing with it I think it's more powerful and flexible than Mercurial. This video explains some differences between Git and Mercurial.

At home I have a NAS running FreeNas 9.3 and I have a bunch of Mercurial repositories. Now that I understand Git I want to migrate my projects to Git. But first I have to install a Git server on FreeNas. Here we go...

Process

1. Create a new jail

Jails are like virtual machines in FreeNas. It's recommended to install plugins or services in separated jails. To create a jail login into your FreeNas account and press Jails in the top menu. Select "Add Jails" and create a new jail. I called my jail "GitServer2". Go to advanced mode and write an IP address for your jail. If you want more information about jails you can check this link

2. Create new user

I prefer to use SSH instead of the web-based shell that comes with FreeNas. Before activating SSH you have to create a new user in the Jail. Using the web shell (4th icon in the bottom of Jails menu in FreeNas) execute:

adduser

Follow the steps and make sure to add user to "wheel" group when you are asked to invite user to other groups.


@@img_alt_fq

3. Enable and configure SSH

Use the web shell to enable SSH in this jail. Edit the following file using "ee" editor:

ee /etc/rc.conf

Change sshd_enable parameter to yes:

sshd_enable="YES"

To exit ee editor pres [ESC]. Make sure to save changes (option (a), then option (a) again). Then stop and start the virtual machine using the buttons at the bottom.


@@img_alt_fq

At this point you can close the web terminal. Open a terminal on your local computer (or execute Putty in Windows) and connect to your SSH account:

ssh new_jail_user@jail_ip_address

Your computer will ask you if you want to accept the new key for this connection. Type "yes".

4. Install and configure MySql

I chose Gogs Git server which requires MySql. For installing MySql execute:

su
pkg install mysql55-server

Choose "y" to all questions asked during the installation process.

Edit /etc/rc.conf file and add the following line

ee /etc/rc.conf
# Add following line at the end of the file
mysql_enable="YES"

To exit ee editor pres [ESC]. Make sure to save the changes (option (a), then option (a) again).

Now open the MySql configuration file:

ee /usr/local/etc/my.cnf

It should look like this:

# The MySQL server configuration
[mysqld]

socket = /tmp/mysql.sock

# Don't listen on a TCP/IP port at all.
skip-networking
skip-name-resolve

#Expire binary logs after one day:
expire_logs_days = 1

port = 3306

Change permission of file executing:

chmod 750 /usr/local/etc/my.cnf

Exit su mode by typing "exit". Then restart the jail as explained before. Reconnect to the jail via SSH. To test MySql server type "mysql". If the MySql prompt is displayed ( mysql> ) everything is OK so far. Check that MySql is listening in the right port by executing:

SHOW GLOBAL VARIABLES LIKE 'PORT';

It should return 3306. Disconnect from MySql prompt by typing "exit".

5. Configure database and user

Connect to MySql as root by typing:

mysql --user=root mysql

Then create a new database, new user with password and grant permission to user for the new database:

CREATE DATABASE gogsDB;
CREATE USER gogsUSR;
update user set password=PASSWORD('user_password_goes_here') where User='gogsUSR';
grant all privileges on gogsDB.* to gogsUSR@jail_ip_address identified by 'user_password_goes_here' with grant option;
exit

6. Install Git

You can easily install Git executing the following commands in SSH terminal:

su
pkg install git

Choose "y" to all questions asked during the installation process.

7. Install Gogs

Gogs is one of many open source Git web managers. I chose Gogs because it seems to be very fast and user friendly. It doesn't have all features offered by other programs but for a small team it should do the work. To install Gogs we will use a script located here (execute it as root). More info can be found here.

git clone https://github.com/jedediahfrey/FreeNAS-Gogs.git
cd FreeNAS-Gogs
./gogs-root.sh

To access "Install Steps For First-time Run" go to your browser and type the jail IP address and port 3000. In my case it would be:

http://192.168.20.25:3000
####### usar screenshots aca. La unica diferencia es que "run user" tuve que poner "git" (gituser no funciono) usa mounted storage src: /mnt/Storage/JailStorage/GitStorage dst: /usr/home/git/gogs-repositories

In "Database Settings" select user, password and database generated in previous steps.

NOTE1: Make sure you have the right permissions for the folder you choose for repositories.

NOTE2: I found a fantastic Git GUI client called GitKraken. It's elegant, simple to use and multiplatform. I use Windows version at work and Linux version at home. It still in beta but it's under strong development process. I totally recommended!!!

NOTE3: It is recommended but not require to add a storage space mounted to the folder where repositories are going to be installed. This process goes outside the scope of this tutorial, but you can ask me to write more information.