Ubuntu+Apache2 搭建Gerrit 环境

发布于:2024-06-26 ⋅ 阅读:(20) ⋅ 点赞:(0)

一、前言

时隔多年,好久没有更新CSDN 博客了,主要原因有如下两点:

1、平时工作繁忙,无暇更新。

2、工作内容涉及信息安全,一些工作经验积累不便更新到互联网上。

  最近一直在折腾搭建Gerrit 环境,最开始是在Windows 环境下尝试搭建的,搭建不成功,遇到很多问题,并且网上Windows 环境下资料和文章较少,所以尝试在ubuntu 环境下搭建。

  也折腾了挺长时间的,期间查阅了大量的CSDN 博文,终于调试ok,CSDN 上关于的搭建apache + unbuntu 搭建Gerrit 的博客挺多的,但大多比较零散,针对遇到的问题,讲解不够透彻,很多点网上只是说要这么配置,但并没有说为什么要这么配置,自己也尝试了很多博客的方法终于搭建成功,很想总结分享给大家。

二、搭建环境说明

平台:VMware® Workstation 17 Pro

系统:elementary OS 5.0 Juno(Ubuntu 7.3.0-16ubuntu3))

Apache 版本号: Apache/2.4.29 (Ubuntu)

Gerrit 版本号:gerrit-3.1.3

   强烈推荐搭建按照这篇博客进行配置,个人感觉这篇文章是CSDN 上写的比较好的,里面各种方法也都是我这边踩过的坑,遇到的问题,并且按照这篇文章进行解决的:

【Git】Ubuntu18.04搭建gerrit+gitweb+apache2服务器_ubuntu 搭建git gerrit服务器-CSDN博客

https://blog.csdn.net/u010440719/article/details/127704664

三、安装过程遇到的问题

  具体的安装Apache 和Gerrit 的方法,网上这类文章很多,就不再这里赘述了,各位可以自己搜索相关文章,这里主要讨论安装过程中遇到的问题,避免大家走弯路。

  安装Gerrit 的方式很简单,直接去对应的网址上下载最新的Gerrit 版本安装即可,安装命令也很简单,一条命令即可:

java -jar gerrit-3.9.1.war init -d ~/review_site

  这里的-d 参数是指定gerrit 初始化安装的目录,执行后安装过程中会让你选择很多参数,大部分参数都不重要,因为你最终都可以在安装完成的gerrit.config 文件中重新修改,这里需要注意的是,Gerrit 的认证方式要改成HTTP,不要选择openid(默认是openid),原因接下来会讲到。

sudo  vim /home/gerrit/review_site/etc/gerrit.config

1、Gerrit 的两种认证方式:

(1)OpenId:在安装完成Gerrit,登录Gerrit 之前需要有一个用户认证,默认就是open id,这种方式我们尝试了很久,各种注册Ubuntu 账号,最终还是不行,不得已尝试第2种方式:Apache 反向代理的方式

(2)Apcahe 反向代理:刚开始我也不懂什么叫做Apache 反向代理,只是一股脑的按照网上的各种教程配置,最终发现还是不行,踩了很多的坑。

  所谓Apache 的反向代理就是,Gerrit 会运行在一个IP地址的一个端口上,比如是192.168.170.129:8092,这个时候Gerrit 会监听192.168.170.129 地址上8092 端口上的访问请求,Apache 默认运运行的端口是80 和8080,它会监听这个端口上的访问请求;

  按理说我们配置运行完Gerrit 后直接访问192.168.170.129:8092 端口就好了,但是,因为我们使用的Apache 反向代理做为Gerrit 首次登录认证的一种的方式,那么我们就不能直接通过192.168.170.129:8092 端口去登录Gerrit 了,我们就必须使用Apache 去作为我们登录Gerrit 的一种认证方式,让Apache 去对登录的用户做一个认证。

  这个时候我们就需要用Apache 做反向代理,那么什么是反向代理呢,直白点就是,当你访问这个地址时,它会代理到另外一个地址。这里说的清除点就是,当你访问Apache 的8080 端口时,它会帮你代理代理到Gerrit 的8092 端口,这样的话,你访问192.168.170.129:8080 地址实际上就是访问的192.168.170.129:8092,这个就是Apache 的反向代理。

  那么回到开始的,Gerrit 的Apache 反向代理认证,因为我们做了用Apache 对Gerrit 做了反向代理,并且也通过Apache 对Gerrit 做认证,那么在通过Apache 的8080 端口访问Gerrit 时,Gerrit 就会要求,Apache 的代理请求中带上Gerrit 的认证信息,如果没有带Gerrit 就会拒绝访问,就会出现如下的Gerrit 报错:

就是这个报错,相信大家也深受这个报错的折磨,我有折腾了好久好久,按照网上的方法尝试了很多次都不行,这个其实就是Apache 反向代理配置的问题。

出现这个问题的原因主要有:

(1)Apache 反向代理不生效:

相信大家刚开始的时候都会按照网上的方法

sudo apt-get install apache2
cd /etc/apache2
创建httpd.conf
sudo touch /etc/apache2/httpd.conf
sudo vim httpd.conf
文件写入以下内容:

通过httpd.conf 去配置Apache 反向代理,但是不知道时我的原因,还是本身这种方法就不对,这个文件中配置一直就不生效,所以折腾了很久,改了很多参数,一点用都没有。

后来,参考这篇文章才找到配置生效的地方:/etc/apache2/sites-available/my-default.conf

这里就不在赘述了,可以直接参考下面的这篇文章,已经讲的很清楚了。

Linux中配置Gerrit的apache2反向代理_gerrit apache-CSDN博客

https://blog.csdn.net/m0_48465029/article/details/137328385

然后直接在新建的这个/etc/apache2/sites-available/my-default.conf 文件中配置Apache 反向代理就好了。

  配置完直接重启Apache 服务就好了,推荐大家用下面的命令:

sudo /etc/init.d/apache2 restart

  重启过程中可能会有一些报错导致Apache 运行不起来,这是个好消息,至少说明你配置的地方是对,说明你方向是对的。

  报错原因大家可以直接查看Apache 的日志文件。

sudo vim /var/log/apache2/error.log

sudo vim /var/log/apache2/other_vhosts_access.log

sudo vim /var/log/apache2/access.log

主要看error.log 就可以了,我这边遇到的第一个问题就是没有添加Apache 代理的依赖库文件:

这里可以参考这篇文章:

AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??-CSDN博客

https://blog.csdn.net/zhaojigao/article/details/86641824

 添加依赖的so 库文件,按照这个文章的描述操作就可以了。

主要是添加这三个依赖文件,然后按照命令操作下就可以了:

gerrit1@pc:/etc/apache2$ grep -rn slotmem_shm_module  ./
./mods-available/slotmem_shm.load:1:LoadModule slotmem_shm_module /usr/lib/apache2/modules/mod_slotmem_shm.so
gerrit1@pc:/etc/apache2$ 
gerrit1@pc:/etc/apache2$ sudo ln -s /etc/apache2/mods-available/slotmem_shm.load /etc/apache2/mods-enabled/slotmem_shm.load
gerrit1@pc:/etc/apache2$ 
gerrit1@pc:/etc/apache2$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
   ...done.
gerrit1@pc:/etc/apache2$

这里解决了反向代理基本上就没问题了,然后就是解决各种报错就好了,Apache 各种报错原因及解决办法网上有很多的文章。

因为使用的是Apache 的反向代理,那么这里首先就好看下Apache 监听的是哪些端口:

这里可以用这个命令查看:

wangpeng@linux:~$ sudo netstat -tulnp | grep apache
[sudo] wangpeng 的密码:       
tcp6       0      0 :::8080                 :::*                    LISTEN      916/apache2         
tcp6       0      0 :::80                   :::*                    LISTEN      916/apache2         
wangpeng@linux:~$

可以看到Apache 主要监听的8080 和80 端口,这里我也尝试给Apache 添加一个新的端口,通过这个端口去做Gerrit 反向代理,但是发现不太行,添加不上去,于是就放弃了,还是使用Apache 原本监听的8080 和80 端口,因为Apache 默认就监听的这两端口,也不用自己新加了,直接用就好了,我选用的是8080 端口,80 默认是系统保留的端口,所以尽量选用大一点的端口号,一般不会被系统占用。

那么接下来就是配置上面说的新建的这个/etc/apache2/sites-available/my-default.conf 文件,添加一个新的虚拟主机,这里大家可以直接参考我的配置:

<VirtualHost *:8080>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf

    ServerName localhost:8080

    ProxyRequests off
    ProxyVia Off
    ProxyPreserveHost On

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

    <Location "/login/">
        AuthType Basic
        AuthName "WP Gerrit Code Review"
        Require valid-user
        AuthBasicProvider file
        AuthUserFile /home/gerrit/review_site/passwords
        #这个路径是gerrit账户密码管理,后续的步骤中会创建此文件。路径有写正确
    </Location>

    AllowEncodedSlashes On

    ProxyPass / http://192.168.170.129:8092/
    ProxyPassReverse / http://192.168.170.129:8092/
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

这里也有坑,需要注意这里的端口号<VirtualHost *:8080>,也要改成8080

这里配置好了之后基本上代理就没啥问题了。

上面说到配置代码的目的是为了让Apache 对Gerrit 做认证,就这里的

AuthBasicProvider file
AuthUserFile /home/gerrit/review_site/passwords

这里要新建两个用户,并配置密码,用户和密码的密文存在AuthUserFile /home/gerrit/review_site/passwords 文件中,配置密码的方式大家可以在网上查找,最终配置生效后大家可以看下/home/gerrit/review_site/passwords 文件中的密码信息。

配置完成后重启Apache 服务让配置生效,大家也可以使用命令查看Apache 服务器的运行情况:

sudo systemctl status apache2.service

只有变成绿色的才代表Apache 运行成功了。

(2)gerrit.config 配置的问题

至此Apache 的反向代理就弄好了,接下来就是配置/home/gerrit/review_site/etc/gerrit.config 文件,我的配置如下:

[gerrit]
	basePath = git
	canonicalWebUrl = http://192.168.170.129:8092
	serverId = **********************************
[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 = gerrit
	javaHome = /usr/lib/jvm/java-11-openjdk-amd64
[database]
    type = mysql
    hostname = localhost
    database = reviewdb
    username = gerrit
[index]
	type = lucene
[auth]
	type = HTTP
[receive]
	enableSignedPush = true
[sendemail]
    enable = true
    smtpServer = smtp.qq.com
    smtpServerPort = 465
    smtpEncryption = SSL
    sslVerify = true
    smtpUser = *********@qq.com
    from = *********@qq.com
[sshd]
	listenAddress = *:29418
[httpd]
	listenUrl = proxy-http://192.168.170.129:8092/
[cache]
	directory = cache

这个文件没有啥配置的,按照网上的抄就行了,需要主要的是这里配置的是Gerrit 的网址和端口,需要和Apache 代理的网址和端口一致,并且这里的Gerrit 的端口不要和Apache 的端口8080 配成一样的,我这里配置的8092

这里配置完了之后,重启Gerrit 服务就可以生效了:

sudo /home/gerrit/review_site/bin/gerrit.sh stop

sudo /home/gerrit/review_site/bin/gerrit.sh run

如果一切顺利的话,到这里Gerrit 就可以登录上了

2、Gerrit SMTP 邮箱配置

  接下来就是配置Gerrit 的SMTP 邮箱服务了,因为Gerrit 必须要认证邮箱,才可以push 代码,SMTP 配置也是在/home/gerrit/review_site/etc/gerrit.config 文件中,

  首先给大家说下为什么要配置SMTP 邮箱,这里是因为,gerrit 登录上如果想要提交代码或者新建项目都是必须要做验证登录用户的邮箱的,否则你是不能提交代码的。

  要验证用户邮箱,就需要你把Gerrit 的SMTP 邮箱配置ok,这样你的Gerrit 才可以发送验证邮件给你完成验证。

这里大家可以参考这篇文章:

gerrit服务器邮箱设置(三)_gerrit邮箱配置-CSDN博客

https://blog.csdn.net/sevenjoin/article/details/118189159

需要注意的是:

1、建议大家把密码写在这个文件中:/home/gerrit/review_site/etc/secure.config

2、主要注意这里的enable、smtpServer、smtpServerPort、smtpUser 等等字段的顺序必须按照,否则在Gerrit 上点击Send 验证邮件的时候会各种奇奇怪怪的错。

 这里大家直接Copy 我的配置即可:

[sendemail]
    enable = true
    smtpServer = smtp.qq.com
    smtpServerPort = 465
    smtpEncryption = SSL
    sslVerify = true
    smtpUser = *******@qq.com
    from = *******@qq.com

3、Gerrit 新建项目

  这里也是一条命令即可,这条命令是新建一个Test 的项目

ssh -p 29418 gerrit@192.168.170.129 gerrit create-project --owner gerrit --empty-commit "Test"

未完待续。。