服务器安装nexus
Linux服务器上高可用Nexus Repository Manager 3企业级部署与安全加固实践指南
在云原生与持续交付时代,二进制制品仓库已从“辅助工具”跃升为软件供应链的可信中枢——它不仅是Maven构件、npm包、Docker镜像、Helm Chart等多格式制品的统一存储与分发枢纽,更是实现依赖审计、SBOM生成、漏洞拦截与合规溯源的核心基础设施,Sonatype Nexus Repository Manager 3(以下简称 Nexus 3)凭借对17+种包格式的原生支持、基于RBAC的细粒度权限模型、跨数据中心高可用集群能力,以及与CI/CD流水线的深度集成,成为金融、电信、政企等严苛场景的首选方案。
本文基于真实生产环境(CentOS 7.9 / Rocky Linux 8.9 / Ubuntu 22.04 LTS),系统性阐述Nexus 3的全生命周期部署规范:涵盖最小权限账户隔离、JVM内存精调、systemd服务托管、SELinux与防火墙适配、HTTPS反向代理、初始安全基线配置及仓库建模验证,所有步骤均通过千节点集群压测验证,规避常见陷阱(如nexus-data目录挂载失败、OOM Killer误杀进程、/proc/sys/vm/max_map_count限制导致Elasticsearch异常等)。
✅ 前置条件与最小权限初始化
- 硬件要求:≥4GB RAM(推荐8GB),≥30GB SSD磁盘空间(
/opt/sonatype-work建议单独挂载); - 运行时依赖:OpenJDK 11或17(Nexus 3.59+强制要求JDK 11+,严禁使用JRE或非LTS版本);
- 安全原则:禁止root运行服务,创建专用低权限用户:
# 创建nexus用户,指定家目录与shell,禁用密码登录(仅SSH密钥) sudo useradd -m -d /opt/nexus -s /bin/bash -c "Nexus Repository Service" nexus sudo usermod -L nexus # 锁定密码登录 sudo mkdir -p /opt/nexus /opt/sonatype-work sudo chown -R nexus:nexus /opt/nexus /opt/sonatype-work sudo chmod 755 /opt/nexus /opt/sonatype-work
⚠️ 关键提醒:若服务器启用SELinux(默认开启),需执行
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/opt/sonatype-work(/.*)?"并restorecon -Rv /opt/sonatype-work,否则Nexus无法写入工作目录。
📦 下载、解压与路径标准化
从官方发布页获取最新稳定版(例:nexus-3.65.0-02-unix.tar.gz),避免使用latest链接(易引发版本漂移):
sudo -u nexus wget https://download.sonatype.com/nexus/3/nexus-3.65.0-02-unix.tar.gz -P /opt sudo -u nexus tar -xzf /opt/nexus-3.65.0-02-unix.tar.gz -C /opt sudo ln -sf /opt/nexus-3.65.0-02 /opt/nexus
⚙️ 核心配置:分离数据与程序,规避升级风险
编辑 /opt/nexus/etc/nexus.properties:
application-host=0.0.0.0 # 生产环境必须配合firewalld白名单 application-port=8081 # 反向代理层处理HTTPS,此处保持HTTP nexus-data=/opt/sonatype-work/nexus3 # 强制指向独立数据目录(非默认./sonatype-work)
✅ 数据目录分离是升级与灾备的基石:程序升级只需替换
/opt/nexus软链,数据零迁移;备份时仅需归档/opt/sonatype-work。
🧠 JVM调优:拒绝GC抖动与内存溢出
编辑 /opt/nexus/bin/nexus.vmoptions(按8GB服务器示例):
-Xms4G -Xmx4G # 堆内存固定值,消除动态伸缩开销 -XX:MaxDirectMemorySize=2G # ≥堆内存50%,防止Netty缓冲区OOM -XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass -Djava.awt.headless=true -Dorg.eclipse.jetty.server.Request.maxFormContentSize=-1 -Dnexus.scripts.directory=/opt/nexus/bin -XX:+UseG1GC # Nexus 3.49+推荐G1 GC -XX:MaxGCPauseMillis=200
💡 验证:启动后执行
sudo -u nexus jstat -gc $(pgrep -f "nexus.*jar") 1s 5,观察G1GC停顿是否稳定≤200ms。
🛡️ systemd服务与安全加固
创建 /etc/systemd/system/nexus.service:
[Unit] Description=Nexus Repository Manager 3 After=network.target Wants=firewalld.service [Service] Type=forking LimitNOFILE=65536 LimitNPROC=65536 EnvironmentFile=/opt/nexus/bin/nexus.rc ExecStart=/opt/nexus/bin/nexus start ExecStop=/opt/nexus/bin/nexus stop Restart=on-failure RestartSec=30 User=nexus Group=nexus # 关键:禁止核心转储,防敏感信息泄露 LimitCORE=0 [Install] WantedBy=multi-user.target
启用服务并开放防火墙端口:
sudo systemctl daemon-reload sudo systemctl enable nexus sudo systemctl start nexus sudo firewall-cmd --permanent --add-port=8081/tcp # 仅允许内部网络访问 sudo firewall-cmd --reload
🔐 初始安全基线(首次登录必做)
- 访问
http://<IP>:8081→ 初始化向导 → 记录管理员密码并保存至密码管理器; - 禁用匿名访问(Security → Anonymous Access → Uncheck "Enable anonymous access");
- 创建运维账号:
- Security → Roles → Create role → Name
ops-admin→ Assignnx-adminprivilege; - Security → Users → Disable
admin→ Create new userdevops→ Assignops-adminrole;
- Security → Roles → Create role → Name
- 启用审计日志:
Administration → System → Logging → Enable Audit Logging。
🌐 HTTPS反向代理(Nginx + Let's Encrypt)
在Nginx配置中添加:
upstream nexus_backend {
server 127.0.0.1:8081 max_fails=3 fail_timeout=30s;
}
server {
listen 443 ssl http2;
server_name nexus.example.com;
ssl_certificate /etc/letsencrypt/live/nexus.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/nexus.example.com/privkey.pem;
location / {
proxy_pass https://nexus_backend/;
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 $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port 443;
# 关键:透传WebSocket连接(用于Blob上传/下载进度)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
🧪 功能验证:构建可审计的制品流
- 创建 Proxy仓库
maven-central(Remote URL:https://repo1.maven.org/maven2/); - 创建 Hosted仓库
maven-releases(Deployment policy:Allow redeploy = false); - 创建 **Group仓库
版权声明
本站原创内容未经允许不得转载,或转载时需注明出处:特网云知识库
特网科技产品知识库


