Nexus Repository Manager OSS

Nexus Repository Manager OSS 用于搭建私有源、镜像代理,支持 maven, docker, bower, npm 等

安装 Java Runtime

Nexus 依赖 Java 运行时环境,官方只支持 Oracle Java

但 openjdk 也应能正常工作:

  1. sudo yum install java-1.8.0-openjdk

安装

没有 rpm 包可用,只能手动下载安装

  1. curl -LO http://download.sonatype.com/nexus/3/latest-unix.tar.gz
  2. tar vxf latest-unix.tar.gz
  3. sudo mv nexus-* /opt
  4. sudo ln -s /opt/nexus-*/ /opt/nexus
  5. # 创建单独的用户作为运行时用户,避免安全隐患
  6. sudo useradd -M -c "Nexus repository manager" -s /sbin/nologin nexus
  7. sudo chown -R nexus: /opt/nexus/
  8. # 添加 systemd 管理服务
  9. sudo tee /etc/systemd/system/nexus.service <<-'EOF'
  10. [Unit]
  11. Description=nexus service
  12. After=network.target
  13. [Service]
  14. Type=forking
  15. ExecStart=/opt/nexus/bin/nexus start
  16. ExecStop=/opt/nexus/bin/nexus stop
  17. ExecReload=/opt/nexus/bin/nexus force-reload
  18. User=nexus
  19. Restart=on-abort
  20. [Install]
  21. WantedBy=multi-user.target
  22. EOF
  23. sudo systemctl daemon-reload

配置

配置文件保存在安装目录的 etc, bin 子目录下

监听地址

Nexus 默认监听所有 interface 的 8081 端口

生产环境下使用 Nginx 作为前端,反向代理到 nexus,因此 nexus 只需监听本地即可

/opt/nexus/etc/org.sonatype.nexus.cfg:

  1. application-host=127.0.0.1
  2. # 若部署在子路径,此处设置子路径
  3. # nexus-context-path=/nexus/

数据目录

数据目录默认为安装目录下的 data 目录,改为其它目录,以便升级 nexus

/opt/nexus/bin/nexus.vmoptions:

  1. -Dkaraf.data=/data/nexus
  2. -Djava.io.tmpdir=/data/nexus/tmp

创建数据目录:

  1. sudo mkdir /data/nexus
  2. sudo chown nexus: /data/nexus

SEO

禁止搜索引擎索引,以减少被发现、滥用的风险

/opt/nexus/public/robots.txt:

  1. User-agent: *
  2. Disallow: /

Nginx

  1. server {
  2. listen 80;
  3. server_name nexus.example.com;
  4. return 301 https://$host$request_uri;
  5. access_log /var/log/nginx/nexus.access.log main;
  6. error_log /var/log/nginx/nexus.error.log warn;
  7. }
  8. server {
  9. listen 443 http2 ssl;
  10. server_name nexus.example.com;
  11. ssl_certificate /etc/letsencrypt/live/nexus.example.com/fullchain.pem;
  12. ssl_certificate_key /etc/letsencrypt/live/nexus.example.com/privkey.pem;
  13. proxy_send_timeout 120;
  14. proxy_read_timeout 300;
  15. proxy_buffering off;
  16. keepalive_timeout 55;
  17. tcp_nodelay on;
  18. # allow large uploads of files
  19. client_max_body_size 1G;
  20. # optimize downloading files larger than 1G
  21. proxy_max_temp_file_size 2G;
  22. location / {
  23. proxy_pass http://127.0.0.1:8081;
  24. proxy_set_header Host $http_host;
  25. proxy_set_header X-Real-IP $remote_addr;
  26. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  27. # 若未使用 https,注释该配置
  28. proxy_set_header X-Forwarded-Proto "https";
  29. }
  30. access_log /var/log/nginx/nexus.access.log main;
  31. error_log /var/log/nginx/nexus.error.log warn;
  32. }

运行

使用 systemd 管理 nexus 运行状态:

  1. # 查看状态/启动/停止/重启
  2. sudo systemctl start/stop/restart/status nexus
  3. # 启用/禁用自动启动
  4. sudo systemctl enable/disable nexus

网页端默认的管理账号为:

  • username: admin
  • password: admin123

首次运行后应立即修改密码

升级

步骤:

  1. 下载新版本并解压缩到 /opt/ 目录下,修改其中的配置文件
  2. 停止运行 nexus
  3. 修改 /opt/nexus 符号链接,指向最新版本
  4. 启动 nexus

由于配置文件保存在安装目录中,每次升级都须重新修改配置文件,比较麻烦,可使用以下脚本简化操作:

  1. #!/usr/bin/bash
  2. # 升级 nexus 版本
  3. # 参数:新版本目录
  4. set -e
  5. # nexus 运行时的用户
  6. user=nexus
  7. # 数据目录
  8. data_dir=/data/nexus
  9. # 新版本文件所在目录
  10. new_version_path=$1
  11. if [[ -z "$new_version_path" ]]; then
  12. echo "请指定新版本文件所在目录"
  13. exit 1
  14. elsif [[ ! -d "$new_version_path" ]];
  15. echo "$new_version_path 不是一个正确的目录"
  16. exit 1
  17. fi
  18. base_name=`basename $new_version_path`
  19. if [[ -d "/opt/$base_name" ]]; then
  20. echo "/opt/$base_name 已存在"
  21. exit 1
  22. fi
  23. # 安装文件目录移动到 /opt
  24. sudo mv $new_version_path /opt/
  25. sudo chown -R $user: /opt/$base_name
  26. # 修改配置文件。根据实际的配置情况按需修改
  27. cd /opt/$base_name
  28. sudo sed -i 's#^application-host=.*$#application-host=127.0.0.1#' etc/org.sonatype.nexus.cfg
  29. sudo sed -i 's#^-Dkaraf.data=.*$#-Dkaraf.data=/data/nexus#' bin/nexus.vmoptions
  30. sudo sed -i 's#^-Djava.io.tmpdir=.*$#-Djava.io.tmpdir=/data/nexus/tmp#' bin/nexus.vmoptions
  31. sudo tee public/robots.txt <<-'EOF'
  32. User-agent: *
  33. Disallow: /
  34. EOF
  35. # 重启 nexus
  36. sudo systemctl stop nexus
  37. sudo -sf /opt/$base_name /opt/nexus
  38. sudo systemctl start nexus

参考资料