Nexus安装和使用
1、前言
最近項目需要搭建maven私服,方便管理后期團隊成員使用上傳自己的包,因此決定使用nexus來搭建私服,搭建好的nexus地址。
2、準備工作
阿里云服務器ECS一臺 1核CPU 2G內存(注意:最低為2G,否則運行報錯)
3、開始安裝
3.1 安裝java
java的安裝網上的文章好多,不過我是自己寫的shell文件安裝的,如下:
#!/bin/bash
# jdk install
# 請將下載的jdk-xxx-linux-xxx.tar.gz包與此腳本放置到同一目錄
# 授予此腳本可執行權限(chmod +x install_jdk.sh)
# 在終端執行此腳本開始安裝(./文件名)
# 注意:不可有多個版本的jdk包!
# 為了使配置的環境變量生效,安裝完成后你應該重新登陸。
jvmpath=/usr/local/java
# 不存在
if [ ! -d "$jvmpath" ]; then
echo "正在創建$jvmpath目錄"
sudo mkdir $jvmpath
echo "目錄$jvmpath創建成功"
fi
jdkfile=$(ls | grep jdk-*-linux-*.gz)
jdkdirname="jdk1.8.0_201"
if [ ! -f "$jdkfile" ]; then
echo "正在下載jdk請稍等..."
wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz"
fi
jdkfile=$(ls | grep jdk-*-linux-*.gz)
if [ -f "$jdkfile" ]; then
sudo tar -zxvf $jdkfile -C /usr/local/java/
echo "安裝JDK成功"
echo "配置環境變量"
mv ~/.bashrc ~/.bashrc.backup.java
cat ~/.bashrc.backup.java >> ~/.bashrc
echo "PATH="$PATH:$jvmpath/$jdkdirname/bin"" >> ~/.bashrc
echo "JAVA_HOME=$jvmpath/$jdkdirname" >> ~/.bashrc
echo "CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar" >> ~/.bashrc
source ~/.bashrc
echo "配置環境成功"
echo "測試是否安裝成功"
java -version
echo "安裝成功"
fi
執行該shell文件,如下所示:
3.2 安裝maven
maven安裝我也是自己寫的shell文件,如果你們不想用我的可以到網上找文章看看吧,下面是我的shell文件:
#!/bin/bash
# maven install
mvnpath=/usr/local/maven
# 不存在
if [ ! -d "$mvnpath" ]; then
echo "正在創建$mvnpath目錄"
sudo mkdir $mvnpath
echo "目錄$mvnpath創建成功"
fi
#apache-maven-3.6
echo "正在下載maven安裝包,請稍等..."
wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "http://211.162.31.136/files/71480000031E20AE/mirrors.hust.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz"
mvnfile=$(ls | grep apache*maven-*.gz)
if [ -f "$mvnfile" ]; then
#這個名字其實就是mvn .tar.gz解壓之后的文件夾的名字
mvndirname="apache-maven-3.6.0"
#不能加 用'zxvf' 加了 z 就創建了包里面的apace* 文件夾,而我們只要把apace*文件夾下的文件全部解壓到 mvnpath里面就好
tar zxvf $mvnfile -C $mvnpath
echo "安裝maven成功"
echo "配置環境變量"
mv ~/.bashrc ~/.bashrc.backup.mvn
cat ~/.bashrc.backup.mvn >> ~/.bashrc
echo "PATH="$PATH:$mvnpath/$mvndirname/bin"" >> ~/.bashrc
echo "MAVEN_HOME=$mvnpath/$mvndirname" >> ~/.bashrc
source ~/.bashrc
echo "配置環境成功"
echo "測試是否安裝成功"
mvn -v
echo "安裝成功"
else
echo "沒有找到maven文件"
fi
執行該shell文件,如下所示:
3.3 安裝nexus
nexus雖然我也是使用shell文件安裝,但有些配置我們還是要手動設置,下面是安裝shell文件:
#!/bin/bash
#判斷是否是roo用戶
if [ $(id -u) != "0" ]; then
echo "Error:You must be root to run this script"
fi
#每次使用只需修改自定義內容即可
#自定義用戶名和組
Group_Name="nexus"
User_Name="nexus"
#自定義nginx變量
Install_Path="/usr/local/nexus"
Version="nexus-3.15.0-01"
Package_Type=".tar.gz"
Package=$Version$Package_Type
#創建/usr/local/nexus目錄
#mkdir /usr/local/nexus
if [ -e $Install_Path ]
then
echo " $Install_Path 目錄已經存在."
echo " $Install_Path Directory Already Exists."
else
echo " $Install_Path 目錄正在創建."
mkdir $Install_Path
fi
#下載nexus 文件
Setup_path="/root/"
cd $Setup_path
wget https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.15.0-01-unix.tar.gz
Group_User(){
egrep "^$Group_Name" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
echo "nexus 用戶組正在添加."
groupadd $Group_Name
else
echo " The $Group_Name user group already exists."
echo "nexus 用戶組已經添加."
fi
#判斷nexus用戶是否存在
egrep "^$User_Name" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
echo "nexus 用戶正在添加."
useradd -g $Group_Name $User_Name
else
echo "nexus 用戶已經添加."
echo " The $User_Name user already exists."
fi
}
Group_User
# 設置/usr/local/nexus 目錄所屬組和用戶是nexus
chown -R nexus:nexus $Install_Path
#判斷文件是否存在
if [ -e $Setup_path$Version$Package_Type ]
then
echo "$Package The Package exists."
else
echo "$Package The package does not exist."
fi
cd $Setup_path
#解壓nexus包到/usr/local/nexus
tar -zxvf $Package -C $Install_Path
echo '設置環境變量'
mv ~/.bashrc ~/.bashrc.backup.nexus
cat ~/.bashrc.backup.nexus >> ~/.bashrc
echo "NEXUS_HOME=$Install_Path/$Version" >> ~/.bashrc
echo "PATH="$PATH:$NEXUS_HOME/bin"" >> ~/.bashrc
# 切換nexus用戶
su nexus
echo '接下來配置:1、vim bin/nexus.rc run_as_user="nexus"'
安裝完成之后,我們到/usr/local/nexus/nexus-3.15.0-01/bin目錄下修改nexus文件
# 設置本地jdk目錄
INSTALL4J_JAVA_HOME_OVERRIDE="/usr/local/java/jdk1.8.0_201"
接著,我們相同目錄下nexus.rc文件,如下
# 指定用戶是nexus而不是root,如果是root會出現警告!
run_as_user="nexus"
好了,這樣就安裝好了,我們訪問下網站,注意默認端口是8081,賬號:admin,密碼:admin123
3.4 免費申請阿里云SSL證書
購買成功之后,配置好域名,我這里的域名是: nexus.awbeci.com,設置好之后等待審核,審核成功之后選擇nginx版本并下載證書
下載完成之后是個.zip的壓縮包,我們上傳到服務器上,使用下面命令:
scp your-cert.zip root@your-server-ip:/your-server-directory
上傳成功之后我們等待下一步操作。
3.5 安裝nginx并代理nexus 8081端口,同時配置https訪問
因為訪問網站的時候端口是8081,所以想要使用80端口訪問的話,我們就用nginx 80端口代理8081,同時設置https訪問
安裝nginx 我們還是通過shell文件來安裝,如下
#!/bin/bash
#判斷是否是roo用戶
if [ $(id -u) != "0" ]; then
echo "Error:You must be root to run this script"
fi
#每次使用只需修改自定義內容即可
#自定義用戶名和組
Group_Name="nginx"
User_Name="nginx"
#自定義nginx變量
Install_Path="/usr/local/nginx"
Package_Type=".tar.gz"
Version="nginx-1.15.8"
Package=$Version$Package_Type
Setup_path="/root/"
RPM="nginx"
#創建/usr/local/nginx目錄
#mkdir /usr/local/nginx
if [ -e $Install_Path ]
then
echo " $Install_Path 目錄已經存在."
echo " $Install_Path Directory Already Exists."
else
echo " $Install_Path 目錄正在創建."
mkdir $Install_Path
fi
#下載nginx 文件
cd $Setup_path
wget http://nginx.org/download/nginx-1.15.8.tar.gz
#安裝依賴關系
yum group install "Development Tools" "Server Platform Deveopment"
yum install -y curl openssl-devel pcre-devel
Group_User(){
egrep "^$Group_Name" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
echo "nginx 用戶組正在添加."
groupadd $Group_Name
else
echo " The $Group_Name user group already exists."
echo "nginx 用戶組已經添加."
fi
#判斷nginx用戶是否存在
egrep "^$User_Name" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
echo "nginx 用戶正在添加."
useradd -g $Group_Name $User_Name
else
echo "nginx 用戶已經添加."
echo " The $User_Name user already exists."
fi
}
Group_User
#判斷文件是否存在
if [ -e $Setup_path$Version$Package_Type ]
then
echo "$Package The Package exists."
else
echo "$Package The package does not exist."
fi
#編譯安裝nginx
cd $Setup_path
#解壓nginx包到/usr/local/nginx
tar -zxvf $Package -C $Install_Path
cd $Install_Path
cd $Version
configure_opts=(
--prefix=$Install_Path
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_flv_module
--with-http_stub_status_module
--with-http_gzip_static_module
)
./configure ${configure_opts[@]}
if [[ $? -eq 0 ]]
then
make && make install
else
echo "編譯失敗,請重新編譯" && exit 1
fi
#添加Nginx命令到環境變量
cat >/etc/profile.d/nginx.sh <<EOF
export PATH=/usr/local/nginx/sbin/:$PATH
EOF
source /etc/profile
#啟動服務
/usr/local/nginx/sbin/nginx
ss -tnlp | grep nginx
安裝成功后,我們把一步上傳的證書.zip復制到/root文件夾下,并解壓縮,如下:
# 創建ssl文件夾
mkdir -p /usr/local/nginx/cert
# 把上一步的.zip證書解壓并復制到ssl文件夾下
unzip /root/your-cert-package.zip
# 解壓之后應該是兩個文件.pem和.key
# 復制.crt和.key文件到ssl目錄下
cp your-cert.crt your-cert.key /usr/local/nginx/cert
設置好之后,接下來我們配置/usr/local/nginx/conf/nginx.conf文件,如下所示:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 100m;
client_header_timeout 1m;
client_body_timeout 1m;
proxy_connect_timeout 18000; ##修改成半個小時
proxy_send_timeout 18000;
proxy_read_timeout 18000;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name nexus.awbeci.com;
return 301 https://nexus.awbeci.com$request_uri;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8081; #代理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;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
# HTTPS server
#
server {
listen 443 ssl;
server_name nexus.awbeci.com;
ssl_certificate /usr/local/nginx/cert/nexus.awbeci.com.pem;
ssl_certificate_key /usr/local/nginx/cert/nexus.awbeci.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8081; #代理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;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
}
這樣就成功的配置好了,現在我們重啟下nginx,并訪問nexus.awbeci.com網站看看
# 重啟nginx
/usr/local/nginx/sbin/nginx -s reload
4、開始使用Nexus
4.1 Nexus介紹
這里有一篇文章寫得非常好,大家可以看看,我就不特別詳細的寫介紹了,主要還是告訴你們怎么結合項目使用。
4.2 創建Blob Stores
4.3 創建User
4.4 創建阿里云的代理倉庫
然后再public組里面將這個aliyun-proxy倉庫加入,排在maven-central之前即可
4.5 創建Host倉庫 策略是release
4.6 創建Host倉庫 策略是snapshots
創建好之后我們再來Public設置下優先順序,把剛才加的兩個倉庫放到aliyun-proxy前面
創建完倉庫預覽
4.7 配置本地maven settings.xml
提示:兩種配置方法,一種是直接配置maven目錄下的conf下的settings.xml文件,另外一種是復制該文件到用戶目錄下的.m2目錄,兩種方法配置效果是一樣的,看個人喜好了,加載順序是.m2下的settings.xml目錄接著是maven config目錄下的settings.xml,配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!--這里配置我們剛才創建的user用戶所對應的releases-->
<server>
<id>releases</id>
<username>user</username>
<password>123456</password>
</server>
<!--這里配置我們剛才創建的user用戶所對應的Snapshots-->
<server>
<id>Snapshots</id>
<username>user</username>
<password>123456</password>
</server>
</servers>
<mirrors>
<!-- <mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror> -->
<!--這里配置我們線上的public倉庫就好-->
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>https://nexus.awbeci.com/repository/maven-public/</url>
</mirror>
</mirrors>
</settings>
4.8 配置要上傳到nexus項目pom.xml文件
<packaging>jar</packaging>
<distributionManagement>
<!--配置線上releases倉庫地址,只要是正式版本都會上傳到該地址(注意要和settings.xml文件里面的配置名稱相同)-->
<repository>
<id>releases</id>
<url>https://nexus.awbeci.com/repository/awbeci/</url>
</repository>
<!--配置線上Snapshots倉庫地址,只要是快照版本都會上傳到該地址(注意要和settings.xml文件里面的配置名稱相同)-->
<snapshotRepository>
<id>Snapshots</id>
<url>https://nexus.awbeci.com/repository/awbeci-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4.9 編輯并發布jar包
mvnclean && mvn deploy -DskipTests=true
執行完成后,我們到線上看看是否上傳成功
可以看到不管是release版本還是snapshot版本都上傳并發布成功
4.10 測試發布的包
自己新建一個maven項目,然后引入我們剛才發布的release包
<dependency>
<groupId>com.awbeci</groupId>
<artifactId>awbeci-core</artifactId>
<version>1.0.8-SNAPSHOT</version>
</dependency>
執行該代碼,如下所示:
測試成功!??!
4.10 上傳第三方包
有兩種方式,一種是命令
mvn deploy:deploy-file
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<type-of-packaging>
-Dfile=<path-to-file>
-DrepositoryId=<server-id-settings.xml>
-Durl=<url-of-the-repository-to-deploy>
另外一種是使用Nexus上傳
兩種方式結果都是一樣,就看你偏向哪種了。
5、總結
1)服務器內存剛開始配置是1CPU 1G 內存,nexus start運行之后報錯,升級服務器為2G內存之后就沒問題了
2)nexus 默認是8081端口,我們可以修改文件/usr/local/nexus/nexus-3.15.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
值得注意的是不能把端口直接改成80,這樣你就不能啟動nexus,所以我們是通過nginx 80端口代理8081端口了。
3)nexus 配置內存是在/usr/local/nexus/nexus-3.15.0-01/bin/nexus.vmoptions
-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
4)最好自己創建nexus用戶,不要使用root用戶啟動nexus否則會出現警告
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Starting nexus
5)啟動nexus:
/usr/local/nexus/nexus-3.15.0-01/bin/nexus {start|stop|run|run-redirect|status|restart|force-reload}
6、引用
Maven私服Nexus3.x環境構建操作記錄
Nexus 3.x Linux環境搭建(手把手) 排坑之旅
Centos7.3安裝nexus-3.14.0-04
搭建nexus3版的maven私服(Centos7環境)
Gitlab+Nexus Maven部署
Linux 使用 Nexus3.x 搭建 Maven 私服指南
maven私服nexus3.x搭建與使用
centos7搭建nexus maven私服
centos7搭建nexus maven私服
Maven Nexus
Nexus 私有倉庫搭建與 Maven 集成
Nexus max file descriptors
maven私服nexus3.x環境配置
搭建Maven私服-Nexus
總結
以上是生活随笔為你收集整理的Nexus安装和使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 好看的漫画排行榜玄幻冒险(好看的漫画排行
- 下一篇: 什么是禅让制(禅让制传说是指什么)