一、Nexus搭建环境

环境:CentOS 7.4、JDK8、Sonatype Nenux、Maven

二、搭建步骤

1、配置JDK环境变量

解压JDK安装包,修改 vi /etc/profile配置文件,增加环境变量配置。修改完,保存后执行source /etc/profile命令,让新修改的环境变量生效。

JAVA_HOME=/usr/local/java/jdk1.8.0_181;
PATH=$JAVA_HOME/bin:/bin:$PATH;
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar;

二、安装私服Nexus

1、Nexus资源包下载

Nexus官方下载地址:https://www.sonatype.com/download-oss-sonatype
Nexus官方文档地址:https://www.sonatype.com/oss-thank-you-tar.gz
参考链接地址:https://cloud.tencent.com/developer/article/1336556

上面第3个参考链接,对 为项目分配独立的仓库 有误,权限类型选择错误。
参考这部分权限类型:本地私服仓库nexus3.3.1使用手册 - 云+社区 - 腾讯云

2、Nexus 资源包解压

2.1、在 /usr/local目录中创建子目录 nexus

mdkir /usr/local/nexus

2.2、解压Nexus到指定目录

tar -zxvf  nexus-3.13.0-01-unix.tar.gz -C /usr/local/nexus
  • Nexus 目录结构

nexus3部署在arm架构 nexus3教程_maven

  • Nexus 各个目录结构说明

目录名称

目录说明

bin

包含nexus的启动脚本以及启动相关的配置文件,例如通过bin/nexus.vmoptions文件,你可以配置一些JVM参数和日志存放位置等配置

etc

包含应用级别的配置文件

lib

包含 Apache Karaf 相关的jar包

public

包含应用相关的公共资源

system

包含应用相关的构件和插件

2.3、检查私服端口和工作目录

nexus-3.13.0-01 目录中有子目录 etc,其中保存私服应用的配置信息。查看nexus-default.properties 文件,确定私服访问端口和工作目录。如果没有端口相关冲突,该配置文件不做任何内容修改。配置文件内容如下:

## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/

# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
 nexus-pro-feature

Nexus 运行配置文件 nexus-3.13.0-01/bin/nexus.vmoptions

特别注意,nexus3 解压出来没有 sonatype-work,运行后创建。如果没有创建,手动nexus-3.13.0-01 平级创一个目录 sonatype-work

-Xms1200M
-Xmx1200M
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
-XX:LogFile=../sonatype-work/nexus3/log/jvm.log
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
-Dkaraf.data=../sonatype-work/nexus3
-Djava.io.tmpdir=../sonatype-work/nexus3/tmp
-Dkaraf.startLocalConsole=false

2.4、修改Nexus运行用户

Nexus私服启动后,私服应用需要访问Linux的文件系统,所以需要有足够的权限。Nexus的启动脚本文件中,可以指定应用的访问用户,此信息在nexus-3.13.0-01/bin/nexus.rc配置文件中定义。需修改如下信息:

run_as_user="nexus" # 修改后的内容,代表Nexus私服使用nexus用户权限

一般不建议使用root用户权限启动,可以新建一个nexus用户,启动Nexus。

2.5、修改防火墙,开放Nexus私服端口访问

修改防火墙配置文件,开放Nexus私服的访问端口 8081

vi /etc/sysconfig/iptables  # 注意centOS系统环境
增加如下配置:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8081 -j ACCEPT

iptables status # 查看端口状态
service iptables restart # 重启防火墙

阿里云服务器,需要在阿里云管理后台,设置服务器的端口规则。不在iptables中限制端口的访问

2.6、启动并测试访问

启动Nexus私服
`/usr/local/nexus-3.13.0-01/bin/nexus start`
  • 启动成功日志

nexus3部署在arm架构 nexus3教程_配置文件_02

  • 常用的命令
nexus start  # 启动
nexus stop  # 停止
nexus status  # 查看状态
nexus restart  # 重启

启动成功访问:http://[你的IP]:8081

nexus3部署在arm架构 nexus3教程_配置文件_03

2.7、问题汇总

  • 可以指定JDK的版本
修改$NEXUS_HOME/bin/nexus

# 这一行是注释的,释放掉,后面写JAVA_HOME的路径
INSTALL4J_JAVA_HOME_OVERRIDE=${填写你的JDK版本}
  • nexus - nofile 65536 文件警告

官方参考链接:https://help.sonatype.com/repomanager3/system-requirements#filehandles

 

nexus3部署在arm架构 nexus3教程_maven_04

修改 /etc/security/limits.conf,在文件中增加下面语句,重新登录用户,重启Nexus生效。

nexus - nofile 65536

三、阿里云服务外网域名访问

阿里云服务器配置外网域名可以访问,配置域名解析,通过Nginx配置访问nexus服务。

1、添加阿里云域名解析,如 nexus.[你的域名]

nexus3部署在arm架构 nexus3教程_配置文件_05

2、配置阿里云服务器的Nginx配置文件

server {
       listen       80;
       server_name nexus.[你的域名];

        charset utf-8;
        access_log  logs/nexus_access.log main;
        include proxy.conf;

        location / {
            proxy_pass http://127.0.0.1:8081;
        }

        location ~ ^/(WEB-INF)/ {
            deny all;
        }

        location ~ \.(php|asp|aspx) {
            deny all;
        }
    }

其中 proxy.conf 的内容为

proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        RealIP          $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size    100m;
proxy_connect_timeout   3s;
proxy_send_timeout      1m;
proxy_read_timeout      1m;
proxy_temp_file_write_size 1024m;
proxy_buffer_size         32k;
proxy_buffers             4 32k;
proxy_busy_buffers_size 64k;
proxy_ignore_client_abort on;
proxy_next_upstream error timeout invalid_header http_503;

2、配置好,Nginx重新reload下,使配置生效。

3、通过域名测试访问是否生效。

特别注意:nexus3 的访问域名为:http://nexus.[你的域名]

nexus3部署在arm架构 nexus3教程_配置文件_06