Contributed by: Takeshi Miyaoka on Saturday, February 08 2025
Last modified on Saturday, February 15 2025
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
sudo chmod +x /usr/lib/git-core/git-http-backend
6. configure Apache virtual host
sudo nano /etc/apache2/sites-available/git.conf
<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
git clone http://gituser@git.example.com/git/myrepo.git