LoginSignup
9
6

More than 5 years have passed since last update.

CentOS7 Gitリポジトリをsmart http + nginxで共有する

Last updated at Posted at 2016-05-12

git(smart http) + fcgiwrap + nginx で、Gitの公開リポジトリを設定します。
Centos7用の手順です。

nginxは他のアプリと共存できるように、/gitディレクティブに対応させました。

gitリポジトリ

gitのインストール

$ sudo yum isntall git

git ユーザーの作成

$ sudo useradd -s /sbin/nologin git

ベースディレクトリ作成

$ sudo mkdir /var/gitrepos
$ sudo chown git:nginx /var/gitrepos
$ sudo chmod 2775 /var/gitrepos

空のリポジトリ作成

例としてリポジトリsampleを作成します。

$ sudo -u git git init --bare --shared=group  /var/gitrepos/sample.git
$ sudo -u git GIT_DIR=/var/gitrepos/sample.git git update-server-info
$ sudo -u git GIT_DIR=/var/gitrepos/sample.git git config http.receivepack true
$ sudo -u git cp /var/gitrepos/sample.git/hooks/post-update.sample /var/gitrepos/sample.git/hooks/post-update

nginx

インストール

インストール済みであれば飛ばしてください。
epelからインストールしていますが、最新版利用する場合は本家nginx.orgのリポジトリからインストールする手順でインストールしてください。

$ sudo yum install epel-release
$ sudo yum install nginx

default.d/*.conf のincludeを外している場合は適宜includeしてください。

$ sudo vi /etc/nginx/default.d/gitrepo.conf
/etc/nginx/default.d/gitrepo.conf
location /git {

  # BASIC認証入れない場合はコメントアウト
  auth_basic            "Restricted";
  auth_basic_user_file  /var/lib/git/htpasswd;

  access_log  /var/log/nginx/git_access.log  main;
  error_log   /var/log/nginx/git_error.log;

  proxy_read_timeout    300;
  proxy_connect_timeout 300;
  proxy_redirect        off;

  fastcgi_split_path_info ^(/git)(/[^/]+\.git/.*);

  fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
  fastcgi_param GIT_PROJECT_ROOT /var/gitrepos;
  fastcgi_param GIT_HTTP_EXPORT_ALL "";
  fastcgi_param PATH_INFO $fastcgi_path_info;
  include fastcgi_params;

  fastcgi_pass unix:/var/lib/git/fcgiwrap.sock;
}

BASIC認証

設定で指定したhtpasswdファイルを作成します。

$ sudo yum install httpd-tools
$ sudo htpasswd -cs /var/lib/git/htpasswd ユーザー名

firewallポート開放

$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --reload

fcgiwrap

fcgiwrapインストール

Centos7ではGitHubのソースからmakeするしかなさそう。

makeするにはautoconf automake fcgi-devel が必要なので予めインストールします。
epel入れてない場合はsudo yum install epel-releaseしてください。

$ sudo yum install fcgi-devel automake autoconf spawn-fcgi

リポジトリからcloneして、READMEにあるinstallation手順にしたがってインストールします。

$ git clone https://github.com/gnosek/fcgiwrap
$ cd fcgiwrap
$ autoreconf -i
$ ./configure
$ make
$ sudo make install

spawn-fcgi設定

不用意にスクリプト実行されないよう、soketのユーザーをgitにしています。
さらにsocketのアクセス制限のため、nginxグループで書き込みするようにしています。

$ sudo mkdir /var/lib/git
$ sudo chown git:nginx /var/lib/git
$ sudo vi /etc/sysconfig/spawn-fcgi

末尾に追加。

/etc/sysconfig/spawn-fcgi
# You must set some working options before the "spawn-fcgi" service will work.
# If SOCKET points to a file, then this file is cleaned up by the init script.
#
# See spawn-fcgi(1) for all possible options.
#
# Example :
#SOCKET=/var/run/php-fcgi.sock
#OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
OPTIONS="-u git -g nginx -s /var/lib/git/fcgiwrap.sock -S -M 0660 -F 4 -P /var/lib/git/spawn-fcgi.pid -- /usr/local/sbin/fcgiwrap"

fcgi起動

$ sudo systemctl enable spawn-fcgi
$ sudo systemctl start spawn-fcgi

nginxをリロードします。

$ sudo systemctl reload nginx

SELinux無効化

一時的。

$ sudo setenforce 0

恒久

$ sudo vi /etc/selinux/config

SELINUX=permissive

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

テスト

$ git clone http://localhost/git/sample.git
$ cd sample
$ echo "README" >> README
$ git add .
$ git commit -m "test commit"
$ git push origin master

注意

リポジトリは適宜追加してください。
パブリックなネットワークに公開する場合は、かならず認証を設定し、 TLS(ssl)を通すなどしてください。

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6