设为首页 收藏本站
查看: 749|回复: 0

[经验分享] Apache 搭建git 服务器

[复制链接]

尚未签到

发表于 2017-1-1 08:10:06 | 显示全部楼层 |阅读模式



原文地址: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:


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:


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:

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 commandgit clone
http://localhost/Test.git

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

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:



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:

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:

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:



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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-322174-1-1.html 上篇帖子: git 整合apache 下篇帖子: Git分布式版本控制教程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表