操作记录(主要依据官方文档)"/>
在 Linux 系统上安装 nexus 私库的相关理解和详细操作记录(主要依据官方文档)
文章目录
- 1 系统需求
- 2 关于安装
- 2.1 作为服务来运行
- 2.2 配置内存
- 2.3 修改 HTTP 端口
- 2.4 修改 Context Path
- 2.5 配置数据目录
- 2.6 配置临时目录
- 2.7 使用反向代理
- 2.8 访问用户页面
- 2.9 安装后检查清单
- 3 Nginx 安装步骤
- 4 Nexus 安装步骤
- 5 Apache Maven 配置
- 6 采坑问题
Nexus 官网文档地址:
Nexus 版本包括收费版 Nexus Repository Manager PRO 和 开源免费版 Nexus Repository Manager OSS,本文使用 开源免费 OSS 版。
当前最先版本:Latest Version: 3.27.0-03
本文中目录相关的引用说明
nexus tar 包文件解压之后,会有两个文件夹,分别
- $intall-dir 对应 nexus-3.27.0-03
- $data-dir 对应 sonatype-work/nexus3
1 系统需求
wget .27.0-03-unix.tar.gz
执行时,4G 和公司网络均无法连接到下载地址,最后通过迅雷下载工具曲线实现下载,然后上传到服务器的。
官方建议新建并使用专用账户 nexus 来运行,不要使用 root 用户。
useradd nexus
Nexus 需要消耗比系统用户默认值更多的 file handler,在运行 nexus 之前需要将 open files descriptors 限制提高到 65536 或者更高。
在 /etc/security/limits.conf 添加如下内容,nexus 为用户ID
nexus - nofile 65536
上面的修改,nexus 进程下次重启有效。
最小 CPUs 4 个,建议CPUs 8 个。
查看CPU信息:
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
2
cat /proc/cpuinfo | grep "cpu cores" | uniq
cpu cores : 4
cat /proc/cpuinfo| grep "processor"| wc -l
8
必须设置 jvm heap memory 的最小值(-Xms)和最大值(-Xmx),且值大小必须相等,最大值不超过 4G。
内存设置通用指南:
- 最小主机物理内存 8G
- 堆内存最小值(-Xms)和最大值(-Xmx)必须相等
- 最小堆内存 2703MB
- 最大堆内存 <= 4G
- 最小直接内存(-XX:MaxDirectMemorySize)大小 2703MB
- 最小未分配物理/RAM内存应不小于总物理RAM的1/3,以允许进行虚拟内存交换
- 最大堆内存 + 最大直接内存 <= 总物理内存 * 2/3
最大内存配置示例:
Physical/RAM Memory | Example Maximum Memory Configuration |
---|---|
8GB | -Xms2703M -Xmx2703M -XX:MaxDirectMemorySize=2703M |
12GB | -Xms4G -Xmx4G -XX:MaxDirectMemorySize=4014M |
16GB | -Xms4G -Xmx4G -XX:MaxDirectMemorySize=6717M |
32GB | -Xms4G -Xmx4G -XX:MaxDirectMemorySize=17530M |
64GB | -Xms4G -Xmx4G -XX:MaxDirectMemorySize=39158M |
2 关于安装
不建议安装并运行在用户目录,而是建议使用 /opt 目录。
第一步,解压下载好的 nexus-3.27.0-03-unix.tar.gz 到 /opt/nexus 目录;
tar zxvf nexus-3.27.0-03-unix.tar.gz
第二步,在 nexus bin 目录下运行
./nexus run
上面的命令会在当前 shell 启动 nexus 并显示日志输出。“Started Sonatype Nexus” 日志输出显示之后,就可以访问 nexus 服务了。CTRL + C 可以结束停止 nexus 服务。
除了上面的 run 命令,也可以使用 nexus 脚本的 start、stop、restart、force-reload 和 status 命令,让 nexus 作为后台应用服务来管理。
2.1 作为服务来运行
生产环境一般将 nexus 作为服务或者后台进程来运行,服务器重启时,nexus 也重启。nexus 发行包不包括运行服务的配置。与操作系统无关的三步如下:
- 创建专用的操作系统用户,用于将 nexus 作为服务来运行;
- 确认安装了正确的 Java 运行环境;
- 配置服务确保其在操作系统启动时启动;
在 bin/nexus.rc 中指定专用用户:
run_as_user="nexus"
创建 $intall-dir/bin/nexus 到 /etc/init.d/nexus 软链接
sudo ln -s /opt/nexus-3.15.2-01/bin/nexus /etc/init.d/nexus
使用 chkconfig,使 init.d 中的初始化脚本运行 nexus 服务,运行下面的命令脚本激活服务
cd /etc/init.d
sudo chkconfig --add nexus
sudo chkconfig --levels 345 nexus on
sudo service nexus start
chkconfig 管理 /etc/rc[0-6].d 中的文件软链接,当操作系统重启或者切换运行级别的时候,这些软链接控制服务的启动和停止
2.2 配置内存
- 打开 $install-dir/bin/nexus.vmoptions
- 找到下面配置内存的行
-Xms2703M
-Xmx2703M
-XX:MaxDirectMemorySize=2703M
- 用如下格式修改配置项的值,数字size数值和字母单位(大小写均可)
-Xmssize[g|G|m|M|k|K]
-Xmxsize[g|G|m|M|k|K]
-XX:MaxDirectMemorySize=size[g|G|m|M|k|K]
- 保存文件,并确保 运行 nexus 服务的用户拥有文件权限和所有权
2.3 修改 HTTP 端口
默认端口是 8081,编辑 $data-dir/etc/nexus.properties 文件,修改配置项 application-port
application-port=9081
2.4 修改 Context Path
编辑 $data-dir/etc/nexus.properties 文件,修改默认值 nexus-context-path=/
nexus-context-path=/components/
2.5 配置数据目录
我们可以在文件 $install-dir/bin/nexus.vmoptions 中配置新数据目录位置。相关配置项 -Dkaraf.data,-Djava.io.tmpdir,-Dkaraf.log 和 -XX:LogFile,例如
-Dkaraf.data=/opt/sonatype-work/nexus3
-Djava.io.tmpdir=/opt/sonatype-work/nexus3/tmp
-XX:LogFile=/opt/sonatype-work/nexus3/log/jvm.log
-Dkaraf.log=/opt/sonatype-work/nexus3/log
2.6 配置临时目录
Nexus 临时目录默认使用 java 虚拟机临时目录来存储重要的运行时文件。
建议临时目录设置在 nexus 数据目录中的 $data-dir/tmp,同时必须满足
- nexus 服务运行用户必须是文件拥有者并具有运行权限,否则启动 nexus 会报错;
- 不要处理任何临时目录中的文件,尤其是在服务运行时;
- 确保文件目录所在磁盘拥有 1GB 以上的剩余空间;
修改临时目录,打开并编辑 $install-dir/bin/nexus.vmoptions 文件,修改 -Djava.io.tmpdir=…/sonatype-work/nexus3/tmp 所在行的值,可以是绝对目录或者 $install-dir/bin 的相对目录。
2.7 使用反向代理
在公司的服务器上,出于某些原因,nexus 需要运行在反向代理后面,比如只统一开放 80 端口等。
nginx 反向代理配置示例
http {proxy_send_timeout 120;proxy_read_timeout 300;proxy_buffering off;keepalive_timeout 5 5;tcp_nodelay on;server {listen *:80;server_name www.example;# allow large uploads of filesclient_max_body_size 1G;# optimize downloading files larger than 1G#proxy_max_temp_file_size 2G;location / {# Use IPv4 upstream address instead of DNS name to avoid attempts by nginx to use IPv6 DNS lookupproxy_pass http://127.0.0.1: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;}}
}
2.8 访问用户页面
Nexus 拥有具有完全访问权限的管理用户 admin,初始密码在 $data-dir 目录的 admin.password 文件中。可以通过点击用户页面右上角的 Sign in 按钮来登录。
2.9 安装后检查清单
- 修改 admin 密码
Nexus 在安装后启动时,为 admin 用户生成一个随机密码,写入 $data-dir 下的 admin.password 文件中。 - 配置匿名访问
- 修改管理员邮箱地址
- 配置 SMTP 设置
- 配置 HTTP 反向代理
- 为服务器做备份设置
3 Nginx 安装步骤
下载和安装 pcre 到 /usr/local/src/,支持正则表达式,nginx core 和 rewrite 依赖
wget .44/pcre-8.44.tar.gz
tar zxf pcre-8.44.tar.gz
./configure
make
make install
下载和安装 zlib 到 /usr/local/src/,支持 header 压缩,nginx Gzip 依赖
wget wget .2.11.tar.gz
tar zxf zlib-1.2.11.tar.gz
./configure
make
make install
下载安装包 nginx-1.18.0.tar.gz 到 /usr/local/src/
解压安装包到 /usr/local/
进入 /usr/local/nginx-1.18.0/
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre=../pcre-8.44 --with-zlib=../zlib-1.2.11 --with-http_ssl_module --with-stream --with-mail
make
make install
启动并验证 nginx 服务
/usr/local/nginx/nginx
访问显示欢迎页面
4 Nexus 安装步骤
第一步,新建操作系统用户 nexus
useradd nexus
查看用户所属组
groups nexus
第二步,创建目录 /opt/nexus-work,并将目录拥有者修改 nexus
mkdir /opt/nexus-work
chown nexus:nexus /opt/nexus-work
第三步,切换到 nexus 用户,解压安装包到 /opt/nexus-work 下
su nexus # 不是 su - nexus
tar zxf /usr/local/src/nexus-3.27.0-03-unix.tar.gz -C /opt/nexus-work
第四步,切换到用户 root,编辑 /etc/security/limits.conf,修改系统默认 open file descriptors 最大值
nexus - nofile 65536
[nofile] - max number of open file descriptors
第五步,创建连接,编辑 /opt/nexus-work/nexus/bin/nexus.vmoptions
# 进入目录 /opt/nexus-work/,创建 nexus -> nexus-3.27.0-03 连接
ln -s nexus-3.27.0-03 nexus# 当前目录 /opt/nexus-work/nexus,编辑 bin/nexus.vmoptions
vim bin/nexus.vmoptions
编辑后的相关项配置内容如下,没有出现的项采用默认值
-Xms2703m
-Xmx2703m
-XX:MaxDirectMemorySize=2703m
-XX:LogFile=/opt/nexus-work/sonatype-work/nexus3/log/jvm.log
-Dkaraf.data=/opt/nexus-work/sonatype-work/nexus3
-Dkaraf.log=/opt/nexus-work/sonatype-work/nexus3/log
-Djava.io.tmpdir=/opt/nexus-work/sonatype-work/nexus3/tmp
编辑 /opt/nexus-work/nexus/bin/nexus.rc,指定服务运行用户 nexus(带双引号)
run_as_user="nexus"
第六步,新增配置文件 /opt/nexus-work/sonatype-work/etc/nexus.properties,添加如下配置
application-port=9081
nexus-context-path=/nexus3/
第七步,配置 nginx 反向代理配置
http {proxy_send_timeout 120;proxy_read_timeout 300;proxy_buffering off;keepalive_timeout 5 5;tcp_nodelay on;server {listen *:80;server_name 192.168.3.37;# allow large uploads of filesclient_max_body_size 1G;# optimize downloading files larger than 1G#proxy_max_temp_file_size 2G;location /nexus3 {# Use IPv4 upstream address instead of DNS name to avoid attempts by nginx to use IPv6 DNS lookupproxy_pass http://127.0.0.1:9081/nexus3;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
}
第八步,配置 nexus 和 nginx 开机启动,并启动运行
使用 systemd 运行 nexus 服务,在 /etc/systemd/system/ 目录下创建文件 nexus.service,添加如下内容
[Unit]
Description=nexus service
After=network.target[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus-work/nexus/bin/nexus start
ExecStop=/opt/nexus-work/nexus/bin/nexus stop
User=nexus
Restart=on-abort
TimeoutSec=600[Install]
WantedBy=multi-user.target
给 nexus 添加 sudo 权限
# 添加 write 权限
chmod u+w /etc/sudoers
# 编辑
vim /etc/sudoers
# 添加如下内容
nexus ALL=(ALL) NOPASSWD: /usr/bin/systemctl
# 保存并恢复权限状态
chmod u-w /etc/sudoers
[nexus@yxftest nexus-work]$ sudo systemctl enable nexus.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nexus.service to /etc/systemd/system/nexus.service.
使用下面的命令激活服务
sudo systemctl daemon-reload
# 设置开机启动
sudo systemctl enable nexus.service
# 启动服务
sudo systemctl start nexus.service
使用 systemd 运行 nginx 服务,在 /etc/systemd/system/ 目录下创建文件 nginx.service,添加如下内容
[Unit]
Description=nginx
After=network.target[Service]
Type=forking
ExecStart= /usr/local/nginx/nginx
ExecReload= /usr/local/nginx/nginx reload
ExecStop= /usr/local/nginx/nginx quit
PrivateTmp= true[Install]
WantedBy=multi-user.target
设置开机启动
systemctl enable nginx.service
systemctl start nginx.service
【systemd】 字母 d 是守护进程 daemon 的缩写,自我理解是系统守护进程管理
启动 nexus 服务
sudo systemctl start nexus.service
查看日志 /opt/nexus-work/sonatype-work/nexus3/log/nexus.log,启动之后最后会显示如下内容
2020-09-23 10:39:24,946+0800 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer -
-------------------------------------------------Started Sonatype Nexus OSS 3.27.0-03-------------------------------------------------
第九步,访问 nexus http://192.168.3.37/nexus3,开始进行系统设置
在首页点击 Sign in 按钮进行登录
根据弹出框提示位置,查看 admin 初始密码,进行登录
向导 1/4,点击 Next
向导 2/4,修改 admin 帐号初始密码
向导 3/4,允许匿名访问,选中如图标示选项,Next
向导 4/4,完成,点击 Finish
第十步,添加阿里云 maven 代理仓库,依赖下载优先请求阿里云 maven
三种仓库类型
- proxy - 远程仓库的代理,当用户向这个仓库请求依赖包,它会先在本地查找,如果找不到的话,就会从远程仓库下载,然后返回给用户
- hosted - 宿主仓库,用户可以 deploy 到 hosted 仓库中,也可以手工上传依赖组件到 hosted 仓库里,在 远程代理仓库中获取不到的依赖组件包,就需要手工上传到 hosted 仓库里
- group - 仓库组,将 多个 proxy 或者 hosted 类型仓库聚合,对用户暴露统一的访问地址,使多个仓库可以通过同一个地址对外提供服务
现有仓库
- maven-central - maven 远程中心仓库代理(proxy),会从远程中心仓库中下载依赖
- maven-releases - 宿主仓库(hosted),只存储 release 版的文件,我们 deploy 自己项目的时候,会上传存储到该位置,供其余用户依赖使用
- maven-snapshots - 宿主仓库(hosted),和 maven-releases 类似,唯一不同的是该库只针对 snapshot 版本的操作
- maven-public - 仓库组(group),是多个仓库的组合,这里是 maven-central、maven-releases、maven-snapshots 三个仓库的组合,提供统一的对外访问地址,方便多个仓库同时对外提供服务
选择 maven2(proxy)
按照下图所示输入 自定义仓库名(Name)、阿里云 maven 库地址(Remote storage) 等
阿里云 maven 仓库地址:/
最后点击 Create repository 按钮
第十一步,添加 3rd-party 仓库,用于上传第三方依赖组件包(公司购买的商业组件包,公共仓库中不存在的部分依赖包文件)
输入页面 1/2
输入页面 2/2
第十二步,将上面两步添加的阿里云maven代理仓库和第三方组件仓库添加到 maven-public 仓库组,通过统一的 url 对外提供访问服务
添加后调整顺序如下,保存
第十三步,根据需要添加用户和角色,来控制仓库的访问权限
添加 role nx-developer,赋予匿名权限、依赖组件包上传权限和3rd-party、maven-releases、maven-snapshots 三个仓库 add|browse|edit|read 权限,可以根据需要和实践进行调整
添加用户 yuandian,并分配 nx-anonymous 和 nx-devoloper 角色
nexus 是典型的给 role 赋予具体资源的权限,然后通过给 user 分配 role 来控制用户的资源的访问权限
nexus 配置和部署到此基本完成。
5 Apache Maven 配置
要结合 maven 访问部署的 nexus 服务,需要对 maven 的配置进行修改,配置如下
<settings><localRepository>/path/to/your/localRepository</localRepository><servers><server><!-- id 与 下面的 repository/mirror 的 id 匹配,username 和 password 为 nexus 服务的访问帐号 --><id>nexus-xxx</id><username>username</username><password>password</password></server></servers><mirrors><mirror><!-- 此 id 与 server id 匹配,以便连接 mirror 时从 server 配置获取凭证 --><id>nexus-xxx</id><!-- * 表示对任何仓库的请求,都会统一重定向到配置的 url 指向的统一的 仓库组 --><mirrorOf>*</mirrorOf><url>http://192.168.3.37/nexus3/repository/maven-public/</url><name>nexus-public</name></mirror></mirrors><profiles><profile> <id>nexus</id><!-- 仓库 --><repositories><repository><!-- 统一开启 仓库的 snapshot 功能,nexus-xxx 已被 mirror 通过 * 统一映射到 http://192.168.3.37/nexus3/repository/maven-public/,所以该处的 url 会被覆盖掉 --><id>nexus-xxx</id><url>http://nexus-xxx</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><!-- 插件仓库,配置同上 --><pluginRepositories><pluginRepository><id>nexus-xxx</id><url>http://nexus-xxx</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories></profile></profiles><activeProfiles><!-- 激活上面的 profile --><activeProfile>nexus</activeProfile></activeProfiles>
</settings>
在项目 pom 文件中配置 maven,指定 release 和 snapshot 仓库地址
<distributionManagement><repository><id>nexus-xxx</id><name>Nexus Releases</name><url>http://192.168.3.37/nexus3/repository/maven-releases</url></repository><snapshotRepository><id>nexus-xxx</id><name>Nexus Snapshot</name><url>http://192.168.3.37/nexus3/repository/maven-snapshots</url></snapshotRepository></distributionManagement>
6 采坑问题
nexus 启动之后,报异常如下
org.apache.http.conn.ConnectTimeoutException: Connect to sonatype-download.global.ssl.fastly:443 [sonatype-download.global.ssl.fastly/128.242.240.20] failed: connect timed outat org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)at com.sonatype.nexus.plugins.outreach.internal.outreach.OutreachConnector.get(OutreachConnector.java:136)at com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach.remote(SonatypeOutreach.java:273)at com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach.getOutreachBundle(SonatypeOutreach.java:158)at com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach.getPageBundle(SonatypeOutreach.java:147)at com.sonatype.nexus.plugins.outreach.Outreach$getPageBundle$0.call(Unknown Source)at com.sonatype.nexus.plugins.outreach.internal.ui.OutreachComponent.readStatus(OutreachComponent.groovy:62)at com.sonatype.nexus.plugins.outreach.internal.ui.OutreachComponent$$EnhancerByGuice$$6d40061b.CGLIB$readStatus$6(<generated>)at com.sonatype.nexus.plugins.outreach.internal.ui.OutreachComponent$$EnhancerByGuice$$6d40061b$$FastClassByGuice$$30bfbcc8.invoke(<generated>)at com.google.inject.internal.cglib.proxy.$MethodProxy.invokeSuper(MethodProxy.java:228)at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:76)at com.palominolabs.metrics.guice.ExceptionMeteredInterceptor.invoke(ExceptionMeteredInterceptor.java:23)at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:78)at com.palominolabs.metrics.guice.TimedInterceptor.invoke(TimedInterceptor.java:26)at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:78)at com.google.inject.internal.InterceptorStackCallback.intercept(InterceptorStackCallback.java:54)at com.sonatype.nexus.plugins.outreach.internal.ui.OutreachComponent$$EnhancerByGuice$$6d40061b.readStatus(<generated>)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
解决办法
admin 登录 nexus,进入如下图页面
点击 Disable 按钮,将 Outreach:Management 状态 设为不可用
个人理解为访问部分的国外地址不可达,从而报超时错误,关了就不访问了
~~~~~~~~ 已到底线 ~~~~~~~~
更多推荐
在 Linux 系统上安装 nexus 私库的相关理解和详细操作记录(主要依据官方文档)
发布评论