知识总结

CODE IS POETRY

centos7 安装配置git+nginx

一、nginx安装

1
yum install –y nginx

输入命令‘nginx’ 启动,浏览器http://ip查看,如出现nginx默认页则启动成功

安装git

1
yum install –y git

git –version查看是否安装成功

三、配置http git

1、安装fcgi

1
2
3
cd /usr/local/src
git clone https://github.com/lighttpd/spawn-fcgi.git
cd spawn-fcgi && ./autogen.sh && ./configure && make && make install

如出现:./autogen.sh: line 11: autoreconf: command not found执行:

1
yum install install autoconf automake libtool

继续执行:

1
yum -y install fcgi-devel

如无法找到安装包,先:

1
yum install epel-release

2、安装fcgi的管理工具

1
2
3
cd /usr/local/src
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap && autoreconf -i && ./configure && make && make install

3、配置启动脚本

1
vi /etc/init.d/fcgiwrap

写入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /bin/bash
### BEGIN INIT INFO
# Provides: fcgiwrap
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: FastCGI wrapper
# Description: Simple server for running CGI applications over FastCGI
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SPAWN_FCGI="/usr/local/bin/spawn-fcgi"
DAEMON="/usr/local/sbin/fcgiwrap"
NAME="fcgiwrap"

PIDFILE="/var/run/$NAME.pid"

FCGI_SOCKET="/var/run/$NAME.socket"
FCGI_USER="nginx"
FCGI_GROUP="nginx"
FORK_NUM=5
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
echo -n "Starting $NAME... "

PID=`pidof $NAME`
if [ ! -z "$PID" ]; then
echo " $NAME already running"
exit 1
fi

$SPAWN_FCGI -u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -P $PIDFILE -F $FORK_NUM -f $DAEMON

if [ "$?" != 0 ]; then
echo " failed"
exit 1
else
echo " done"
fi
;;

stop)
echo -n "Stoping $NAME... "

PID=`pidof $NAME`
if [ ! -z "$PID" ]; then
kill `pidof $NAME`
if [ "$?" != 0 ]; then
echo " failed. re-quit"
exit 1
else
rm -f $pid
echo " done"
fi
else
echo "$NAME is not running."
exit 1
fi
;;

status)
PID=`pidof $NAME`
if [ ! -z "$PID" ]; then
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped"
exit 0
fi
;;

restart)
$SCRIPTNAME stop
sleep 1
$SCRIPTNAME start
;;

*)
echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
exit 1
;;
esac

注意spawn-fcgi跟fcgiwrap脚本路径及FCGI_GROUP跟FCGI_GROUP
让它可执行:

1
2
3
chmod a+x /etc/init.d/fcgiwrap
chkconfig --level 35 fcgiwrap on
/etc/init.d/fcgiwrap start

4、安装httpd工具包,用于管理http密码

1
yum -y install httpd-tools

4、创建一个http账号,先创建文件夹

1
2
3
cd /home
mkdir httpwd
htpasswd -c /home/httpwd/gitpass.db git

重复输入再次密码,就创建了git账号,记住密码

四、 修改nginx配置,启动git http

1、创建一个git项目

1
2
3
cd /home
mkdir git-repos
git init --bare test.git

此时创建了test.git项目,可通过git clone root@xxxx.xxxx.xxxx:/home/git-repos/test.git克隆

2、添加nginx配置

1
vi  /etc/nginx/conf.d/test.conf

增加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
listen 80;
server_name 有域名写域名,无域名写服务器ip;

client_max_body_size 100m;

auth_basic "Git User Authentication";
auth_basic_user_file /home/httpwd/gitpass.db;

location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
root /home/git-repos;
}

location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
root /home/git-repos;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_connect_timeout 24h;
fastcgi_read_timeout 24h;
fastcgi_send_timeout 24h;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend
fastcgi_param PATH_INFO $uri;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /home/git-repos
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
}
}

注意认证文件gitpass.db路径,注意root路径,为git创建时的路径
注意git-http-backend路径 , 到git的安装目录下去找,可执行命令查看:

1
rpm -ql git

重启nginx

1
nginx –s reload

此时可通过git clone http://ip/test.git 克隆项目了


© 2020 Tung

粤ICP备19047572号