{"id":38,"date":"2011-06-15T20:55:10","date_gmt":"2011-06-16T03:55:10","guid":{"rendered":"http:\/\/www.marcmorgan.ca\/?p=38"},"modified":"2011-06-16T18:30:06","modified_gmt":"2011-06-17T01:30:06","slug":"version-control-with-git-access-control-with-gitolite-and-authenticated-web-repository-viewing-with-gitweb-apache-on-ubuntu-11-04-server","status":"publish","type":"post","link":"https:\/\/www.marcmorgan.ca\/?p=38","title":{"rendered":"Version Control With Git, Access Control With Gitolite, and Authenticated Web Repository Viewing With Gitweb + Apache on Ubuntu 11.04 Server"},"content":{"rendered":"<p>Phew, that&#8217;s a long title.<br \/>\nThis post aims to start with a fresh install of Ubuntu 11.04 Server, installing git, gitolite, apache, and gitweb in order to do version control with access control and a web-based repository viewer. If you&#8217;re not running Ubuntu 11.04, most of this guide should still apply.<br \/>\n<!--more--><\/p>\n<h2>Assumptions<\/h2>\n<p>Here&#8217;s a list of (hopefully) not too restrictive assumptions this post makes:<\/p>\n<ul>\n<li>You have full root shell access on the server you&#8217;re working with<\/li>\n<li>Your git repos will be stored in \/home\/git, and the git repos will be managed by a user named &#8220;git&#8221;<\/li>\n<li>You already have public\/private SSH key authentication set up<\/li>\n<li>You&#8217;re OK with using apache&#8217;s htpasswd method for authenticating access to gitweb<\/li>\n<li>I&#8217;ll be using &#8220;example.com&#8221; to represent your domain name. If you don&#8217;t have a domain name, you&#8217;ll have to use the subdirectory method when configuring apache<\/li>\n<li>I&#8217;ll also be using &#8220;you&#8221; as your user name. Replace this with your actual user name<\/li>\n<\/ul>\n<p>Any commands this guide uses that require root privileges will be preceded by &#8220;sudo.&#8221;<\/p>\n<h2>Git<\/h2>\n<p>First, you&#8217;ll want to install git and do the initial configuration:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install git\r\ngit config --global user.name &quot;Your Name&quot;\r\ngit config --global user.email &quot;you@example.com&quot;\r\n<\/pre>\n<h2>Gitolite<\/h2>\n<p>It is possible to install gitolite using apt, but I decided to get the latest version from the gitolite repository.<br \/>\nFirst, copy your public key (usually ~\/.ssh\/id_dsa.pub) from your local computer to \/tmp\/you.pub on your server.<br \/>\nDownload and install gitolite (this process is identical to the root method on the <a href=\"http:\/\/sitaramc.github.com\/gitolite\/doc\/1-INSTALL.html#_root_method\">gitolite site<\/a>). You&#8217;ll be creating the &#8220;git&#8221; user along the way.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# Clone the repo\r\ngit clone git:\/\/github.com\/sitaramc\/gitolite\r\ncd gitolite\r\n# Install gitolite\r\nsudo src\/gl-system-install\r\n# Create the git user (if you don't already have one)\r\n# with its own group and home directory at \/home\/git\r\nsudo useradd -d \/home\/git -m -U git\r\n# Become that user\r\nsudo su - git\r\n# Do the initial setup with your public key\r\ngl-setup \/tmp\/you.pub\r\n<\/pre>\n<p>Gitolite will now prompt you with its config file, asking you to change any variables you want. The important variables to change are:<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\n# Set this to 1 if you want to use wildcard repository names\r\n$GL_WILDREPOS = 1;\r\n# Equivalent to rwxr-x--- permissions - you'll want apache\r\n# to be able to read the repos (you'll add it to the git\r\n# group later)\r\n$REPO_UMASK = 0027;\r\n# Apache htpasswd file\r\n$HTPASSWD_FILE = &quot;\/home\/git\/htpasswd&quot;;\r\n<\/pre>\n<p>On your local machine, you can check out a copy of the gitolite-admin repository:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngit clone git@example.com:gitolite-admin.git\r\n<\/pre>\n<p>Take a look at the config file (gitolite-admin\/conf\/gitolite.conf). By default, it has a user configured for you based on the name of your public key. To add new users, add their key to gitolite-admin\/keydir (making sure to git add the file) and commit. The default config has the admin repo and a testing repo and looks like this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nrepo    gitolite-admin\r\n        RW+     =   you\r\n\r\nrepo    testing\r\n        RW+     =   @all\r\n<\/pre>\n<p>I won&#8217;t go into detail of configuring repositories here. For more information, check the <a href=\"http:\/\/sitaramc.github.com\/gitolite\/doc\/gitolite.conf.html\">official documentation<\/a>. Once you&#8217;ve changed the config to your liking, make sure to commit and push:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngit commit -am 'Made initial changes to the admin repo'\r\ngit push\r\n<\/pre>\n<h2>Installing Apache<\/h2>\n<p>At this point, you&#8217;ll need to install apache. You&#8217;ll be configuring it later.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install apache2\r\n<\/pre>\n<p>You&#8217;ll also want to add the apache user to the git group, and give it write permission to the \/home\/git\/.gitolite folder (otherwise gitweb will throw an error later on):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo usermod -a -G git www-data\r\nsudo chmod -R 770 \/home\/git\/.gitolite\r\n<\/pre>\n<p>Also, create the htpasswd file used in the gitolite config earlier:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo -u git touch \/home\/git\/htpasswd\r\n<\/pre>\n<p>At this point, from your local machine, you can also create your password in the htpasswd file. Thankfully, gitolite provides a simple way for you to do this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nssh git@example.com htpasswd\r\n<\/pre>\n<h2>Gitweb<\/h2>\n<p>I installed gitweb using apt:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install gitweb\r\n<\/pre>\n<p>You&#8217;ll now need to customize the gitweb config (\/etc\/gitweb.conf). The <a href=\"https:\/\/github.com\/sitaramc\/gitolite\/blob\/pu\/contrib\/gitweb\/gitweb.conf\">gitolite docs<\/a> show how to do this. Here&#8217;s what my config looks like (the (hopefully) only line you might have to change is highlighted):<\/p>\n<pre class=\"brush: perl; highlight: [20]; title: ; notranslate\" title=\"\">\r\n# --------------------------------------------\r\n# Per-repo authorization based on gitolite ACL\r\n# Include this in gitweb.conf\r\n# See doc\/3-faq-tips-etc.mkd for more info\r\n\r\n# please note that the author does not have personal experience with gitweb\r\n# and does not use it.  Some testing may be required.  Patches welcome but\r\n# please make sure they are tested against a &quot;github&quot; version of gitolite\r\n# and not an RPM or a DEB, for obvious reasons.\r\n\r\n# HOME of the gitolite user\r\nmy $gl_home = $ENV{HOME} = &quot;\/home\/git&quot;;\r\n\r\n# the following variables are needed by gitolite; please edit before using\r\n\r\n# this should normally not be anything else\r\n$ENV{GL_RC} = &quot;$gl_home\/.gitolite.rc&quot;;\r\n\r\n# this can have different values depending on how you installed.\r\n$ENV{GL_BINDIR} = &quot;\/usr\/local\/bin&quot;;\r\n\r\n# finally the user name\r\n$ENV{GL_USER} = $cgi-&gt;remote_user || &quot;gitweb&quot;;\r\n\r\n# now get gitolite stuff in...\r\nunshift @INC, $ENV{GL_BINDIR};\r\nrequire gitolite_rc;    gitolite_rc -&gt; import;\r\nrequire gitolite;       gitolite    -&gt; import;\r\n\r\n# set project root etc. absolute paths\r\n$ENV{GL_REPO_BASE_ABS} = ( $REPO_BASE =~ m(^\/) ? $REPO_BASE : &quot;$gl_home\/$REPO_BASE&quot; );\r\n$projects_list = $projectroot = $ENV{GL_REPO_BASE_ABS};\r\n\r\n$export_auth_hook = sub {\r\n    my $repo = shift;\r\n    # gitweb passes us the full repo path; so we strip the beginning\r\n    # and the end, to get the repo name as it is specified in gitolite conf\r\n    return unless $repo =~ s\/^\\Q$projectroot\\E\\\/?(.+)\\.git$\/$1\/;\r\n\r\n    # check for (at least) &quot;R&quot; permission\r\n    my ($perm, $creator) = &amp;repo_rights($repo);\r\n    return ($perm =~ \/R\/);\r\n};\r\n\r\n###############################\r\n# ORIGINAL GITWEB CONFIG FILE #\r\n###############################\r\n# path to git projects (&lt;project&gt;.git)\r\n#$projectroot = &quot;\/home\/git\/repositories&quot;;\r\n\r\n# directory to use for temp files\r\n$git_temp = &quot;\/tmp&quot;;\r\n\r\n# target of the home link on top of all pages\r\n#$home_link = $my_uri || &quot;\/&quot;;\r\n\r\n# html text to include at home page\r\n#$home_text = &quot;indextext.html&quot;;\r\n\r\n# file with project list; by default, simply scan the projectroot dir.\r\n#$projects_list = $projectroot;\r\n\r\n# stylesheet to use\r\n#@stylesheets = (&quot;static\/gitweb.css&quot;);\r\n\r\n# javascript code for gitweb\r\n#$javascript = &quot;static\/gitweb.js&quot;;\r\n\r\n# logo to use\r\n#$logo = &quot;static\/git-logo.png&quot;;\r\n\r\n# the 'favicon'\r\n#$favicon = &quot;static\/git-favicon.png&quot;;\r\n\r\n# git-diff-tree(1) options to use for generated patches\r\n#@diff_opts = (&quot;-M&quot;);\r\n@diff_opts = ();\r\n<\/pre>\n<h2>Configuring Apache<\/h2>\n<p>When configuring apache, you can either install gitweb as a subdirectory (example.com\/git) or a subdomain (git.example.com). I&#8217;ll cover both methods.<\/p>\n<h4>Subdirectory Method<\/h4>\n<p>In your apache config file (probably \/etc\/apache2\/sites-enabled\/000-default if you just installed apache), insert the highlighted lines (my entire default config file is provided for context):<\/p>\n<pre class=\"brush: plain; highlight: [24,25,26,27,28,29,30,31,32,33]; title: ; notranslate\" title=\"\">\r\n&lt;VirtualHost *:80&gt;\r\n        ServerAdmin webmaster@localhost\r\n\r\n        DocumentRoot \/var\/www\r\n        &lt;Directory \/&gt;\r\n                Options FollowSymLinks\r\n                AllowOverride None\r\n        &lt;\/Directory&gt;\r\n        &lt;Directory \/var\/www\/&gt;\r\n                Options Indexes FollowSymLinks MultiViews\r\n                AllowOverride None\r\n                Order allow,deny\r\n                allow from all\r\n        &lt;\/Directory&gt;\r\n\r\n        ScriptAlias \/cgi-bin\/ \/usr\/lib\/cgi-bin\/\r\n        &lt;Directory &quot;\/usr\/lib\/cgi-bin&quot;&gt;\r\n                AllowOverride None\r\n                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\r\n                Order allow,deny\r\n                Allow from all\r\n        &lt;\/Directory&gt;\r\n\r\n        Alias \/git \/usr\/share\/gitweb\r\n        &lt;Directory &quot;\/usr\/share\/gitweb&quot;&gt;\r\n           Options ExecCGI\r\n           DirectoryIndex gitweb.cgi\r\n\r\n           AuthType Basic\r\n           AuthName &quot;Gitweb&quot;\r\n           AuthUserFile \/home\/git\/htpasswd\r\n           Require valid-user\r\n        &lt;\/Directory&gt;\r\n\r\n        ErrorLog ${APACHE_LOG_DIR}\/error.log\r\n\r\n        # Possible values include: debug, info, notice, warn, error, crit,\r\n        # alert, emerg.\r\n        LogLevel warn\r\n\r\n        CustomLog ${APACHE_LOG_DIR}\/access.log combined\r\n\r\n    Alias \/doc\/ &quot;\/usr\/share\/doc\/&quot;\r\n    &lt;Directory &quot;\/usr\/share\/doc\/&quot;&gt;\r\n        Options Indexes MultiViews FollowSymLinks\r\n        AllowOverride None\r\n        Order deny,allow\r\n        Deny from all\r\n        Allow from 127.0.0.0\/255.0.0.0 ::1\/128\r\n    &lt;\/Directory&gt;\r\n\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<p>Now, reload apache:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo service apache2 restart\r\n<\/pre>\n<p>and you should be able to login using your username and password you set up earlier by going to http:\/\/example.com\/git.<\/p>\n<h4>Virtual Host Method<\/h4>\n<p>You&#8217;ll want to create a new config file to contain the virtual host (for example, \/etc\/apache2\/sites-enabled\/001-gitweb). Fill that config file with this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;VirtualHost *:80&gt;\r\n   ServerName git.example.com\r\n\r\n   DocumentRoot \/usr\/share\/gitweb\r\n   &lt;Directory \/usr\/share\/gitweb&gt;\r\n      Options ExecCGI\r\n      DirectoryIndex gitweb.cgi\r\n\r\n      AuthType Basic\r\n      AuthName &quot;Gitweb&quot;\r\n      AuthUserFile \/home\/git\/htpasswd\r\n      Require valid-user\r\n   &lt;\/Directory&gt;\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<p>Now, reload apache:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo service apache2 restart\r\n<\/pre>\n<p>and you should be able to login using your username and password you set up earlier by going to http:\/\/git.example.com.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Phew, that&#8217;s a long title. This post aims to start with a fresh install of Ubuntu 11.04 Server, installing git, gitolite, apache, and gitweb in order to do version control with access control and a web-based repository viewer. If you&#8217;re not running Ubuntu 11.04, most of this guide should still apply.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[10,7,8,9,11,6],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-apache","tag-git","tag-gitolite","tag-gitweb","tag-tutorial-2","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38"}],"version-history":[{"count":63,"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=\/wp\/v2\/posts\/38\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marcmorgan.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}