腾讯云上个人服务器(Centos6.9)的搭建(四) Git服务器搭建(通过https验证)

先安装git:

下载
#wget https://github.com/git/git/archive/v2.9.2.tar.gz
解压
# tar zxvf v2.9.2.tar.gz
# cd git-2.9.2
编译安装
# make configure
# ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv
# make all doc
# sudo make install install-doc install-html
修改环境变量
# sudo vim /etc/profile
在最后一行添加
export PATH=/usr/local/git/bin:$PATH
保存后使其立即生效
# source /etc/profile
查看是否安装成功
#git --version
显示 git version 2.9.2 ,安装完成。

安装apache:

安装所需软件包 ,gcc通常默认带,估计云主机为了省空间给省略了。openssl和openssl-devel也是必须的
#yum -y install gcc openssl openssl-devel
下载Apache源码,版本2.2.29
#wget  http://archive.apache.org/dist/httpd/httpd-2.2.29.tar.gz
编译带ssl,安装到/usr/local/apache2目录
#./configure --enable-ssl --enable-so -–prefix=/usr/local/apache2
#make & make install
正常安装完成。

设置apache的自启动:

先查看启动级别,不是3的话改为3
#vi /etc/inittab
id:3:initdefault:
直接将上述的apachectl文件拷贝到 /etc/rc.d/init.d 目录中,命名为习惯用的httpd
#cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
在脚本上加上注释,以便让chkconfig认识
#vi /etc/rc.d/init.d/httpd
在脚本第二行加上如下注释,2345是默认运行级别,61启动顺序,39停止顺序,可以自己指定
# chkconfig: 2345 61 39  #在脚本第二行开始添加
# description: Apache #centos6以后版本可加可不加,最好加上
追加自启动
# chkconfig --add httpd 
默认开启2345运行级别,当然可以通过选项--level num指定运行级别
#chkconfig httpd on 
#chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

配置apache的https访问:

安装在阿里云申请的SSL证书安装
文件说明:
1. 证书文件1530652701965.pem,包含两段内容,请不要删除任何一段内容。
2. 如果是证书系统创建的CSR,还包含:证书私钥文件1530652701965.key、证书公钥文件public.pem、证书链文件chain.pem。
( 1 ) 在Apache的安装目录下创建cert目录,并且将下载的全部文件拷贝到cert目录中。如果申请证书时是自己创建的CSR文件,请将对应的私钥文件放到cert目录下并且命名为1530652701965.key;
( 2 ) 打开 apache 安装目录下 conf 目录中的 httpd.conf 文件,找到以下内容并去掉“#”:
#LoadModule ssl_module modules/mod_ssl.so (如果找不到请确认是否编译过 openssl 插件)
#Include conf/extra/httpd-ssl.conf
( 3 ) 打开 apache 安装目录下 conf/extra/httpd-ssl.conf 文件 (也可能是conf.d/ssl.conf,与操作系统及安装方式有关), 在配置文件中查找以下配置语句:
# 添加 SSL 协议支持协议,去掉不安全的协议
SSLProtocol all -SSLv2 -SSLv3
# 修改加密套件如下
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
SSLHonorCipherOrder on
# 证书公钥配置
SSLCertificateFile cert/public.pem
# 证书私钥配置
SSLCertificateKeyFile cert/1530652701965.key
# 证书链配置,如果该属性开头有 '#'字符,请删除掉
SSLCertificateChainFile cert/chain.pem
( 4 ) 重启 Apache。
( 5 ) 通过 https 方式访问站点,正常显示 It works!

设置访问git服务器的用户名密码

#建立git访问的用户名密码文件 
htpasswd -m -c /usr/local/apache2/conf/git-team.htpasswd zhang
chown apache:apache /usr/local/apache2/conf/git-team.htpasswd
chmod 640 /usr/local/apache2/conf/git-team.htpasswd
#创建了zhang的用户名密码,再追加用户的话,不要加-c  -m是选择MD5加密方式

建立空的git仓库

mkdir /git-data/git/sample.git -p
cd /git-data/git/sample.git
git init --bare
ll 可以看到里面的branches,hooks,hooks,info,objects,refs,config,description,HEAD

配置git的https访问:

#打开httpd.conf
vi /usr/local/apache2/conf/httpd.conf
找到下面内容,打开注释,没有的话可以自己追加上。
#Include conf/extra/httpd-ssl.conf
#打开/usr/local/apache2/conf/extra/httpd-ssl.conf
找到下面内容 <VirtualHost _default_:443>
在里面追加下面内容
DocumentRoot "/git-data/git"
ServerName www.域名:443
ServerAdmin 邮件地址
SetEnv GIT_HTTP_EXPORT_ALL
SetEnv GIT_PROJECT_ROOT /git-data/git
ScriptAlias /git/ /usr/local/git/libexec/git-core/git-http-backend/
<Location /git>
    AuthType Basic
    AuthName "Git"
    AuthUserFile /usr/local/apache2/conf/git-team.htpasswd
    Require valid-user
</Location>

重启apache: 

service httpd restart

确认可以通过Https访问git。

git checkout https://域名/git/sample.git
#提示输入用户名密码,输入刚才创建的zhang和密码,提示checkout了一个空文件夹

从个人电脑里把原来git里面的代码直接导入到搭建的git服务器上。

git clone --mirror 老repo的地址
cd 上面clone完的目录
git remote set-url origin https://域名/git/sample.git
git push -f origin

到此为止,git服务器(https验证)搭建完毕。

整理一下遇到的坑:

1,编译apache的时候必须指定ssl,否则只能重新编译安装。

2,把代码转移到自己搭建的服务器上时,可能会遇到  fatal: Unable to find remote helper for 'https' ,原因时curl-devel没装

#服务器上安装curl-devel
yum install curl-devel
#重新编译安装git后解决

猜你喜欢

转载自blog.csdn.net/zyldlxl/article/details/81255824