{"id":20281,"date":"2016-02-03T06:27:39","date_gmt":"2016-02-03T11:57:39","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/how-to-setup-git-on-self-hosted-server\/"},"modified":"2023-08-16T23:11:43","modified_gmt":"2023-08-17T04:41:43","slug":"how-to-setup-git-on-self-hosted-server","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/how-to-setup-git-on-self-hosted-server\/","title":{"rendered":"How to setup git on self hosted server."},"content":{"rendered":"<p>This article discusses setting up Git server on a self-hosted server, e.g. you can buy an AWS EC2 and have your own git environment or you can have your own local server. (http:\/\/git.example.com).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Knowledge_Base\"><\/span><strong>Knowledge Base:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If cost is not an issue, there are multiple services like github.com(Pricing model is number of repository based) and bitbucket.com (Pricing model is based on number of users &#8211; up to 5 user it&#8217;s all free, you can create n number of repository).<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Why_we_should_self-host_Git\"><\/span><strong>Why we should self-host Git:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>Pros:<\/strong><\/p>\n<ul>\n<li>You can save money by hosting on your local server in your office premise; you just need an Ubuntu or Linux based system!<\/li>\n<li>Data transfer speed will be super-fast as it will be on your local network<\/li>\n<li>Your data is on your server, so it&#8217;s secure!<\/li>\n<\/ul>\n<p><strong>Cons:<\/strong><\/p>\n<ul>\n<li>You are managing this in-house, so you may need IT persons to handle ongoing support and maintenance of server<\/li>\n<li>If you are hosting on your local network and you need to access outside network you will need static IP and should be pointing to some domain, so static IP may cost something.<\/li>\n<li>You will not get smart UI to manage repository, users and its access.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"How_to_setup_Local_Git\"><\/span><strong>How to setup Local Git!<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Steps for how to install Git on Ubuntu 12.04 LTS server, for other version steps are same, but I have tested on this version.<\/p>\n<p>Assuming you have Ubuntu server ready and you are connected using Terminal app in Mac or some SSH tool.<\/p>\n<p><strong>Step #1<\/strong>: Install Git, Apache<\/p>\n<p>$ sudo apt-get install git apache2<\/p>\n<p><strong>Step #2:<\/strong> Create the \u201cgit\u201d user\/group.<\/p>\n<p>$ sudo adduser git<\/p>\n<p>Fill all required information it asks, make sure you remember password.<\/p>\n<p><strong>Step #3:<\/strong> Setup Client Machine to manage Git Repos<\/p>\n<ul>\n<li>Client machine is from where you will be remotely managing Git repositories, mostly an IT Admin&#8217;s PC.<\/li>\n<li>Make sure it&#8217;s a Linux machine, ssh-keygen is already installed, and it came by default with Mac machine.<\/li>\n<li>Create RSA key without passphrase which is identity of your machine.<\/li>\n<\/ul>\n<p>This command should have created 2 files \u201cid_rsa\u201d and \u201cid_rsa.pub\u201d under $HOME\/.ssh directory.<\/p>\n<p><strong>PS:<\/strong> If you would like to manage repository from other PCs later, you may need to share this key with new machine, feel free to contact me in case of trouble , I will be happy to help.<\/p>\n<p>Copy &#8220;id_rsa.pub&#8221; file to your remote Ubuntu server which we are setting up as Git Server.<\/p>\n<p><strong>KB:<\/strong> scp is command to copy file on remote server, this command will copy &#8220;id_rsa.pub&#8221; file from Client Machine to Remote server. Make sure this command is fired on Client Machine \ud83d\ude42<\/p>\n<p><strong>Step #4: <\/strong>Setup Git Admin<\/p>\n<p>Switch back from Terminal to remote server!<\/p>\n<p>Move and Rename pub key file to proper location now and also set owner to git group and user.<\/p>\n<p>$ sudo mv \/tmp\/id_rsa.pub \/home\/git\/Git-Adm.pub<br \/>\n$ sudo chown git:git \/home\/git\/Git-Adm.pub<\/p>\n<p><strong>Step #5:<\/strong> Login as git user<\/p>\n<p>Login as git user now, $ su -l git<\/p>\n<p><strong>Step #6:<\/strong> Gitolite installation<\/p>\n<p>Gitolite is a tool to manage Git repos.<\/p>\n<p><strong>#1:<\/strong> Checkout source from github.<\/p>\n<p>$ git clone https:\/\/github.com\/sitaramc\/gitolite<\/p>\n<p><strong>#2:<\/strong> Create bin folder at \/home\/git<\/p>\n<p>$ mkdir bin<\/p>\n<p><strong>#3:<\/strong> Install gitolite<\/p>\n<p>$ cd gitolite\/<br \/>\n$ .\/install<br \/>\n$ cd ..<br \/>\n$ \/home\/git\/gitolite\/src\/gitolite setup -pk Git-Adm.pub<br \/>\n$ exit<\/p>\n<p>Exit is to logout from git user, now you should be in your root account.<\/p>\n<p><strong>Step #7:<\/strong> Create bin folder to your Web Root and set permission and owner<\/p>\n<p>$ cd \/var\/www<br \/>\n$ sudo mkdir bin<br \/>\n$ install -d -m 0755 -o git -g git \/var\/www\/bin<\/p>\n<p><strong>Step #9:<\/strong> Scripts for Gitolite<\/p>\n<p>$ cd \/var\/www\/bin<br \/>\n$ sudo vi gitolite-suexec-wrapper.sh<\/p>\n<p><strong>Paste below script:<\/strong><\/p>\n<p>#!\/bin\/bash<br \/>\n#<br \/>\n# Suexec wrapper for gitolite-shell<br \/>\n#<\/p>\n<p>export GIT_PROJECT_ROOT=&#8221;\/home\/git\/repositories&#8221;<br \/>\nexport GITOLITE_HTTP_HOME=&#8221;\/home\/git&#8221;<\/p>\n<p>exec ${GITOLITE_HTTP_HOME}\/gitolite\/src\/gitolite-shell<\/p>\n<p><strong>Set Proper permission<\/strong><\/p>\n<p>$ chown -R git:git \/var\/www\/bin<br \/>\n$ chmod 750 \/var\/www\/bin\/gitolite-suexec-wrapper.sh<br \/>\n$ chmod 755 \/var\/www\/bin<\/p>\n<p>Step #10: Modify &#8220;UMASK&#8221;<\/p>\n<p>$ sudo vi \/home\/git\/.gitolite.rc<\/p>\n<p>Change UMASK,<\/p>\n<p>From:<br \/>\nUMASK =&gt; 0077<\/p>\n<p>To<br \/>\nUMASK =&gt; 0027<\/p>\n<p>Step #11: Clone Gitolite Admin repo to Client Machine<\/p>\n<p>PS: Switch to your Client Machine terminal<\/p>\n<p>$ mkdir ~\/git-repos<br \/>\n$ cd ~\/git-repos<br \/>\n$ git clone git@ip-address-of-git-server:gitolite-admin.git<br \/>\n<strong><br \/>\nStep #12:<\/strong> Adding new users and repos<\/p>\n<p>Get pubkeys for your users<\/p>\n<p>For example users \u201cjaym\u201d and \u201czaidp\u201d.<\/p>\n<p>Let&#8217;s create keys for &#8220;jaym&#8221; and &#8220;zaidp&#8221;<\/p>\n<p>Make sure you create without passphrase<\/p>\n<p>$ ssh-keygen -t rsa -C &#8220;Creating jaym keys&#8221; -f &#8220;jaym&#8221;<br \/>\n$ ssh-keygen -t rsa -C &#8220;Creating zaidp keys&#8221; -f &#8220;zaidp&#8221;<\/p>\n<p><strong>PS:<\/strong> keys will be generated in the same directory you are now in, it won&#8217;t create in ~\/.ssh folder as you have provided -f parameter in command<\/p>\n<p><strong>KB:<\/strong> this time we have used -f param to specify file name of keys, if we don&#8217;t provide -f it will generate with id_rsa name only.<\/p>\n<p>$ cp jaym.pub zaidp.pub ~\/git-repos\/gitolite-admin\/keydir<\/p>\n<p>Let&#8217;s create repository now, we will creating 3 repos<\/p>\n<ul>\n<li>&#8220;foo&#8221; : only jaym will have Read\/Write access and zaidp will have only Read<\/li>\n<li>&#8220;bar&#8221; : only zaidp will have Read\/Write access and jaym will have only Read<\/li>\n<li>&#8220;foobar&#8221; : only jaym &amp; zaidp both will Read\/Write access<\/li>\n<\/ul>\n<p>$ cd ~\/git-repos\/gitolite-admin\/<br \/>\n$ vi conf\/gitolite.conf<\/p>\n<p>Updated gitolite conf looks like below,<\/p>\n<p>repo gitolite-admin<br \/>\nRW+ = Git-Adm<\/p>\n<p>repo testing<br \/>\nRW+ = @all<\/p>\n<p>repo foo<br \/>\nRW = jaym<br \/>\nR = zaidp<\/p>\n<p>repo bar<br \/>\nR = jaym<br \/>\nRW = zaidp<\/p>\n<p>repo foobar<br \/>\nRW = jaym<br \/>\nRW = zaidp<\/p>\n<p><strong>PS:<\/strong> R is for Read and W is for Write.<\/p>\n<p>Push changes to server now,<\/p>\n<p>$ git add keydir conf<br \/>\n$ git commit -m &#8216;added users jaym and zaidp and also created sample repos&#8217;<br \/>\n$ git push origin master<\/p>\n<p><strong>Output:<\/strong><br \/>\nCounting objects: 4, done.<br \/>\nDelta compression using up to 8 threads.<br \/>\nCompressing objects: 100% (3\/3), done.<br \/>\nWriting objects: 100% (4\/4), 455 bytes | 0 bytes\/s, done.<br \/>\nTotal 4 (delta 0), reused 0 (delta 0)<br \/>\nremote: Initialized empty Git repository in \/home\/git\/repositories\/bar.git\/<br \/>\nremote: Initialized empty Git repository in \/home\/git\/repositories\/foo.git\/<br \/>\nremote: Initialized empty Git repository in \/home\/git\/repositories\/foobar.git\/<br \/>\nTo git@192.168.1.40:gitolite-admin.git<\/p>\n<p>6a5ee66..d032149 master &gt; master<br \/>\n<strong><br \/>\nStep #13:<\/strong> How Jay and Zaid will be able to use Git<\/p>\n<p>Assuming both are using Mac systems<\/p>\n<p>#1: Send jaym key file to Jay in his Mac<br \/>\n#2: Jay needs to copy that file in ~\/.ssh folder.<br \/>\n#3: in same ~\/.ssh folder you should be able to find config file, if you cannot find you can create new file named &#8220;config&#8221;, PS: no extension.<br \/>\n#4: Open config file in any text editor. i.e. textedit, brackets<br \/>\n#5: Paste below line and save &amp; close file.<\/p>\n<p>Host git-jaym<br \/>\nHostName<br \/>\nUser git<br \/>\nIdentityFile ~\/.ssh\/jaym<br \/>\nIdentitiesOnly yes<\/p>\n<p>Let&#8217;s checkout repo.<\/p>\n<p>#6: Now open terminal app<\/p>\n<p>$ mkdir ~\/foo<br \/>\n$ cd ~\/foo<br \/>\n$ git clone git@git-jaym:foo.git<\/p>\n<p>Output:<br \/>\nCloning into &#8216;foo&#8217;&#8230;<br \/>\nwarning: You appear to have cloned an empty repository.<br \/>\nChecking connectivity&#8230; done.<\/p>\n<p>So that&#8217;s it! Jay is successfully able to checkout foo repo, we can do the same for other repo and other users!<\/p>\n<p>You can also use Sourcetree directly to checkout new Repo.<br \/>\n<a href=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2021\/02\/git-server.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1902\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2021\/02\/git-server.png\" alt=\"git setup\" width=\"594\" height=\"626\" \/><\/a><\/p>\n<p>Share your findings with me and drop queries or troubles in comments, I will be happy to help! Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article discusses setting up Git server on a self-hosted server, e.g. you can buy an AWS EC2 and have your own git environment or you can have your own local server. (http:\/\/git.example.com). Knowledge Base: If cost is not an issue, there are multiple services like github.com(Pricing model is number of repository based) and bitbucket.com [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20283,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108],"tags":[],"class_list":["post-20281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile-app"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20281"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20281\/revisions"}],"predecessor-version":[{"id":38635,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20281\/revisions\/38635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/20283"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}