阿里雲 ECS服務器(CentOS 7)安裝和使用Nexus教程

一、前言
最近項目須要搭建maven私服,方便管理後期團隊成員使用上傳本身的包,所以決定使用nexus來搭建私服,搭建好的nexus地址html

準備工做

阿里雲服務器ECS一臺 1核CPU 2G內存(注意:最低爲2G,不然運行報錯)java

clipboard.png

開始安裝

安裝java

java的安裝網上的文章好多,不過我是本身寫的shell文件安裝的,以下:linux

#!/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文件,以下所示:nginx

clipboard.png

安裝maven

maven安裝我也是本身寫的shell文件,若是大家不想用個人能夠到網上找文章看看吧,下面是個人shell文件:git

#!/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文件,以下所示:github

clipboard.png

安裝nexus

nexus雖然我也是使用shell文件安裝,但有些配置咱們仍是要手動設置,下面是安裝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 '接下來配置:一、vim bin/nexus.rc run_as_user="nexus"'

安裝完成以後,咱們到/usr/local/nexus/nexus-3.15.0-01/bin目錄下修改nexus文件apache

# 設置本地jdk目錄
INSTALL4J_JAVA_HOME_OVERRIDE="/usr/local/java/jdk1.8.0_201"

接着,咱們相同目錄下nexus.rc文件,以下 vim

# 指定用戶是nexus而不是root,若是是root會出現警告!
run_as_user="nexus"

好了,這樣就安裝好了,咱們訪問下網站,注意默認端口是8081,帳號:admin,密碼:admin123 centos

clipboard.png

免費申請阿里雲SSL證書

clipboard.png

購買成功以後,配置好域名,我這裏的域名是: nexus.awbeci.com,設置好以後等待審覈,審覈成功以後選擇nginx版本並下載證書

clipboard.png

clipboard.png

下載完成以後是個.zip的壓縮包,咱們上傳到服務器上,使用下面命令:

scp your-cert.zip root@your-server-ip:/your-server-directory

上傳成功以後咱們等待下一步操做。

安裝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

clipboard.png

開始使用Nexus

Nexus介紹

這裏有一篇文章寫得很是好,你們能夠看看,我就不特別詳細的寫介紹了,主要仍是告訴大家怎麼結合項目使用。

建立Blob Stores

clipboard.png

建立User

clipboard.png

建立阿里雲的代理倉庫

clipboard.png

而後再public組裏面將這個aliyun-proxy倉庫加入,排在maven-central以前便可

clipboard.png

建立Host倉庫 策略是release

clipboard.png

建立Host倉庫 策略是snapshots

clipboard.png

建立好以後咱們再來Public設置下優先順序,把剛纔加的兩個倉庫放到aliyun-proxy前面

clipboard.png

建立完倉庫預覽

clipboard.png

配置本地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>

配置要上傳到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>

編輯併發布jar包

mvn clean && mvn deploy -DskipTests=true

執行完成後,咱們到線上看看是否上傳成功

clipboard.png

clipboard.png

能夠看到不論是release版本仍是snapshot版本都上傳併發布成功

測試發佈的包

本身新建一個maven項目,而後引入咱們剛纔發佈的release包

<dependency>
    <groupId>com.awbeci</groupId>
    <artifactId>awbeci-core</artifactId>
    <version>1.0.8-SNAPSHOT</version>
</dependency>

clipboard.png
執行該代碼,以下所示:

clipboard.png

測試成功!!!

上傳第三方包

有兩種方式,一種是命令

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上傳

clipboard.png

兩種方式結果都是同樣,就看你偏向哪一種了。

總結

服務器內存剛開始配置是1CPU 1G 內存,nexus start運行以後報錯,升級服務器爲2G內存以後就沒問題了

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端口了。

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

最好本身建立nexus用戶,不要使用root用戶啓動nexus不然會出現警告

WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended! 
WARNING: ************************************************************
Starting nexus

啓動nexus:

/usr/local/nexus/nexus-3.15.0-01/bin/nexus {start|stop|run|run-redirect|status|restart|force-reload}

引用

  1. Maven私服Nexus3.x環境構建操做記錄
  2. Nexus 3.x Linux環境搭建(手把手) 排坑之旅
  3. Centos7.3安裝nexus-3.14.0-04
  4. 搭建nexus3版的maven私服(Centos7環境)
  5. Gitlab+Nexus Maven部署
  6. Linux 使用 Nexus3.x 搭建 Maven 私服指南
  7. maven私服nexus3.x搭建與使用
  8. centos7搭建nexus maven私服
  9. centos7搭建nexus maven私服
  10. Maven Nexus
  11. Nexus 私有倉庫搭建與 Maven 集成
  12. Nexus max file descriptors
  13. maven私服nexus3.x環境配置
  14. 搭建Maven私服-Nexus
相關文章
相關標籤/搜索