CentOS 7.2 安裝Gerrit 2.14.6

1.環境

本文使用VMWare虛擬機進行實驗。html

  • 2核CPU,4GB內存,20GB硬盤,IP:192.168.159.131
  • CentOS 7.2最小安裝(CentOS-7-x86_64-Minimal-1511.iso)

2.需求

Gerrit支持多種數據庫、多種認證方式(參考官網手冊),這裏使用java

  • mariadb數據庫
  • http認證
  • Apache http server做爲web服務器,提供反向代理
  • gitweb做爲Repository Browser

3.安裝

3.1 安裝軟件

yum -y install git gitweb httpd mariadb-server

 

3.2 配置數據庫

systemctl start mariadb
mysql

 

鍵入以下內容,這裏密碼是secretmysql

CREATE USER 'gerrit2'@'localhost' IDENTIFIED BY 'secret';
CREATE DATABASE reviewdb DEFAULT CHARACTER SET 'utf8';
GRANT ALL ON reviewdb.* TO 'gerrit2'@'localhost';
FLUSH PRIVILEGES;

 

3.3 配置httpd反向代理

vim /etc/httpd/conf.d/gerrit.conf

 

鍵入以下內容linux

<VirtualHost *>
    ServerName 192.168.159.131

    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On

    <Proxy *>
          Order deny,allow
          Allow from all
    </Proxy>

    <Location /login/>
      AuthType Basic
      AuthName "Gerrit Code Review"
      AuthBasicProvider file
      AuthUserFile /gerrit.password
      Require valid-user
    </Location>

    AllowEncodedSlashes On
    ProxyPass / http://127.0.0.1:8081/
</VirtualHost>

注意,git

  • ServerName根據本身實際狀況來定, 我用的本機IP
  • gerrit的http認證方式,須要使用HTTP基本認證,這裏使用文件(/gerrit.password,httpd要有訪問權限才行)方式認證。
  • 反向代理設置,從根目錄"/",代理到"http://127.0.0.1:8081/",注意端口8001後面有個斜線。8081是gerrit監聽的端口,後面會提到。

 

啓動httpd,將監聽80端口(反向代理到8081端口)web

systemctl start httpd

 

3.4 添加HTTP認證用戶

touch /gerrit.passwd
htpasswd /gerrit.passwd "root"
New password: 
Re-type new password: Adding password for user root

這裏添加了用戶root,密碼123456(跟Linux本地用戶沒有關係!)sql

這樣首次訪問gerrit時的用戶將成爲gerrit的管理員,若想再添加用戶,須要先在/gerrit.passwd中添加,再登陸gerrit。數據庫

 

3.5 安裝JRE

官網下載Java SE Runtime Environment 8u162 Linux x64vim

jre-8u162-linux-x64.tarapi

tar zxvf jre-8u162-linux-x64.tar -C /opt
export JAVA_HOME=/opt/jre1.8.0_162 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/toos.jar

 

3.6 安裝gerrit

3.6.1 添加用戶
sudo adduser gerrit2
sudo su gerrit2

官網下載gerrit的war包gerrit-2.14.6.war,這裏放至gerrit2的home目錄

 

3.6.2 建立工做目錄
mv gerrit-2.14.6.war gerrit.war
mkdir
gerrit-test

 

3.6.3 安裝

進入交互命令,藍色粗體表示輸入的內容

[gerrit2@ted ~]$ java -jar gerrit.war init -d gerrit-test/
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2018-01-28 13:14:14,823] [main] INFO  com.google.gerrit.server.config.GerritServerConfigProvider : No /home/gerrit2/gerrit-test/etc/gerrit.config; assuming defaults

*** Gerrit Code Review 2.14.6
*** 

Create '/home/gerrit2/gerrit-test' [Y/n]y

*** Git Repositories
*** 

Location of Git repositories   [git]: #回車 *** SQL Database
*** 

Database server type           [h2]: mariadb

Gerrit Code Review is not shipped with MariaDB Connector/J 1.5.9
**  This library is required for your configuration. **
Download and install it now [Y/n]? y
Downloading https://repo1.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/1.5.9/mariadb-java-client-1.5.9.jar ... OK
Checksum mariadb-java-client-1.5.9.jar OK
Server hostname                [localhost]: #回車
Server port                    [(mariadb default)]: #回車
Database name                  [reviewdb]: #回車
Database username              [gerrit2]: #回車
gerrit2's password             : #secret
              confirm password : #secret *** Index
*** 

Type                           [lucene/?]: #回車 *** User Authentication
*** 

Authentication method          [openid/?]: http
Get username from custom HTTP header [y/N]? #回車
SSO logout URL                 : #回車
Enable signed push support     [y/N]? #回車

*** Review Labels
*** 

Install Verified label         [y/N]? #回車

*** Email Delivery
*** 

SMTP server hostname           [localhost]: #回車
SMTP server port               [(default)]: #回車
SMTP encryption                [none/?]: #回車
SMTP username                  : co3@qq.com
co3@qq.com's password          : #123456
              confirm password : #123456 *** Container Process
*** 

Run as                         [gerrit2]: #回車
Java runtime                   [/opt/jre1.8.0_162]: #回車
Copy gerrit.war to gerrit-test/bin/gerrit.war [Y/n]? #回車
Copying gerrit.war to gerrit-test/bin/gerrit.war

*** SSH Daemon
*** 

Listen on address              [*]: #回車
Listen on port                 [29418]: #回車
Generating SSH host key ... rsa... dsa... ed25519... ecdsa 256... ecdsa 384... ecdsa 521... done

*** HTTP Daemon
*** 

Behind reverse proxy           [y/N]? y
Proxy uses SSL (https://)      [y/N]? #回車
Subdirectory on proxy server   [/]: #回車
Listen on address              [*]: 127.0.0.1
Listen on port                 [8081]: #回車
Canonical URL                  [http://localhost/]: http://192.168.159.131

*** Cache
*** 


*** Plugins
*** 

Installing plugins.
Install plugin commit-message-length-validator version v2.14.6 [y/N]? y
Installed commit-message-length-validator v2.14.6
Install plugin download-commands version v2.14.6 [y/N]? y
Installed download-commands v2.14.6
Install plugin hooks version v2.14.6 [y/N]? y
Installed hooks v2.14.6
Install plugin replication version v2.14.6 [y/N]? y
Installed replication v2.14.6
Install plugin reviewnotes version v2.14.6 [y/N]? y
Installed reviewnotes v2.14.6
Install plugin singleusergroup version v2.14.6 [y/N]? y
Installed singleusergroup v2.14.6
Initializing plugins.

Initialized /home/gerrit2/gerrit-test
Executing /home/gerrit2/gerrit-test/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on 192.168.118.158:80 ... OK
Opening http://192.168.118.158/#/admin/projects/ ...FAILED #(由於尚未用戶,因此failed,不影響)
Open Gerrit with a JavaScript capable browser:
http://192.168.118.158/#/admin/projects/
[gerrit2@ted ~]$

 

完成後查看端口狀況

[root@ted ~]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2718/mysqld         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1125/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1563/master         
tcp6 0 0 :::29418 :::* LISTEN 3118/GerritCodeRevi tcp6 0 0 :::80 :::* LISTEN 2465/httpd tcp6 0 0 127.0.0.1:8081 :::* LISTEN 3118/GerritCodeRevi 
tcp6       0      0 :::22                   :::*                    LISTEN      1125/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1563/master         
[root@ted ~]# 

 

這樣初始化好後,gerrit的配置文件(gerrit-test/etc/gerrit.config)

[gerrit]
        basePath = git serverId = 70a67d2b-e12e-42b9-a978-705d7685654d canonicalWebUrl = http://192.168.118.145 [database] type = mariadb hostname = localhost database = reviewdb username = gerrit2 [index] type = LUCENE [auth] type = HTTP [receive] enableSignedPush = false [sendemail] smtpServer = localhost smtpUser = co3@qq.com [container] user = gerrit2 javaHome = /opt/jre1.8.0_162 [sshd] listenAddress = *:29418 [httpd] listenUrl = proxy-http://127.0.0.1:8081/ [cache] directory = cache

 

4.測試

使用瀏覽器訪問canonicalWebUrl ,

http://192.168.118.145

提示輸入用戶密碼,就是/gerrit.password裏的http認證用戶密碼

 

登陸成功後,將看到gerrit用戶信息登記界面(添Full Name什麼的),而後進入主界面

管理命令以下

./gerrit-test/bin/gerrit.sh start
./gerrit-test/bin/gerrit.sh stop
./gerrit-test/bin/gerrit.sh status

5.配置gitweb

參考官網手冊,可使用命令行實現,我這裏直接編輯配置文件,

vi gerrit-test/etc/gerrit.config

 

添加以下gitweb配置

[gitweb]
        cgi = /var/www/git/gitweb.cgi
        type = gitweb

 

注意!此處有坑!若是按官網手冊用命令配置,type是沒有值的,而默認是disabled,不會顯示gitweb超連接!必須手動設置!)

 

 

 重啓gerrit,退出web窗口,從新認證登陸,能夠看見gitweb超連接了

瀏覽hey項目,

 

6.配置郵箱驗證

若是按前面默認配置,使用本機smtp發送郵件,這樣若是發到公網郵箱(qq、163等),會被斷定爲垃圾郵件,發不出去。

若是是實驗性質的話,能夠在局域網搭建postfix+dovecot+dnsmasq簡單郵件系統,進行郵箱註冊。

(gerrit的帳號必須註冊郵箱,添加ssh公鑰/http密碼,git 配置郵箱必須跟註冊郵箱一致,才能push)

 

我再看看其餘解決垃圾郵件方法...

相關文章
相關標籤/搜索