本文使用VMWare虛擬機進行實驗。html
Gerrit支持多種數據庫、多種認證方式(參考官網手冊),這裏使用java
yum -y install git gitweb httpd mariadb-server
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;
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
啓動httpd,將監聽80端口(反向代理到8081端口)web
systemctl start httpd
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。數據庫
從官網下載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
sudo adduser gerrit2 sudo su gerrit2
從官網下載gerrit的war包gerrit-2.14.6.war,這裏放至gerrit2的home目錄
mv gerrit-2.14.6.war gerrit.war
mkdir gerrit-test
進入交互命令,藍色粗體表示輸入的內容
[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
使用瀏覽器訪問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
參考官網手冊,可使用命令行實現,我這裏直接編輯配置文件,
vi gerrit-test/etc/gerrit.config
添加以下gitweb配置
[gitweb] cgi = /var/www/git/gitweb.cgi type = gitweb
(注意!此處有坑!若是按官網手冊用命令配置,type是沒有值的,而默認是disabled,不會顯示gitweb超連接!必須手動設置!)
重啓gerrit,退出web窗口,從新認證登陸,能夠看見gitweb超連接了
瀏覽hey項目,
若是按前面默認配置,使用本機smtp發送郵件,這樣若是發到公網郵箱(qq、163等),會被斷定爲垃圾郵件,發不出去。
若是是實驗性質的話,能夠在局域網搭建postfix+dovecot+dnsmasq簡單郵件系統,進行郵箱註冊。
(gerrit的帳號必須註冊郵箱,添加ssh公鑰/http密碼,git 配置郵箱必須跟註冊郵箱一致,才能push)
我再看看其餘解決垃圾郵件方法...