CentOS 7.x安装部署Gerrit完整教程

本文介绍基于CentOS 7.x搭建Gerrit服务的详细步骤。搭建前需准备JDK、Nginx、Git等环境,随后依次介绍了Git、JDK、Nginx的安装过程,以及Gerrit的下载、初始化、设置nginx代理等操作,还给出了访问Gerrit的地址及相关注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gerrit服务搭建教程

本文介绍基于CentOS 7.x 搭建Gerrit服务,Gerrit服务搭建需要如下环境准备。

JDK:Gerrit是用Java语言开发,运行时需要Java环境,本文Gerrit版本是3.12,需要jdk 1.8+版本

Nginx:作为统一网关使用

Git: 代码托管平台

Git安装

安装Git依赖

[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

移除系统上已有的Git

[root@localhost ~]# yum remove git

下载Git安装包并上传到Linux,Git下载地址:https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.22.0.tar.gz

解压到/usr目录

[root@localhost ~]# tar -zxvf git-2.22.0.tar.gz -C /usr
[root@localhost ~]# cd /usr/git-2.22.0

编译Git源码

[root@localhost git-2.22.0]# make prefix=/usr/local/git all

安装 Git

[root@localhost git-2.22.0]# make prefix=/usr/local/git install

配置环境变量

[root@localhost git-2.22.0]# vim /etc/profile

在文件末尾最后一行加上 export PATH=$PATH:/usr/local/git/bin

查看Git是否安装完成

[root@localhost git-2.22.0]# source /etc/profile
[root@localhost git-2.22.0]# git --version

JDK安装

下载JDK压缩包并上传到Linux,省略

解压并重命名

[root@localhost ~]# tar -zxvf jdk-8u192-linux-x64.tar.gz -C /usr/local/java
[root@localhost ~]# mv  /usr/local/java/jdk1.8.0_192 /usr/local/java/jdk1.8

配置环境变量

[root@localhost jdk1.8]# vim /etc/profile

在文件末尾加上

export JAVA_HOME=/usr/local/java/jdk1.8
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

使配置生效

[root@localhost jdk1.8]# source /etc/profile
[root@localhost jdk1.8]# java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)
[root@localhost jdk1.8]# 

Nginx安装

安装nginx前置依赖

[root@localhost ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

下载nginx压缩包,nginx下载地址:http://nginx.org/en/download.html

[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

解压到/usr/local目录下

[root@localhost ~]# tar -zxvf nginx-1.20.1.tar.gz -C /usr/local

编译、安装

[root@localhost ~]# cd /usr/local/nginx-1.20.1
[root@localhost nginx-1.20.1]# ./configure
[root@localhost nginx-1.20.1]# make && make install

启动nginx

[root@localhost nginx-1.20.1]# cd /usr/local/nginx/sbin/ 
[root@localhost sbin]# ./nginx

检查nginx是否启动成功

[root@localhost sbin]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost sbin]# 

注意:如果nginx正常启动,但是不能访问的话,检查linux是否开放了端口,检查防火墙是否关闭

# 开放端口
[root@localhost conf]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
[root@localhost conf]# firewall-cmd --reload
# 关闭防火墙
[root@localhost conf]# systemctl disable firewalld.service
# 防火墙状态
[root@localhost sbin]# systemctl status firewalld.service

Gerrit安装

下载

下载地址:https://www.gerritcodereview.com/index.html

本文以gerrit-3.1.2.war安装为示例

[root@localhost ~]# cd /usr
[root@localhost usr]# wget https://gerrit-releases.storage.googleapis.com/gerrit-3.1.2.war

初始化

遇到空白项回车就行

[root@localhost usr]# java -jar gerrit-3.1.2.war init -d review_site
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2022-07-07 00:41:18,289] [main] INFO  com.google.gerrit.server.config.GerritServerConfigProvider : No /usr/review_site/etc/gerrit.config; assuming defaults

*** Gerrit Code Review 3.1.2
*** 

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

*** Git Repositories
*** 

Location of Git repositories   [git]: /gerrit/gerrit_repo

*** Index
*** 

Type                           [lucene]: 

*** User Authentication
*** 

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

*** Review Labels
*** 

Install Verified label         [y/N]? n

*** Email Delivery
*** 

SMTP server hostname           [localhost]: smtp.163.com
SMTP server port               [(default)]: 25
SMTP encryption                [none/?]: 
SMTP username                  [root]: liao535299@163.com
liao535299@163.com's password  : 
              confirm password : 

*** Container Process
*** 

Run as                         [root]: 
Java runtime                   [/usr/local/java/jdk1.8/jre]: /usr/local/java/jdk1.8/jre
Copy gerrit-3.1.2.war to review_site/bin/gerrit.war [Y/n]? y
Copying gerrit-3.1.2.war to review_site/bin/gerrit.war

*** SSH Daemon
*** 

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

*** HTTP Daemon
*** 

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

*** Cache
*** 


*** Plugins
*** 

Installing plugins.
Install plugin codemirror-editor version v3.1.2 [y/N]? y
Installed codemirror-editor v3.1.2
Install plugin commit-message-length-validator version v3.1.2 [y/N]? y
Installed commit-message-length-validator v3.1.2
Install plugin delete-project version v3.1.2 [y/N]? y
Installed delete-project v3.1.2
Install plugin download-commands version v3.1.2 [y/N]? y
Installed download-commands v3.1.2
Install plugin gitiles version v3.1.2 [y/N]? y
Installed gitiles v3.1.2
Install plugin hooks version v3.1.2 [y/N]? y
Installed hooks v3.1.2
Install plugin plugin-manager version v3.1.2 [y/N]? y
Installed plugin-manager v3.1.2
Install plugin replication version v3.1.2 [y/N]? y
Installed replication v3.1.2
Install plugin reviewnotes version v3.1.2 [y/N]? y
Installed reviewnotes v3.1.2
Install plugin singleusergroup version v3.1.2 [y/N]? y
Installed singleusergroup v3.1.2
Install plugin webhooks version v3.1.2 [y/N]? y
Installed webhooks v3.1.2
Initializing plugins.

Initialized /usr/review_site
Init complete, reindexing projects with: reindex --site-path review_site --threReindexing projects:    100% (2/2)
Reindexed 2 documents in projects index in 0.2s (9.8/s)
Executing /usr/review_site/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on localhost:8081 ... OK
Opening http://localhost:8081/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:
  http://localhost:8081/#/admin/projects/
[root@localhost usr]# 

设置nginx代理

[root@localhost usr]# mkdir /usr/local/nginx/gerrit
[root@localhost usr]# vim /usr/local/nginx/conf/nginx.conf

在nginx.conf文件加入如下配置

server {  
    listen *:8080; 
    # 192.168.1.161 为安装gerrit的服务器ip,也可以写localhost
    server_name 192.168.1.161
    # 开启http basic认证,有些nginx版本默认不开启
    auth_basic on;
    auth_basic "Welcomme to Gerrit Code Review Site!"; 
    auth_basic_user_file /usr/local/nginx/gerrit/htpasswd.conf; 
        
    location / {  
        proxy_pass  http://localhost:8081; 
    }  
}

重新加载nginx配置

[root@localhost usr]# /usr/local/nginx/sbin/nginx -s reload

安装apache

[root@localhost usr]# yum install httpd
# 登录gerrit的账号
[root@localhost usr]# htpasswd -c /usr/local/nginx/gerrit/htpasswd.conf admin
New password: 
Re-type new password: 
Adding password for user admin
[root@localhost usr]# 

访问Gerrit

http://192.168.1.161:8080/

注意:如果遇到访问不了的情况,请检查8080端口是否开放,防火墙是否关闭

# 开放端口
[root@localhost conf]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
# 关闭防火墙
[root@localhost conf]# systemctl disable firewalld.service
# 防火墙状态
[root@localhost sbin]# systemctl status firewalld.service
[root@localhost conf]# firewall-cmd --reload

为了后续让IDEA等开发工具集成Gerrit,现在简单修改下gerrit配置

[root@devops-yanxiao-1-161 etc]# vim gerrit.config 

[gerrit]
        basePath = /gerrit/gerrit_repo
        # 将canonicalWebUrl 由127.0.0.1改为gerrit的安装服务ip
        canonicalWebUrl = http://192.168.1.161:8081
        serverId = f3d5003a-aee7-4762-9f66-405d555ca2ec
[container]
        javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
        javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
        user = root
        javaHome = /data/server/jdk/jre
[index]
        type = lucene
[auth]
        type = HTTP
[receive]
        enableSignedPush = false
[sendemail]
        smtpServer = smtp.163.com
        smtpServerPort = 25
        smtpUser = liao535299@163.com
[sshd]
        listenAddress = *:29418
[httpd]
       # 将listenUrl 改为如下
        listenUrl = proxy-http://*:8081/
[cache]
        directory = cache

然后重启gerrit

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值