Build a Git Server on Ubuntu and clone it using HTTP
- Saturday, February 08 2025
- Contributed by: Takeshi Miyaoka
- Views: 280
To build a Git Server on Ubuntu and use Apache to access Git repositories via HTTP, follow these steps to configure Apache.
First, make sure that /usr/lib/cgi-bin/git-http-backend has execute permission, and if it doesn't, grant it.
The following content was added
If authentication is required, do the following
1. installing Apache
sudo apt update
sudo apt install apache2
2. enable the required modules
sudo a2enmod cgi alias
sudo systemctl restart apache2
3. install Git
sudo apt install git
4. create a Git repository
sudo mkdir -p /var/www/git
cd /var/www/git
sudo git init --bare myrepo.git
sudo chown -R www-data:www-data myrepo.git
sudo chmod -R 755 myrepo.git
5. configure git-http-backend
First, make sure that /usr/lib/cgi-bin/git-http-backend has execute permission, and if it doesn't, grant it.
sudo chmod +x /usr/lib/git-core/git-http-backend
6. configure Apache virtual host
sudo nano /etc/apache2/sites-available/git.conf
The following content was added
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/git ServerName git.example.com <Directory /var/www/git> Options +ExecCGI Require all granted </Directory> ScriptAlias /git/ /usr/lib/git-core/git-http-backend/ SetEnv GIT_PROJECT_ROOT /var/www/git SetEnv GIT_HTTP_EXPORT_ALL <LocationMatch "^/git/.*/git-receive-pack$"> AuthType Basic AuthName "Git Access" AuthUserFile /etc/apache2/.htpasswd Require valid-user </LocationMatch> <Location /git> Require all granted </Location> </VirtualHost>
7. user authentication settings
sudo apt install apache2-utils
sudo htpasswd -c /etc/apache2/.htpasswd gituser
8.Activate the configuration and restart Apache
sudo a2ensite git.conf
sudo systemctl restart apache2
9. client-side connection
git clone http://git.example.com/git/myrepo.git
If authentication is required, do the following
git clone http://gituser@git.example.com/git/myrepo.git