1.环境
本文使用VMWare虚拟机进行实验。
- 2核CPU,4GB内存,20GB硬盘,IP:192.168.159.131
- CentOS 7.2最小安装(CentOS-7-x86_64-Minimal-1511.iso)
2.需求
Gerrit支持多种数据库、多种认证方式(参考官网手册),这里使用
- mariadb数据库
- http认证
- HTTPd作为web服务器,提供反向代理
- gitweb作为Repository Browser
3.安装
3.1 安装软件
yum -y install git gitweb httpd mariadb-server
3.2 配置数据库
systemctl start mariadb
mysql
键入如下内容,这里密码是secret
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
键入如下内容
<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>
注意,
- ServerName根据自己实际情况来定, 我用的本机IP
- gerrit的http认证方式,需要使用HTTP基本认证,这里使用文件(/gerrit.password,httpd要有访问权限才行)方式认证。
- 反向代理设置,从根目录"/",代理到"http://127.0.0.1:8081/",注意端口8001后面有个斜线。8081是gerrit监听的端口,后面会提到。
启动httpd,将监听80端口(反向代理到8081端口)
systemctl start httpd
3.4 添加认证用户
touch /gerrit.passwd htpasswd /gerrit.passwd "root"
New password:
Re-type new password:
Adding password for user root
这里添加了用户root,密码123456(跟linux本地用户没有关系!)
这样首次访问gerrit时的用户将成为gerrit的管理员,若想再添加用户,需要先在/gerrit.passwd中添加,再登录gerrit。
3.5 安装JRE
从官网下载Java SE Runtime Environment 8u162 Linux x64
jre-8u162-linux-x64.tar
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 /opt/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
......
[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里的用户密码
登录成功后,将看到gerrit用户信息登记界面(添Full Name什么的),然后进入主界面
管理命令如下
./gerrit-test/bin/gerrit.sh start ./gerrit-test/bin/gerrit.sh stop ./gerrit-test/bin/gerrit.sh status