How to Set Up Git Server Using SSH and HTTP on Ubuntu 14.04
If you are a software developer, you probably have created a GitHub account to manage, share and learn code. To make your contribution public, GitHub is the right place for open source. However, the hosting service of private repositories on GitHub is not free. If you want to co-work with your team using Git in-house, you can set up a Git server yourself over SSH or HTTP.
Git Server Over SSH
Install OpenSSH Server
sudo apt-get install openssh-server
Create a new user:
sudo adduser dynamsoft
Create and initialize a Git repository:
mkdir -p /opt/git/dynamsoft.git
cd /opt/git/dynamsoft.git
sudo git init –bare –shared
Get the repository from the remote server:
git clone dynamsoft@192.168.8.41:/opt/git/dynamsoft.git
When you push changes to the remote server, you may see the error: remote: error: insufficient permission for adding an object to repository database ./objects:
To solve the permission issue, we can create a new group dynamsoftgit
sudo addgroup dynamsoftgit
Add the user dynamsoft to the group:
sudo usermod -a -G dynamsoftgit dynamsoft
Give the folder permission to the group:
sudo chgrp -R dynamsoftgit dynamsoft.git/
Now we can successfully push the changes:
Git Server Over HTTP
Install Apache:
sudo apt-get install apache2 apache2-utils
Enable following modules:
a2enmod cgi alias env rewrite
We need to edit the Apache configuration file. The httpd.conf does not exist anymore in the latest version of Apache. Here is the guide. The default configuration file is located at /etc/apache2/sites-enabled/000-default.conf. If you want to create a new configuration file, do not forget to enable it after saving the file:
a2ensite <custom-conf>
Open the configuration file, we will find the default DocumentRoot is /var/www/html.
Create a Git repository /var/www/html/git/dynamsoft.git as what you have done for SSH.
Add the following configuration to 000-default.conf:
SetEnv GIT_PROJECT_ROOT /var/www/html/git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
RewriteEngine On
RewriteCond %{QUERY_STRING} service=git-receive-pack [OR]
RewriteCond %{REQUEST_URI} /git-receive-pack$
RewriteRule ^/git/ - [E=AUTHREQUIRED]
<Files "git-http-backend">
AuthType Basic
AuthName "Git Access"
AuthUserFile /var/www/html/.htpasswd
Require valid-user
Order deny,allow
Deny from env=AUTHREQUIRED
Satisfy any
</Files>
Give the group permission to www-data:
sudo chgrp -R www-data /var/www/html/git
Create a .htpasswd file for authentication:
htpasswd –c /var/www/html/.htpasswd dynamsoft
To append a user, just ignore –c:
htpasswd /var/www/html/.htpasswd xiao
Now you can successfully pull and push source code using Git server.