Build a git server in Ubuntu

Source: Internet
Author: User
Tags git client openssh server ssh server using git

Recently, the company was preparing to enable git to manage the company's projects. Under the guidance of the CTO header, it finally completed the configuration of the GIT server. Before using git, you must first define several concepts: git server, git client, and git server administrator. This document uses Ubuntu as the GIT server, and PCA and PCB as the server administrator and git client respectively. Usually, OpenSSH server, git, gitosis, and apache2 need to be installed on the GIT server (if you want to use gitweb ).

This article will mainly introduce some configuration methods related to the GIT server and the Administrator. The client and administrator's git operation methods will be detailed in another article.

(1) git Server

1. Use the apt-Get update and apt-Get upgrade commands to update the current system. If the system is not updated, some dependent packages may not be installed when you install some programs later.

 

2. install OpenSSH server (UBUNTU) (the current user is wirror800)

First, run the PS-E | grep SSH command to check whether the SSH service is running on the local machine. If no, continue. Otherwise, skip this step.

Run the following command:

Sudo apt-Get install OpenSSH-Server

 

3. Modify the SSH server configuration file/etc/ssh/sshd_config (the current user is wirror800)

Port 22 # change to the login port you want, such as 2222

Permitrootlogin no # prohibit Root User Login

Strictmodes yes # Check whether the key user and permissions are correct.

Rsaauthentication yes # enable RSA Authentication

Pubkeyauthentication yes # enable Public Key Authentication

Passwordauthentication no # password authentication is disabled. It is enabled by default.

Serverkeybits 1024 # change the intensity of serverkey to 1024 bits after modification

Permitemptypasswords no # change to this status after modification. Do not log on with a blank password.

 

After modification, restart the SSH service:

Sudo/etc/init. d/ssh restart

Note: The SSH operation on the server end is now complete. Copy the "id_rsa file" mentioned on the Internet to the client ~ /. Ssh/directory, copy id_rsa.pub

Server Side ~ In the/. Ssh/directory, renaming authorized_keys is inaccurate. Id_rsa is the RSA private key generated using SSH-keygen. id_rsa.pub

Is a public key generated at the same time. The server administrator can create and manage the key used for communication on the server. If you want to manage the current user wirror800

Personnel are also allowed. For more information about administrator operations, see the second part of this article. We do not recommend that you create an administrator account on the Ubuntu server.

It is recommended that you refer to the content of OpenSSH: http://www.freebsd.org/doc/zh_CN/books/handbook/openssh.html

 

4. Install git (the current user is wirror800)

Sudo apt-Get install Git-core

During the installation of git, you may encounter some dependent package downloading failures, mainly because of the access to the Ubuntu Chinese site, we can go to its English site

Download the relevant files and install them manually. Here is a link:

Http://archive.ubuntu.com/ubuntu/pool/main/

 

5. Install gitosis (the first thing you need to clarify is why you need to use gitosis) (the current user is wirror800)

(1) create a folder to store the downloaded gitosis file, as shown in figure

Mkdir ~ /Gitosis_setup

(2) install gitosis

Cd ~ /Gitosis_setup

Git clone git: // eagain.net/gitosis

CD gitosis

Sudo Python setup. py install

NOTE: If Python setup. py install fails, the python-setuptools.py needs to be installed

Sudo apt-Get install python-setuptools.py

 

6. Create a system user for gitosis (the current user is wirror800)

Sudo adduser -- System -- shell/bin/sh -- GECOS 'git SCM user' -- group -- disabled-Password -- home/git

This method is a common method on the Internet. It creates a git user with a password disabled. However, when you use the su command to switch between git users, empty passwords are always used.

Verification Failed. You can use the following method to create a git User:

Sudo useradd-M git

Sudo password git

 

7. Run gitosis (the current user is wirror800)

(1) Upload or copy the Public Key generated by the Administrator to the server. The public key here needs to be created by using the ssh-keygen-t rsa command under the GIT server administrator,

The method circulating on the internet is SCP/Your pub_path/id_rsa.pub $ {server_ip}: id_rsa.pub. Here we use the direct USB flash drive COPY method.

(2) initialize gitosis

Go to the directory of the copied id_rsa.pub: CD/tmp

Sudo chmod 777 id_rsa.pub

Sudo-H-u git gitosis-init <id_rsa.pub (you can also switch to the GIT user and execute gitosis-init <id_rsa.pub. Remember to switch exit to the current user)

At this point, some directories will be generated under the/home/git directory, If you want others to be able To clone the gitosis-admin.git, you need to do the following:

Sudo chmod 755/home/git/repositories/gitosis-admin.git/hooks/post-Update

So far, the installation of gitosis has been completed, and the related configuration can be operated by the Administrator, and then submitted to the server. The following is the gitweb installation configuration,

Skip the following 8 ~ Step 10.

 

8. Install gitweb (the current user is wirror800)

Sudo apt-Get install gitweb

 

9. Install apache2

Sudo apt-Get install apache2

 

10. Configure gitweb (the current user is wirror800)
(1) No CSS loading by default. Connect the static files used by gitweb to DocumentRoot:
CD/var/www/
Sudo ln-S/usr/share/gitweb /*.

(Pay attention to the following points)

(2) modify the configuration:

Sudo VI/etc/gitweb. conf

Change $ projectroot to gitosis-admin.git Directory:/home/git/Repositories

(3) modify the/home/git/repositories permission. By default, gitosis sets the repositories permission to unreadable.

Sudo chmod 777-r/home/git/Repositories

 

11. Edit The apache2 configuration file and create a web site (the current user is wirror800, which can be ignored by default)

(1) EDIT The apache2 configuration file

The default web directory in Ubuntu is/var/WWW, and the default CGI directory is/usr/lib/cgi-bin/. After gitweb is installed, gitweb. cgi of gitweb is automatically placed.

To this directory. If your cgi path is not the default/usr/lib/cgi-bin/, copy gitweb. cgi installed in/usr/lib/cgi-bin to the original configuration.

And modify the Apache configuration file/etc/apache2/Apache. conf:

Setenv gitweb_config/etc/gitweb. conf
The gitweb. conf configuration file is shown in the following figure: (you can modify it by yourself. We will not detail it here)
<Directory "/srv/www/cgi-bin/gitweb">
Options followsymlinks execcgi
Allow from all
AllowOverride all
Order allow, deny

<Files gitweb. cgi>
Sethandler CGI-script
</Files>
Rewriteengine on
Rewritecond % {request_filename }! -F
Rewritecond % {request_filename }! -D
Rewriterule ^. */gitweb. cgi/$0 [L, pt]
</Directory>

(2) Restart Apache: sudo/etc/init. d/apache2 restart to access http: // localhost/cgi-bin/gitweb. cgi

 

 

(2) git Administrator (run in git bash)

1. Install msysgit,

: Http://code.google.com/p/msysgit/downloads/list

Default installation.

 

2. Generate the server administrator public key.

Run the ssh-keygen-t rsa command to initialize gitosis on the server.

 

3. Clone gitosis-admin.git from server

First select a directory, such as G Disk: CD/G/

Then, create a directory mkdir gitproject

Git clone git @ your_server: gitosis-admin.git

(Your_server can be your server name or IP address. The Ubuntu IP address here is 192.168.1.102)


4. Edit gitosis. conf

CD gitosis-Admin

You will see the following files: gitosis. conf and keydir/. gitosis. conf are the configuration files of gitosis for configuring users and permissions. keydir/is owned by all group members.

Public Key.

VI gitosis. conf: Open the gitosis. conf file with the following content:

[Group gitosis-admin]
Writable = gitosis-Admin
Members = administrator @ Lilin

This is the Management Group permission. The username in members is the username in the uploaded public key.


Modify the content:

[Gitosis]
Gitweb = Yes

[Group gitosis-admin]
Writable = gitosis-Admin
Members = administrator @ Lilin wirror800@192.168.1.102

[Group apps]
Writable = apps
Members = administrator @ Lilin wirror800@192.168.1.102

Here, gitweb = Yes indicates that gitweb support is enabled. A group called apps is defined here, granting administrator @ Lilin and wirror800@192.168.1.102

Permission for two users to write the repo "apps"

After being pushed to the server, the server immediately takes effect.

Git commit-a-m "created a new repository"

Git push

In this way, the server permissions are updated.

 

5. Create an app Repo

Mkdir apps

CD apps

Git init


6. Create and edit. gitignore, which is used to ignore some content that does not require code management. For example, Rails Applications may be as follows:

. Ds_store
Log/*. Log
TMP /**/*
Config/database. yml
DB/*. sqlite3


7. Edit. Git/description, and add the description of apps.

8. Submit code

Git remote add origin git @ your_server: Apps. Git

Git add.

Git commit-am "imitial import"

Git push origin master: refs/heads/Master

9. Add the Member's public key to the system.

CD ../gitosis-Admin

CP member_1.pub keydir/

CP member_2.pub keydir/git

Add keydir/member_1.pub keydir/member_2.pub

 

Modify gitosis. conf and submit

[Group apps]
-Members = wirror800
+ Members = wirror800 member_1 member_2
Writable = apps

 

Git commit-a-m "granted new members commit rights to apps"
Git push

 

10. Other members get the apps code

Git clone git @ your_server: Apps. Git

 

It is also possible to install the GIT server in windows. We started to try cygwin, but encountered difficulties when using gitweb. Git was born after all

Linux, we recommend that you use Linux to build a git server. Mr. Zhang also provides a way to install the GIT server in windows.

For everyone: http://www.timdavis.com.au/git/setting-up-a-msysgit-server-with-copssh-on-windows. Thanks again to CTO mr. Zhang for these days

I have benefited a lot from my persistent dedication.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.