CentOS7安装部署NexusRepository
文章目录
- CentOS7安装部署NexusRepository
- 一、前言
- 1.简介
- 2.环境
- 二、正文
- 1.更换镜像源
- 2.安装和配置
- 3.服务管理
- 1)命令行
- 2)systemd服务
- 3)浏览器访问
- 4.创建角色
- 1)角色说明
- 2)创建使用者角色
- 3)创建管理员角色
- 5.创建用户
- 6.创建仓库
- 1)仓库类型说明
- 2)创建Maven仓库
- 3)创建NPM仓库
- (1)代理仓库
- (2)私有仓库
- (3)仓库组
- (4)配置NPM的认证机制
- 7.解决告警问题
- 1)首页加载慢
- (1)问题
- (2)解决方案
- 2)默认加密密钥
- (1)问题
- (2)解决方案
- 8.数据库切换到PostgreSQL
- 1)安装PostgreSQL
- 2)配置Nexus运行环境
- 9.数据迁移到PostgreSQL
CentOS7安装部署NexusRepository
一、前言
1.简介
Sonatype Nexus Repository 是所有内部和第三方二进制文件、组件、包和 AI 模型的单一事实来源。将所有开发工具集成到集中式组件存储库管理器中,以便您可以选择最佳的开源组件、优化构建性能并快速交付代码,同时提高整个 SDLC 的可见性。
2.环境
Linux 发行版:CentOS-7-x86_64-DVD-2207-02.iso
Nexus 版本:nexus-3.83.1-03-linux-x86_64
Nexus 官网:https://www.sonatype.com/products/sonatype-nexus-repository
Nexus 功能矩阵:https://help.sonatype.com/en/nexus-repository-feature-matrix.html
Nexus 系统要求:https://help.sonatype.com/en/sonatype-nexus-repository-system-requirements.html
Nexus 下载:https://help.sonatype.com/en/download.html
Nexus 历史版本下载:https://help.sonatype.com/en/download-archives—repository-manager-3.html
Nexus 安装:https://help.sonatype.com/en/install-nexus-repository.html
Nexus 服务运行:https://help.sonatype.com/en/run-as-a-service.html
Nexus 使用中心:https://help.sonatype.com/en/usage-center.html
Nexus 角色管理:https://help.sonatype.com/en/roles.html
Nexus 配置密钥 JSON:https://help.sonatype.com/en/configuring-the-runtime-environment.html#create-a-json-file-containing-your-custom-encryption-key
Nexus 自定义加密密钥:https://help.sonatype.com/en/re-encryption-in-nexus-repository.htmlNexus 数据库切换至 PostgreSQL:https://help.sonatype.com/en/install-nexus-repository-with-postgresql.html
Nexus 迁移数据:https://help.sonatype.com/en/migrating-to-a-new-database.html
Ubuntu文本编辑工具nano和vim快捷键:https://blog.csdn.net/u011424614/article/details/136611066
CentOS7切换镜像源和更新软件包:https://blog.csdn.net/u011424614/article/details/149867407
CentOS基础操作命令:https://blog.csdn.net/u011424614/article/details/94555916
CentOS对换home分区与root分区的存储空间:https://blog.csdn.net/u011424614/article/details/125853913
Dell R740安装CentOS:https://blog.csdn.net/u011424614/article/details/113306808
CentOS分区扩容:https://blog.csdn.net/u011424614/article/details/113275862
二、正文
1.更换镜像源
- 具体操作参考:《CentOS7切换镜像源和更新软件包》
2.安装和配置
- Nexus 下载:https://help.sonatype.com/en/download.html
- 下载链接右击,可拷贝下载链接
mkdir /opt/nexus && cd /opt/nexus# 下载软件包,如果 wget 无法下载,可使用浏览器或迅雷下载
wget https://download.sonatype.com/nexus/3/nexus-3.83.1-03-linux-x86_64.tar.gz# 解压,并保留其中的目录符号链接(symlink)
tar xvz --keep-directory-symlink -f ./nexus-3.83.1-03-linux-x86_64.tar.gz# 创建用户组
groupadd nexus
# 创建用户,并设置用户组
useradd -g nexus -m nexus
# 设置用户密码
passwd nexus
# 修改用户默认Shell
usermod -s /bin/bash nexus
# 修改用户家目录并附加组
usermod -d /opt/nexus -G nexus nexus
#递归修改目录权限
chown -R nexus:nexus /opt/nexus
- 配置 nexus 运行用户,创建文件:nexus.rc
vim /opt/nexus/nexus-3.83.1-03/bin/nexus.rc
文件内容:
run_as_user="nexus"
- 查看默认配置文件:包含端口配置
cat /opt/nexus/nexus-3.83.1-03/etc/nexus-default.properties
3.服务管理
注意:Nexus 服务启动前,可以先考虑是否将数据库切换到 PostgreSQL 数据库,如需要则先进行第 8 章的操作
1)命令行
- 指令:start, stop, run, restart, force-reload, status
cd /opt/nexus/nexus-3.83.1-03/bin# 以后台守护进程方式启动Nexus服务,日志输出到:
# /opt/nexus/nexus-3.83.1-03/data/log/nexus.log
./nexus start# 优雅停止Nexus服务,等待当前操作完成后再关闭进程
./nexus stop# 前台运行服务,控制台直接输出日志(用于调试)
# 按Ctrl+C或关闭终端会终止服务
./nexus run# 先执行安全停止,再重新启动服务
# 适用于配置修改后的重新加载
./nexus restart# 强制杀死所有相关Java进程并重新启动
# (仅在服务无响应时使用,可能导致数据损坏)
./nexus force-reload# 检查Nexus服务的运行状态
./nexus status
2)systemd服务
cat > /etc/systemd/system/nexus.service <<EOF
[Unit]
Description=nexus service
After=network.target[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/nexus-3.83.1-03/bin/nexus start
ExecStop=/opt/nexus/nexus-3.83.1-03/bin/nexus stopUser=nexus
Restart=on-abort
TimeoutSec=600[Install]
WantedBy=multi-user.target
EOF
- 管理指令
# 重新加载所有 systemd 单元文件
systemctl daemon-reload
# 系统重启自启动
systemctl enable nexus.service
# 启动服务
systemctl start nexus.service
# 停止服务
systemctl stop nexus.service
# 重启服务
systemctl restart nexus.service
# 查看服务状态
systemctl status nexus.service
- 查看日志
tail -f /opt/nexus/sonatype-work/nexus3/log/nexus.log
- 查看 admin 的默认密码
cat /opt/nexus/sonatype-work/nexus3/admin.password
- 防火墙
# 测试环境
systemctl stop firewalld.service
systemctl disable firewalld.service# 生产环境,建议使用
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --reload
# 查看防火墙已开放端口
firewall-cmd --zone=public --list-ports
3)浏览器访问
- 浏览器访问:http://192.168.249.518081/
- 输入:用户名和密码,默认密码就在登录框提示的文件中
cat /opt/nexus/sonatype-work/nexus3/admin.password
- 登录后按照提示,修改新密码
- 是否启用匿名访问:启用匿名访问意味着默认情况下,用户可以在没有凭据的情况下从存储库中搜索、浏览和下载组件。
- 这里禁用匿名访问
4.创建角色
1)角色说明
- Nexus 默认角色:
- nx-admin:超级管理员角色,拥有 Nexus 所有功能的完全访问权限,包括系统配置、用户管理、仓库管理等
- nx-anonymous:匿名访问角色,允许未登录用户访问公开仓库内容,如开源依赖下载
2)创建使用者角色
- Settings - Security - Roles - 点击【Create Role】按钮
- 选项:角色类型
- 输入:角色ID,artifact-consumer
- 输入:角色名称,组件使用者
- 输入:角色说明,权限:仓库浏览、搜索组件、下载组件
- 选择权限:
nx-repository-view-*-*-browse
:仓库浏览nx-repository-view-*-*-read
:内容读取,组件下载nx-search-read
:搜索组件
- 配置完权限后,点击【Save】按钮
3)创建管理员角色
- Settings - Security - Roles - 点击【Create Role】按钮
- 选项:角色类型
- 输入:角色ID,artifact-manager
- 输入:角色名称,组件管理员
- 输入:角色说明,权限:仓库浏览、搜索组件、下载组件、上传组件、修改组件、删除组件
- 选择权限:
nx-repository-view-*-*-browse
:仓库浏览nx-repository-view-*-*-read
:内容读取,组件下载nx-search-read
:搜索组件nx-repository-view-*-*-add
:上传组件nx-repository-view-*-*-edit
:修改组件nx-repository-view-*-*-delete
:删除组件
- 配置完权限后,点击【Save】按钮
5.创建用户
- Settings - Security - Users - 点击【Create local user】按钮
- 输入:ID,账号
- 输入:姓名
- 输入:密码
- 选择:Status,激活状态
- 选择:Roles,角色
- 点击【Create local user】按钮
6.创建仓库
1)仓库类型说明
-
Hosted(托管仓库):存储企业私有组件
-
Proxy(代理仓库):代理远程公共仓库
-
Group(仓库组):聚合多个仓库为统一入口
2)创建Maven仓库
-
场景说明:创建一个私有的 Maven 托管仓库
-
Settings - Repository - Repositories - 点击【Create Repository】按钮
- 选择:maven2(hosted)
- 输入:Name,私有仓库名称
- 选择:Version policy,根据实际情况选择版本策略:
- Release:仅允许发布/缓存正式版本(如
1.0.0
);用于生产环境稳定依赖 - Snapshot:仅允许发布快照版本(如
1.0.0-SNAPSHOT
),自动覆盖同名旧版本;用于开发阶段频繁迭代 - Mixed:同时允许 Release 和 Snapshot 版本共存;用于混合管理的过渡期项目
- 选择:Deployment policy,根据实际情况选择部署策略:
- Allow Redeploy:允许覆盖同名组件(如更新
SNAPSHOT
版本或修复Release
版本);适用于开发环境、快照仓库 - Disable Redeploy:禁止覆盖已有组件(仅允许上传新版本);适用于生产环境、正式版本仓库
- Read-Only:完全禁止上传和删除,仅允许读取;适用于代理仓库(如 Maven Central)
- 点击【Create repository】按钮
3)创建NPM仓库
场景说明:创建一个淘宝NPM代理仓库,然后创建一个私有仓库,最后,将代理仓库和私有仓库添加到仓库组中,使用仓库组的链接进行统一访问
(1)代理仓库
- Settings - Repository - Repositories - 点击【Create Repository】按钮
- 选择:npm(proxy)
-
输入:Name,代理仓库名称
-
输入:Remote storage,远程仓库链接:https://registry.npmmirror.com
-
点击【Create repository】按钮
(2)私有仓库
- Settings - Repository - Repositories - 点击【Create Repository】按钮
- 选择:npm(hosted)
-
输入:Name,私有仓库名称
-
选择:Deployment policy,根据实际情况选择部署策略:
- Allow Redeploy:允许覆盖同名组件;适用于开发环境
- Disable Redeploy:禁止覆盖已有组件(仅允许上传新版本);适用于生产环境、正式版本仓库
- Read-Only:完全禁止上传和删除,仅允许读取;适用于代理仓库
- 点击【Create repository】按钮
(3)仓库组
- Settings - Repository - Repositories - 点击【Create Repository】按钮
- 选择:npm(group)
-
输入:Name,仓库组名称
-
选择:Member repositories:选择需要的仓库
-
点击【Create repository】按钮
(4)配置NPM的认证机制
-
场景说明:npm 客户端默认通过
Bearer Token
认证(而非用户名密码),必须启用 npm Bearer Token Realm -
Settings - Security - Realms - 点击【npm Bearer Token Realm】从左边添加到右边
-
点击【Save】按钮
7.解决告警问题
1)首页加载慢
(1)问题
- 首页加载慢,且查看Nexus日志发现有报错信息
- 报错信息:
INFO [pool-2-thread-10] admin org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {s}->https://sonatype-download.global.ssl.fastly.net:443: 网络不可达
(2)解决方案
-
解决方案:关闭 Outreach: Management
-
Settings - System- Capabilities- 点击【Outreach: Management】
- 点击【Disable】按钮
2)默认加密密钥
Nexus 自定义加密密钥:https://help.sonatype.com/en/re-encryption-in-nexus-repository.html
Nexus 配置密钥 JSON:https://help.sonatype.com/en/configuring-the-runtime-environment.html#create-a-json-file-containing-your-custom-encryption-key
(1)问题
Nexus was not configured with an encryption key and is using the Default key.
(2)解决方案
- 解决方案:配置自定义的密钥
# 生成密钥,例如输出:FHYIYiW1pz55kIpQjTN8SkTIjEuh3QgdraILLtL836w=
openssl rand -base64 32
# 生成密钥,例如输出:24d46eb97bf043dba253d5ab6cee135e
openssl rand -hex 16
# 生成密钥,例如输出:db8a3b0311bce0f218104f6e2a79f8d5
openssl rand -hex 16
- 创建 secrets.json
vim /opt/nexus/secrets.json
文件内容:
{"active": "my-nexus-key","keys": [{"id": "my-nexus-key","key": "FHYIYiW1pz55kIpQjTN8SkTIjEuh3QgdraILLtL836w="}],"fixedEncryption": {"keyId": "my-nexus-key","salt": "24d46eb97bf043dba253d5ab6cee135e","iv": "db8a3b0311bce0f218104f6e2a79f8d5"}
}
- 授予权限
chmod 600 /opt/nexus/secrets.json
chown nexus:nexus /opt/nexus/secrets.json
- 编辑 nexus-default.properties
vim /opt/nexus/nexus-3.83.1-03/etc/nexus-default.properties
内容最后追加配置:
nexus.secrets.file=/opt/nexus/secrets.json
- 重启 Nexus 服务
systemctl restart nexus.service
- 新密钥读取成功
-
强制 Nexus 使用指定的新密钥,重新加密存储的所有敏感数据
-
方式一:命令行请求服务,注意:根据情况,替换IP+端口,以及参数 secretKeyId 的值
# 执行后,需要输入 admin 的密码
curl -u "admin" -X 'PUT' \'http://192.168.249.51:8081/service/rest/v1/secrets/encryption/re-encrypt' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"secretKeyId": "my-nexus-key"
}'
- 输出 202 表示成功
{"status" : 202,"message" : "Task submitted. ID: 35843038-b8bc-44d6-b9dc-1070abaf2041"
}[
- 方式二:Settings - System- API- 浏览器搜索:/v1/secrets/encryption/re-encrypt
8.数据库切换到PostgreSQL
切换数据库:https://help.sonatype.com/en/install-nexus-repository-with-postgresql.html
- 场景说明:Nexus 默认使用 H2 数据库,迁移到PostgreSQL以确保最佳性能
1)安装PostgreSQL
- 具体操作参考:《CentOS7安装部署PostgreSQL》
2)配置Nexus运行环境
- 编辑 postgresql.conf
vim /var/lib/pgsql/15/data/postgresql.conf
文件内容:
# 增加连接数
max_connections = 1000# 新增日志配置
log_line_prefix = '%m [%p:%l] "%v %x" %q<%u@%d/%a> '
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
log_temp_files = 0
log_autovacuum_min_duration = 0
log_error_verbosity = default
log_statement = 'none'
log_min_duration_statement = 1000ms
log_transaction_sample_rate = 0
- 安装 PostgreSQL 官方提供的附加功能模块集合
yum install postgresql15-contrib
- 重启 PostgreSQL 数据库
systemctl restart postgresql-15
- 初始化 Nexus 运行所需数据库
# 切换到postgres系统用户(PostgreSQL默认超级用户)
sudo -i -u postgres# 以postgres用户身份连接PostgreSQL服务器
psql -U postgres # 创建名为nexus的数据库,使用UTF8编码和英文排序规则,基于纯净模板template0
CREATE DATABASE nexus ENCODING 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8' TEMPLATE template0;# 连接到新创建的nexus数据库
\c nexus;# 在nexus数据库中创建专用的nexus模式(schema)
CREATE SCHEMA nexus;# 创建nexus用户并设置密码
CREATE USER nexus WITH PASSWORD 'nexus@123456';# 授予nexus用户对nexus数据库的所有操作权限
# 包括连接、创建表等基础权限
GRANT ALL PRIVILEGES ON DATABASE nexus TO nexus;# 授予nexus用户对nexus模式的所有操作权限
# 允许在该schema中创建、修改和删除表等对象
GRANT ALL PRIVILEGES ON SCHEMA nexus TO nexus;# 检查pg_trgm扩展是否可用(用于模糊搜索和相似度计算)
# pg_trgm提供文本相似度比较函数和GIN/GiST索引支持
SELECT * FROM pg_available_extensions WHERE name = 'pg_trgm';# 在nexus模式中安装pg_trgm扩展
# 安装后可使用similarity()等函数和三元组索引
CREATE EXTENSION pg_trgm SCHEMA nexus;
- 编辑 nexus-store.properties
vim /opt/nexus/sonatype-work/nexus3/etc/fabric/nexus-store.properties
文件内容:
# jdbcUrl=jdbc\:h2\:file\:${karaf.data}/db/${storeName}
username=nexus
password=nexus@123456
jdbcUrl=jdbc:postgresql://192.168.249.51:5432/nexus?gssEncMode=disable&tcpKeepAlive=true&loginTimeout=5&connectionTimeout=5&socketTimeout=30&cancelSignalTimeout=5&targetServerType=primary
- 重启 Nexus 服务
systemctl restart nexus.service
9.数据迁移到PostgreSQL
数据迁移:https://help.sonatype.com/en/migrating-to-a-new-database.html
-
场景说明:将原默认 H2 数据库上的数据迁移到 PostgreSQL 数据库中
-
下载迁移程序:https://help.sonatype.com/en/download.html
-
将迁移程序 jar 包存放到目录:/opt/nexus/sonatype-work/nexus3/db
-
启动迁移程序,根据实际环境修改:内存占用大小 和 db_url
# 停止 Nexus 服务
systemctl stop nexus.service# 进入 jar 包存放目录
cd /opt/nexus/sonatype-work/nexus3/db# 临时会话级设置(当前终端有效),使用 Nexus 自带的 JDK 17
export JAVA_HOME=/opt/nexus/nexus-3.83.1-03/jdk/temurin_17.0.13_11_linux_x86_64/jdk-17.0.13+11
export PATH=$JAVA_HOME/bin:$PATH# 验证版本
java -version# 执行迁移命令
java -Xmx4G -Xms4G -XX:+UseG1GC -XX:MaxDirectMemorySize=4G \-jar nexus-db-migrator-*.jar \--migration_type=h2_to_postgres \--db_url="jdbc:postgresql://192.168.249.51:5432/nexus?user=nexus&password=nexus@123456¤tSchema=nexus"# 启动 Nexus 服务
systemctl start nexus.service