一、Nexus搭建环境
环境:Ubuntu 16.04.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://help.sonatype.com/repomanager2/download
2、Nexus 资源包解压
2.1、在 /usr/local
目录中创建子目录 nexus
mdkir /usr/local/nexus
2.2、解压Nexus到指定目录
tar -zxvf nexus-2.14.10-01-bundle.tar.gz -C /usr/local/nexus
Nexus压缩包中包含两个子目录:
nexus-2.14.10-01
和sonatype-work
。nexus-2.14.10-01
是具体的私服应用内容;sonatype-work
是Nexus私服下载的构件存放工作目录。
Nexus 目录结构
Nexus 各个目录结构说明
目录名称 |
目录说明 |
---|---|
nexus-2.14.10-01/bin | nexus 启动相关文件 |
nexus-2.14.10-01/conf | nexus配置文件 |
nexus-2.14.10-01/lib | nexus依赖的包文件 |
nexus-2.14.10-01/logs | nexus日志文件 |
nexus-2.14.10-01/nexus | nexus web界面工程文件,使用Jetty启动项目 |
nexus-2.14.10-01/tmp | nexus 临时文件 |
sonatype-work/nexus | nexus 运行工作下载的文件 |
3、检查私服端口和工作目录
在nexus-2.14.10-01
目录中有子目录 conf
,其中保存私服应用的配置信息。查看nexus.properties文件,确定私服访问端口和工作目录。如果没有端口相关冲突,该配置文件不做任何内容修改。配置文件内容如下:
# Sonatype Nexus
# ==============
# This is the most basic configuration of Nexus.
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF
# orientdb buffer size in megabytes
storage.diskCache.bufferSize=4096
4、修改Nexus运行用户
Nexus私服启动后,私服应用需要访问Linux的文件系统,所以需要有足够的权限。Nexus的启动脚本文件中,可以指定应用的访问用户,此信息在nexus-2.14.10-01/bin/nexus
脚本文件中定义。需修改如下信息:
# IMPORTANT - Make sure that the user has the required privileges to write into the Nexus installation directory.
# NOTE - This will set the user which is used to run the Wrapper as well as
# the JVM and is not useful in situations where a privileged resource or
# port needs to be allocated prior to the user being changed.
RUN_AS_USER=root # 修改后的内容,代表Nexus私服使用root用户权限
一般不建议使用root用户权限启动,可以新建一个nexus用户,启动Nexus。
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中限制端口的访问
6、启动并测试访问
启动Nexus私服
`/usr/local/nexus/nexus-2.14.10-01/bin/nexus start`
启动成功日志
常用的命令
nexus start # 启动
nexus stop # 停止
nexus status # 查看状态
nexus restart # 重启
启动成功访问:http://[你的IP]:8081/nexus
四、阿里云服务外网域名访问
阿里云服务器配置外网域名可以访问,配置域名解析,通过Nginx配置访问nexus服务。
1、添加阿里云域名解析,如 nexus.[你的域名]
为例。
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、通过域名测试访问是否生效。
特别注意:nexus2 的访问域名为:
http://nexus.[你的域名]/nexus