Ubuntu(乌班图)系统下搭建Gerrit服务器及使用过程中会遇到的一些问题

一、Java环境

  • 上oracle官网下载你需要的JDK,点击下载,然后把下载链接复制下来:
    Ubuntu(乌班图)系统下搭建Gerrit服务器及使用过程中会遇到的一些问题_第1张图片

  • 然后打开命令行输入,-o后面是输出的文件名(具体命令的含义自行去了解):

sudo wget http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-i586.tar.gz?AuthParam=1511336377_0f3a9752e4eeea9f3cc15496fd75b510 -o jdk8.tar.gz
  • 解压:

tar zxvf jdk8.tar.gz

  • 移动文件:

cp -r jdk1.8.0_152 /usr/lib/jdk

  • 修改环境变量:

vim ~/.bashrc

将以下内容复制进去:

export JAVA_HOME=/usr/lib/jdk
export JRE_HOME=${JAVA_HOME}/jre   
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib   
export PATH=${JAVA_HOME}/bin:$PATH

按esc,输入:wq保存退出,输入(应用):

source ~/.bashrc

  • 验证:

java -version

这样就成功了:

root@iZuf6guu7jd98r2y29a78lZ:~# java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) Server VM (build 25.152-b16, mixed mode)

二、安装Git

  • 输入git,如果没有安装,会提示你安装命令(apt-get与apt的区别是一个旧版命令和一个新版命令):

sudo apt install git

如果提示Unable to locate package,是因为新装的Ubuntu系统,没有update,输入:

sudo apt update

  • 生成SSH公钥:

ssh-keygen -t rsa -C 'your email'

输入密钥的存储路径或默认(~/.ssh/id_rsa),输入使用密钥时的口令或留空:

root@iZuf6guu7jd98r2y29a78lZ:~# ssh-keygen -t rsa -C 'gaochujia@koudailc.com' 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:  
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Usp9w0ftYUlbLb+eaA2aeMCYV6T6XH0/ylq+C44zISQ gaochujia@koudailc.com
The key's randomart image is:
+---[RSA 2048]----+
|         .+o=o   |
|         oo+o    |
|       +o. o.    |
|   E oo++o .o.   |
|    o +.So=o.+. .|
|     . o.=+++ ...|
|      . .o+= . ..|
|       oo + o . .|
|       .oo.=+.   |
+----[SHA256]-----+
  • 查看公钥:

cat ~/.ssh/id_rsa.pub

三、安装Apache

sudo apt install apache2

检验:

apache2 -version

成功结果如下:

root@iZuf6guu7jd98r2y29a78lZ:~# apache2 -version
Server version: Apache/2.4.18 (Ubuntu)
Server built:   2017-09-18T15:09:02

启动:

sudo /etc/init.d/apache2 start

成功结果:

root@iZuf6guu7jd98r2y29a78lZ:~# sudo /etc/init.d/apache2 start
[ ok ] Starting apache2 (via systemctl): apache2.service.

四、安装Gerrit

  • 下载gerrit.war

这里简单拓展一下scp命令,将本机的文件拷贝至另一台主机,需要输入对方主机密码,命令格式:scp 本地文件路径 root@对方ip:对方文件夹(注意空格):

MacBook-Pro:~ GaoSir$ scp /Users/apple/Downloads/Gerrit_full/gerrit2/bin/gerrit.war root@172.16.8.2:/usr/local
root@172.16.8.2's password: 
gerrit.war                                    100%   39MB   9.0MB/s   00:04    
MacBook-Pro:~ GaoSir$
  • 定位到gerrit.war的下载目录下:

cd /usr/local/gerrit

  • 开始安装:

java -jar gerrit.war init -d review_site

配置gerrit:

[root@localhost ~]# cd /usr/local/gerrit/
[root@localhost gerrit]# java -jar gerrit.war init -d review_site

*** Gerrit Code Review 2.10
*** 

Create '/usr/local/gerrit/review_site' [Y/n]? y

*** Git Repositories
*** 

Location of Git repositories   [git]:  //git放的目录,没有的话自己会创建的

*** SQL Database
*** 

Database server type           [h2]:   //我用的默认H2数据库,也可以输入mysql然后回车,这样你用的就是mysql数据库了

*** Index
*** 

Type                           [LUCENE/?]: 

*** User Authentication
*** 

Authentication method          [OPENID/?]: HTTP  //输入HTTP,不然就不是反向代理了
Get username from custom HTTP header [y/N]? n
SSO logout URL                 : 

*** Review Labels
*** 

Install Verified label         [y/N]? 

*** Email Delivery
*** 

SMTP server hostname           [localhost]: smtp.163.com   //输入自动发送邮件的邮箱smtp
SMTP server port               [(default)]: 25  // 465/994是SSL协议端口号,25是非SSL协议端口号(这里指的163邮箱)
SMTP encryption                [NONE/?]:   //如果上一步输入的是465/994,此处输入SSL,否则直接回车
SMTP username                  [root]: gaochujia@163.com     //此处是你自动发送邮件的邮箱
gaochujia@163.com's password   :    
              confirm password :     //此处是你邮箱的授权码,非邮箱密码(切记,不然邮件发不出去)

*** Container Process
*** 

Run as                         [root]:   //这是用户,可以用默认root
Java runtime                   [/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64/jre]: 
Copy gerrit.war to /usr/local/gerrit/review_site/bin/gerrit.war [Y/n]? Y
Copying gerrit.war to /usr/local/gerrit/review_site/bin/gerrit.war

*** SSH Daemon
*** 

Listen on address              [*]: 
Listen on port                 [29418]: 

Gerrit Code Review is not shipped with Bouncy Castle Crypto SSL v149
  If available, Gerrit can take advantage of features
  in the library, but will also function without it.
Download and install it now [Y/n]? y
Downloading http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar ... !! FAIL !!


error: http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar: 302 Found
Please download:

  http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar

and save as:

  /usr/local/gerrit/review_site/lib/bcpkix-jdk15on-149.jar

Press enter to continue 
Continue without this library  [Y/n]? 
Generating SSH host key ... rsa(simple)... done

*** HTTP Daemon
*** 

Behind reverse proxy           [y/N]? y
Proxy uses SSL (https://)      [y/N]? n
Subdirectory on proxy server   [/]: 
Listen on address              [*]: 
Listen on port                 [8081]: 8089   //这个端口号一定不要和apache的端口号一样
Canonical URL                  [http://localhost/]:  //这里填写你访问Gerrit时的url,如:http://127.0.0.1:8080

*** Plugins
*** 

Install plugin commit-message-length-validator version v2.10 [y/N]? y
Install plugin download-commands version v2.10 [y/N]? y
Install plugin replication version v2.10 [y/N]? y
Install plugin reviewnotes version v2.10 [y/N]? y
Install plugin singleusergroup version v2.10 [y/N]? y

Initialized /usr/local/gerrit/review_site
Executing /usr/local/gerrit/review_site/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on localhost:80 ... OK
Opening http://localhost/#/admin/projects/ ...OK
[root@localhost gerrit]#
  • 修改配置文件
[gerrit]
    basePath = git
    canonicalWebUrl = http://106.14.59.64:8090/
[database]
    type = h2
    database = db/ReviewDB
[index]
    type = LUCENE
[auth]
    type = HTTP
[sendemail]
    smtpServer = smtp.qq.com
    smtpServerPort = 465
    smtpEncryption = SSL
    smtpUser = 799713145@qq.com
    from = CodeReview<799713145@qq.com>  //此处加上邮件的来源,不然无法发送邮件
[container]
    user = root
    javaHome = /usr/lib/jdk/jre
[sshd]
    listenAddress = *:29418
[httpd]
    listenUrl = proxy-http://*:8081/
[cache]
    directory = cache

五、创建password文件

-c参数为创建,仅限第一次添加用户时使用,passwords是在apache2目录下创建的文件,第一个admin是用户名,第二个admin是密码:

htpasswd -cb /etc/apache2/passwords admin admin

第二次添加其他用户时,不要加-c:
htpasswd -b /etc/apache2/passwords test test

六、修改Apache的配置文件

这是我的apache2目录:

root@iZuf6guu7jd98r2y29a78lZ:~# cd /etc/apache2
root@iZuf6guu7jd98r2y29a78lZ:/etc/apache2# ls //查看当前目录下的内容
apache2.conf    envvars         mods-enabled  review_site
conf-available  magic           passwords     sites-available
conf-enabled    mods-available  ports.conf    sites-enabled

定位到目录/etc/apache2/sites-enabled,编辑此文件夹目录下的文件000-default.conf:

root@iZuf6guu7jd98r2y29a78lZ:/etc/apache2# cd ./sites-enabled/
root@iZuf6guu7jd98r2y29a78lZ:/etc/apache2/sites-enabled# ls
000-default.conf
root@iZuf6guu7jd98r2y29a78lZ:/etc/apache2/sites-enabled# vim 000-default.conf 

标签内加入以下内容:

ServerName 172.16.8.2  //此处是你的ip地址
    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On

    
        Order deny,allow
        Allow from all
    

    
        AuthType Basic
        AuthName "Gerrit Code Review"
        Require valid-user
        AuthUserFile /etc/httpd/passwords   //刚刚创建的密码文件
    


    AllowEncodedSlashes On
    ProxyPass / http://172.16.8.2:8089/   //此处的8089是gerrit端口号

此处是你的apache端口号,默认80。

  • 如果想新增端口:
Listen 8090  //额外加的端口号
<VirtualHost *:8090>  //改成8090
    ServerName 172.16.8.2  
    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On

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

    <Location /login/>
        AuthType Basic
        AuthName "Gerrit Code Review"
        Require valid-user
        AuthUserFile /etc/httpd/passwords   
    </Location>


    AllowEncodedSlashes On
    ProxyPass / http://172.16.8.2:8089/   
</VirtualHost>

七、问题

Gerrit使用

1、如果遇到Permission denied (publickey) 怎么解决?

答:执行ssh-add就ok了。

2、如何将已有git项目迁移到Gerrit?

答:GitLab项目迁移到Gerrit

3、当你克隆下来的仓库路径是http开头的,如:http://172.14.59.64:8090/Test,push代码的时候会让你输入用户名及密码,为什么一直提示fatal: Authentication failed?

答:切记,用户名就是你服务器上的Username,而密码不是你的登录密码,进入到用户设置,点击HTTP Password,如果没有密码,点击创建密码,生成的密码就是所需密码。

4、Jenkins的Gerrit Trigger之前添加过server,后来把server的hostname改了,但是没有生效?

答:重启Jenkins。

5、通过Gerrit检查代码,网页提示“The page you requested was not found, or you do not have permission to view this page”。

答:打开Apache的配置文件,在标签内,
ProxyPass / http://172.16.8.2:8089/ nocanon
加上红色部分内容,然后重启Apache和Gerrit。

结尾:

当然还没有结束,后续问题模块还会再补充,也希望你们能留下宝贵的意见,如果遇到别的问题,也欢迎留言。

你可能感兴趣的:(Gerrit)