Setting Up a Private Git Repository on RedHat Linux – HTTP Access

Setting Up a Private Git Repository on RedHat Linux – HTTP Access

Git is a widely used distributed version control system that enables multiple developers to collaborate on coding projects. While public repositories on platforms like GitHub are popular, there may be scenarios where hosting a private Git repository is preferable. This is often the case for proprietary code, private projects, or simply for maintaining control over the hosting environment.

In this tutorial, we will demonstrate how to set up a private Git repository server on a RedHat Linux machine using Git’s smart HTTP transport protocol, which allows for pushing and pulling over HTTP without the need for additional external dependencies.

Prerequisites

  • A RedHat Linux server (version 7 or higher).
  • Git installed (execute sudo yum install git).
  • Apache HTTP server installed (execute sudo yum install httpd).
  • A non-root user account to operate the Git server.

Steps

  1. Create a User Account
   sudo adduser git-server
  1. Prepare the Repository Directory
   sudo mkdir /opt/git
   sudo chown git-server:git-server /opt/git
   sudo chmod 755 /opt/git
  1. Initialize the Bare Repository
Switch to the git-server user:
sudo su git-server -s /bin/bash
Initialize the repository:
cd /opt/git 
git init --bare myproject.git
Exit back to your normal user:
exit

4. Set Up Authentication

Generate a password file for repository access:
sudo htpasswd -c /opt/git/htpasswd myuser

5. Configure Apache for Git

Edit the Apache configuration:
sudo vi /etc/httpd/conf.d/git.conf

Add the following configuration (modify as necessary for your setup):

<VirtualHost *:80>
    ServerName gitserver
    DocumentRoot "/opt/git"

    <Directory "/opt/git">
        Options +FollowSymLinks +ExecCGI
        Require all granted
    </Directory>

    SetEnv GIT_PROJECT_ROOT /opt/git
    SetEnv GIT_HTTP_EXPORT_ALL
    SetEnv GITPERMBITS 0770

    ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/

    <LocationMatch "(?!/myproject.git\/)">
        AuthType Basic
        AuthName "Git Access"
        AuthBasicProvider file
        AuthUserFile "/opt/git/htpasswd"
        Require valid-user
    </LocationMatch>
</VirtualHost>

6. Restart Apache to Apply Changes

   sudo systemctl restart httpd

Usage

  • To clone the repository:
  git clone http://myuser:password@gitserver/git/myproject.git
  • To push changes:
  git push http://myuser:password@gitserver/git/myproject.git master

Notes

  • This setup uses HTTP, which is simpler but less secure than other methods like SSH. For production environments, consider setting up SSH-based authentication.
  • Ensure your DNS or hosts file is configured so that gitserver resolves to the correct IP address.

This basic setup uses Git’s smart HTTP transport which is relatively easy to configure but less efficient than Git’s more common data transfer protocols. For better performance on a production server, you may want to look into using the Git SSH transport or Git’s daemon process instead. But for basic private Git hosting needs, this HTTP approach works great.

Leave a Reply

Your email address will not be published. Required fields are marked *


Translate ยป