Maven之私服搭建
安装
前置
# Linux CentOS 7
$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
# Java OpenJDK 8
$ yum install java-1.8.0-openjdk-devel
$ java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
安装
# https://help.sonatype.com/repomanager2/download
# 注意:国内网络可能无法下载,考虑科学上网,或者从其它网站下载
$ curl -OL https://download.sonatype.com/nexus/oss/nexus-latest-bundle.tar.gz
# https://help.sonatype.com/repomanager2/installing-and-running
$ tar zxvf nexus-2.14.16-01-bundle.tar.gz -C /opt
# /opt/nexus-2.14.16-01 Nexus服务程序目录
# /opt/sonatype-work 数据(构件)存放目录
# 创建 nexus 用户,因为 Nexus 不推荐使用 root 用户运行服务
# 注:不能是禁止登录无home目录的虚用户(程序使用 su 切换用户)
$ useradd nexus
# 修改目录拥有者属性,因为后面将使用 nexus 用户来运行服务
$ chown nexus:nexus -R /opt/nexus-2.14.16-01
$ chown nexus:nexus -R /opt/sonatype-work
# 启动服务,默认端口 8081、context-path 为 /nexus,可通过修改配置文件改变
# 本案例中不修改,因为后面会用 Nginx 反向代理
# vi /opt/nexus-2.14.16-01/conf/nexus.properties
# 可以通过环境变量 $RUN_AS_USER 指定运行用户,也可以在启动脚本中直接设置
# vi /opt/nexus-2.14.16-01/bin/nexus
# 本案例中不设置,后面会实现 systemd 服务脚本,脚本中会设置
# 使用前台模式运行(输出消息至控制台)Nexus服务
$ export RUN_AS_USER=nexus; /opt/nexus-2.14.16-01/bin/nexus console
# 启动服务
$ export RUN_AS_USER=nexus; /opt/nexus-2.14.16-01/bin/nexus start
# 停止服务
$ export RUN_AS_USER=nexus; /opt/nexus-2.14.16-01/bin/nexus stop
# 尝试在浏览器中访问测试服务是否安装成功
# http://<server_host>:8081/nexus
# 首次安装后需要做一些设置(非必要,按需来即可)
# https://help.sonatype.com/repomanager2/installing-and-running/post-install-checklist
服务
编写 systemd
服务脚本,将 Nexus
配置为系统服务,以通过 systemd
来管理
# /etc/systemd/system/nexus.service
[Unit]
Description=nexus service
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus-2.14.16-01/bin/nexus start
ExecStop=/opt/nexus-2.14.16-01/bin/nexus stop
User=nexus
Restart=on-abort
[Install]
WantedBy=multi-user.target
测试
$ systemctl daemon-reload
$ systemctl start nexus.service
$ systemctl status nexus.service
$ systemctl enable nexus.service
# 访问,注:使用独立域名,所以需要设置 context-path = / 以方便使用Nginx反向代理
# http://nexus.i.zlikun.com/
反代
以 nexus.i.zlikun.com
域名为例来配置,一般如果只是内网用只需要配置 http
即可,外网可访问如果配置 https
那么 http
应直接重定向到 https
上
# nexus.i.zlikun.com.conf
server {
listen 80;
server_name nexus.i.zlikun.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
location / {
proxy_pass http://192.168.1.101:8081/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name nexus.i.zlikun.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
ssl on;
ssl_certificate nexus.i.zlikun.com.pem;
ssl_certificate_key nexus.i.zlikun.com.key;
location / {
proxy_pass http://192.168.1.101:8081/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
}
配置
修改管理员密码和部署密码
# 浏览器访问 Nexus ,并使用原帐号/密码登录,在右上角的下拉菜单中修改密码即可
# 下面的 admin 和 deployment 是系统内置的两个帐号
# admin:admin123
# deployment:deployment123
代理仓库
# 仓库类型
group 仓库组,聚合多个仓库
hosted 宿主仓库(本地)
proxy 代理仓库(远程)
virtual 虚拟仓库,用于兼容 Central M1版本构件(几乎不用)
# 阿里云公共仓库
# https://maven.aliyun.com/mvn/view
# 添加 Aliyun 公共仓库(Proxy)
# https://maven.aliyun.com/repository/public/
# 在 Public Repositories 中添加 aliyun 仓库
# http://nexus.i.zlikun.com/content/groups/public/
备份
除了公共构件外,私服中还包含一些 thirdparty
、snapshots
、releases
仓库构件,这些是公司内部开发的一些构件,公网上不存在,所以是需要备份的,避免 Nexus
服务数据损坏时,造成构件丢失情况
# 构件最终存储在 /opt/sonatype-work/nexus/storage 目录下的子目录(仓库ID)中
# 每个仓库对应一个目录,选择相应的仓库目录进行备份
# -a 归档模式,以递归方式传输文件,并保持文件的所有属性(rsync通过比较文件大小、最后修改时间来进行增量同步)
# -r 对子目录以递归模式处理
# -z 备份文件在传输时进行压缩处理
# -v 详细输出日志
# -S 对稀疏文件进行特殊处理以节省DST的空间
# -P 保留因故未完全传输的文件,以加快后续的再次传输(断点续传)
$ rsync -arzvSP <src_path> <dest_path>
$ rsync -arzvSP /opt/sonatype-work/nexus/storage/releases/ /data/backup/releases
$ rsync -arzvSP /opt/sonatype-work/nexus/storage/snapshots/ /data/backup/snapshots
# 生产环境将备份命令编写成 shell 脚本,通过 crontab 任务来定时执行
# 另外生产环境备份不会备份在本机,rsync 命令支持将文件备份到其它服务器(也可以是将其它服务器文件备份到本机)
# 具体 rsync 的用法请查阅相关资料自行了解
Ansible
最终 Nexus
在生产环境的安装配置应使用脚本或者 Ansible
等来实现,这里后续补充
TODO