Apache 搭建git 服务器

本文介绍如何在Windows环境中使用Apache配置Git服务器,并通过PHP界面简化仓库管理。

原文地址:http://www.jeremyskinner.co.uk/2010/07/31/hosting-a-git-server-under-apache-on-windows/


Last month I posted abouthosting a git server under IISby usingGitAspx. While this is certainly one way to host a git server on windows, I wouldn’t recommend this in a production environment.

An alternative (and somewhat more stable) approach is to use the native implementation of git-http-backend that ships withmsysgitalong with Apache.


Step 1: Install Git

Firstly you’ll need to installmsysgit. The current stable version is 1.7.0.2, but this process should also work with the 1.7.1 beta. Be sure to selectRun git from the Windows Command promptwhen the installer asks you if you want to modify your PATH variable.

Once installed, you’ll need to tweak the installation slightly. By default, the git http server is located at C:Program Files (x86)Gitlibexecgit-coregit-http-backend.exe(on x64 systems). If you try and run git-http-backend.exe you’ll get the message that the application couldn’t be started because libiconv2.dll is missing:

image

In order to fix this, copy libiconv2.dll fromC:Program Files (x86)GitbintoC:Program Files (x86)Gitlibexecgit-core

Now when you run git-http-backend.exe from a command prompt, the application should run and you should see an HTTP 500 server error:

image

Step 2: Install Apache

Next you’ll need to install the Apache webserver. I’m using the 2.2.16 installerwhich can be found here. I ran through the installation using the default options so my Apache instance is running on port 80.

If you visit http://localhost at this point you should be greeted with Apache’s standard “It works!” message.

Step 3: Create Repositories Directory

Create the directory where you want to store your git repositores. I’m usingC:Repositories.For testing purposes I created an empty test repository:

image

You’ll also need to put some content in this test repositor

Step 4: Modify Apache Configuration

Next, you’ll need to modify the Apache configuration file so that it forwards requests to git-http-backend.exe. This is done by editinghttpd.confinC:Program Files (x86)Apache Software FoundationApache2.2conf

At the bottom of httpd.conf, add the following lines:

SetEnv GIT_PROJECT_ROOT C:/Repositories
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAliasMatch \
        "(?x)^/(.*/(HEAD |  info/refs |  objects/(info/[^/]+ |  [0-9a-f]{2}/[0-9a-f]{38} |  pack/pack-[0-9a-f]{40}.(pack|idx)) | git-(upload|receive)-pack))$" \
       "C:/Program Files (x86)/git/libexec/git-core/git-http-backend.exe/$1"



The first line tells git where your repositories are located. The second line tells git that all repositories in this directory should be published over http (by default, git will only publish those repositories that contain a file named “git-daemon-export-ok”). The final lines tell apache to route git-specific URLs to the git http server.

Finally, if you want to be able to clone from the server without authentication, then you’ll need to tell Apache to allow anonymous access by adding the following lines into httpd.conf:

<Directory />
  Allow from all
</Directory>

After saving the changes, restart the Apache service.

Step 5: Clone the test repository

Next, clone the test repository that you crated in step 3 by issuing the command git clone http://localhost/Test.git

If all goes well, you should see the following output:

image

At this point, you can now clone repositories from the server without any authentication.

Step 6: Authentication

If you try to push changes to the repository you cloned in step 5, you’ll receive an error:

image


This is because by default, you can only pull from repositories anonymously, while pushing requires authentication to be enabled.

Scenario 1: Allow anonymous pushes

Sometimes you may want to allow users to push to your repositories without authentication, for example when using an internal, privately hosted server.

To enable this scenario, edit theconfigfile in C:RepositoriesTest.git on the server and add the following lines to the bottom of the file:

[http]
  receivepack = true

This will allow git to accept pushes from anonymous users.

Note that you’ll have to add this to every repository that you create. I’ll show a nicer way to do this later in the tutorial.

Scenario 2: Anonymous pull, authenticated push

This is the default scenario. Git will only allow users to push if they have been authenticated with apache.

There are several ways to enable user accounts with apache. The most basic is to use .htaccess files, although you can also configure integration with Windows user accounts and Active Directory by using mod_authnz_ldap. Configuring these is outside the scope for this tutorial, but there are plenty of examples on the internets.

Once authentication is set up, you’ll need to ensure that you clone your repositories with the username in the URL, as git will not prompt you for a username by default:

git clone http://MyUserName@mygitserver/Test.git

Git will then prompt you for a password every time that you try to push. You can also hard code the password in the URL (somewhat insecure) if you want to avoid this prompt:

git clone http://MyUserName:Password@mygitserver/Test.git

To make this more secure, you could enable SSL on the server and require authenticated traffic to go over HTTPS. Although configuring OpenSSL with apache is outside the scope for this tutorial, I will point out that once configured, you will need to disable the SSL verification on your git client by running:

git config --global http.sslverify false

If you don’t do this, you’ll get an error saying“error setting certificate verify locations”every time you try to clone/push/pull over HTTPS.

Step 7: A prettier UI

At this point, you should be able to clone, pull from and push to the server. However, creating new repositories requires that you connect remotely to the server and run git init from a command prompt on the server.

A nicer alternative is to use a web-based front for the creation of repositories. For this I’ll be usingGitPhpHomepagewhich is a small collection of PHP scripts that I ported fromGitAspx'sASP.NET-based UI to PHP in order to get it working under Apache.

First, you’ll need to install PHP on the server. I’ll be using the PHP 5.3.3 Windows binaries that can be found athttp://windows.php.net/download/. The download page is somewhat confusing, offering both thread-safe and non-thread-safe versions compiled with both VC6 and VC9. For use with Apache 2.2 be sure to select theVC6 x86 Thread Safe zip package. Here’sa direct link.

Unzip the contents of this package toC:PHPon the server and add this directory to Windows’ PATH environment variable:

image

Next, rename the php.ini-production file to just php.ini and edit the following settings:

Uncomment and edit the “extension_dir” (about half way through the file) so that it says the following:

extension_dir = "c:phpext"

Next, edit your Apache configuration file (C:Program Files (x86)Apache Software FoundationApache2.2confhttpd.conf) and add the following lines to the bottom of the file:

AddType application/x-httpd-php .php
LoadModule php5_module "C:/php/php5apache2_2.dll"
PHPIniDir "C:/php"

This tells Apache to map .php file extensions to the PHP5 apache module located in C:php.

You’ll also need to tell Apache to look for index.php as a default index file. This can be done by searching for the lines that look like this:

<IfModule dir_module>
  DirectoryIndex index.html
</IfModule>

…and changing them to this:

<IfModule dir_module>
  DirectoryIndex index.php index.html
</IfModule>

Be sure to restart the Apache server once you’ve made these changes.

To see whether this is working, create a file called phpinfo.php in C:Program Files (x86)Apache Software FoundationApache2.2htdocs and place in it the following content:

<?php phpinfo(); ?>

Now, visiting http://mygitserver/phpinfo.php should display a page containing PHP configuration information.

Now that PHP is configured, download the GitPhpHomepage files fromhttp://github.com/JeremySkinner/GitPhpHomepageand unzip them into C:Program Files (x86)Apache Software FoundationApache2.2htdocs

Be sure to edit the config.php file so that it accurately reflects both the git installation directory and your repositories directory.

At this point, visiting http://mygitserver should display a page where you can view and create repositories:

image

Pressing the “Create a new bare repository” button will open a dialog where you can create a new repository, including the option to enable anonymous pushes:

image


Obviously, if you’re thinking of using this on a public-facing server you should enable authentication so that not just anyone can create repositories on your server.
搭建和配置 Git 服务器的高级指南需要从多个方面进行深入操作,包括用户权限管理、SSH 密钥认证、仓库管理、自动化钩子(hooks)、备份与恢复机制等。以下是一些关键步骤和配置建议: ### 用户权限管理 为了更好地控制访问权限,可以使用 `gitolite` 或 `GitLab` 等工具来管理 Git 服务器上的用户权限。这些工具支持细粒度的权限控制,例如针对特定分支或文件的读写权限。 ```bash # 安装 gitolite3 sudo apt-get install gitolite3 ``` 创建一个专门用于管理 Git 的用户,并为其设置 SSH 密钥: ```bash # 创建 git 用户 sudo adduser git # 切换到 git 用户 su - git # 初始化 gitolite gl-setup /path/to/admin.pub ``` ### SSH 密钥认证 确保所有客户端使用 SSH 密钥认证方式连接 Git 服务器。这不仅提高了安全性,还简化了自动化脚本的编写。 在服务器端添加用户的公钥到 `~/.ssh/authorized_keys` 文件中: ```bash cat client_public_key >> ~/.ssh/authorized_keys ``` ### 自动化钩子(Hooks) Git 提供了多种钩子(hooks),可以在特定事件发生时触发自定义脚本。例如,在提交代码后自动部署代码或者运行测试。 示例:在 `post-receive` 钩子中自动部署代码: ```bash #!/bin/sh GIT_WORK_TREE=/var/www/html git checkout -f ``` ### 多仓库管理 如果需要管理多个仓库,可以使用 `gitolite` 或 `GitLab` 来集中管理所有仓库。这些工具提供了 Web 界面,方便管理和监控。 ### 备份与恢复 定期备份 Git 仓库是非常重要的。可以使用 `git bundle` 命令创建仓库的备份包,也可以使用第三方工具如 `rsync` 进行增量备份。 示例:使用 `git bundle` 创建备份: ```bash git bundle create repo_backup.bundle --all ``` 恢复备份: ```bash git clone repo_backup.bundle repo_name ``` ### HTTPS 支持 如果你希望通过 HTTPS 访问 Git 仓库,可以配置 Apache 或 Nginx 作为反向代理。这样不仅可以提供更安全的访问方式,还可以利用 SSL/TLS 加密传输数据。 示例:配置 Apache 使用 HTTPS: ```apache <VirtualHost *:443> ServerName git.example.com DocumentRoot /var/www/git SSLEngine on SSLCertificateFile "/etc/ssl/certs/server.crt" SSLCertificateKeyFile "/etc/ssl/private/server.key" <Directory /var/www/git> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost> ``` ### 日志与监控 启用详细的日志记录可以帮助你更好地了解 Git 服务器的运行状态。可以通过修改 `git config` 设置日志级别,或者使用第三方监控工具如 Prometheus 和 Grafana 进行实时监控。 示例:启用详细日志记录: ```bash git config --global core.logAllRefUpdates true ``` ### 性能优化 对于大型项目,Git 服务器的性能可能会成为瓶颈。可以通过以下几种方式进行优化: - 使用 SSD 硬盘提高 I/O 性能; - 启用压缩选项减少网络带宽占用; - 使用缓存机制加速常用操作。 示例:启用压缩: ```bash git config --global pack.threads 0 git config --global compression.level 9 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值