---
title: "搭建maven私服"
author: "hetaoo"
date: "2016-11-23T00:04:13Z"
tags: []
categories: ["开源软件"]
slug: "maven"
draft: false
lastmod: "2016-11-23T00:04:12Z"
id: 2339818
url: "https://hetaoo.iteye.com/blog/2339818"
---

安装`Nexus`的前提是已经安装JDK,JDK的安装这里就不再赘述,安装`JDK-1.7`版本,JDK版本过低在启动Nexus的时候会报错.

### 下载与启动

下载链接为[http://www.sonatype.org/nexus/go/](http://www.sonatype.org/nexus/go/) 最新版本为 `nexus-2.11.4-01` ,我们下载`NEXUS OSS(TGZ)`. 得到`nexus-2.11.4-01-bundle.tar.gz`

1. 将`nexus-2.11.4-01-bundle.tar.gz`复制到`/usr/local`目录下

```
 sudo cp nexus-2.11.4-01-bundle.tar.gz /usr/local
 cd /usr/local
```

2. 解压`nexus-2.11.4-01-bundle.tar.gz`,得到`nexus-2.11.4-01`和`sonatype-work`两个文件夹,为了以后操作方便,我们将创建软链接`nexus`指向`nexus-2.11.4-01`

```
sudo tar -zxvf nexus-2.11.4-01-bundle.tar.gz
sudo ln -s nexus-2.11.4-01 nexus 
```

3. 启动`Nexus`

```
cd nexus 
sudo bin/nexus
```

输出:

```
Usage: bin/nexus { console | start | stop | restart | status | dump }
```

根据英文即可知道每个命令的意思,现在启动`Nexus`

```
sudo bin/nexus start
```

结果输出:

```
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
If you insist running as root, then set the environment variable 

RUN_AS_USER=root before running this script.
```

此时修改`bin/nexus`文件设置`RUN_AS_USER`

```
sudo vim bin/nexus

将 #RUN_AS_USER=
改成 RUN_AS_USER=root
```

重新启动`Nexus`

```
sudo bin/nexus start
```

输出:

```
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Starting Nexus OSS...
Started Nexus OSS.
```

打开`localhost:8081/nexus` 进行查看,启动成功.如果打不开怎么办? 查看日志

```
vim logs/wrapper.log
```

根据错误提示来修改配置文件.

4. 修改`nexus`的端口

```
sudo vim conf/nexus.properties
将application-port=8081 改成你想设置的端口,例如9000.
```

`nexus.properties`文件中的`nexus-work`：指定构件库的存储位置

### 将Nexus设置为系统自启动服务

1. 复制`/usr/local/nexus/bin/nexus`文件到`/etc/init.d/`

```
sudo cp /usr/local/nexus/bin/nexus /etc/init.d/
```

2. 修改`/etc/init.d/nexus`

```
sudo vim /etc/init.d/nexus
```

添加修改以下变量:

```
NEXUS_HOME="/usr/local/nexus"
RUN_AS_USER=root
PIDDIR="/var/run"
```

3. 启动`nexus`服务

```
sudo service nexus start  #启动
sudo service nexus stop   #停止服务
sudo service nexus status #查看服务状态
```

启动之后,跟之前所述一样,打开`http://localhost:8081/nexus`即可.

 提示

默认登录用户名为admin，密码admin123

### 使用nginx

nginx是一个非常轻量级的HTTP服务器，nginx，它的发音为“engine X”， 是一个高性能的HTTP和反向代理服务器，同时也是一个IMAP/POP3/SMTP 代理服务器。

1. `nginx`安装

```
sudo apt-get install nginx
```

默认`nginx`安装,所有的配置文件都在`/etc/nginx`下，并且每个虚拟主机已经安排在了`/etc/nginx/sites-available`下, 启动程序文件在`/usr/sbin/nginx`,日志放在了`/var/log/nginx`中，分别是`access.log`和`error.log`,并已经在`/etc/init.d/`下创建了启动脚本`nginx`,默认的虚拟主机的目录设置在了`/usr/share/nginx/www`

安装过之后,默认已经启动.查看是否启动:

```
sudo service nginx status #查看状态
sudo service nginx stop  #停止服务
sudo service nginx start #启动服务
```

启动之后,打开`http://localhost/`即可.如果不能访问，先不要继续，看看是什么原因，解决之后再继续。

2. 配置`nginx`

```
cd /etc/nginx/conf.d
sudo vim nexus.conf
```

将下列内容复制进去

```
#
# The default server
#
server {
    listen       80  ;
    server_name  127.0.0.1;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    # Load configuration files for the default server block.

    location /nexus {
           proxy_set_header Via "nginx";

           proxy_read_timeout 120;
           proxy_send_timeout 120;
           proxy_connect_timeout 120;
           proxy_set_header  X-Real-IP  $remote_addr;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_redirect off;
           proxy_pass http://127.0.0.1:8081/nexus;      
    }

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}
```

重启`nginx`

```
sudo service restart nginx
```

如果你的机器同时安装了`Apache`，此时`nginx`都可能启动不了，这是因为它们都是用了`80`这个端口。两种方法:停止`apache`或者将nginx的端口修改为`8080`,将`nexus.conf`配置文件中的 `listen 80`改为`listen 8080`

启动成功之后,访问`http://127.0.0.1/nexus`即可.

### 配置nexus

1. 配置`nexus`开启远程索引 新搭建的`neuxs`环境只是一个空的仓库，需要手动和远程中心库进行同步，`nexus`默认是关闭远程索引下载，最重要的一件事情就是开启远程索引下载。登陆`nexus`系统，默认用户名密码为`admin/admin123`。

点击左侧的`Views/Repositories`,下拉菜单中有`Repositories`选项,点击`Repositories`,找到右边仓库列表中的三个仓库`Apache Snapshots`，`Codehaus Snapshots`和`Central`，然后在这三个仓库的`configuration`下把`Download Remote Indexes`修改为`true`。然后在这三个仓库上分别右键，选择`Repair Index`，这样`nexus`就会去下载远程的索引文件。

2. 建立内部仓库 新建公司的内部仓库，步骤为页面上部的`Add –> Hosted Repository`，在页面的下半部分输入框中填入`Repository ID`和`Repository Name`即可，比如分别填入`myrepo`和`my repository`，另外把`Deployment Policy`设置为`Allow Redeploy`，点击`save`就创建完成了.
3. 修改`neuxs`仓库组 `nexus`中仓库组的概念是`Maven`没有的，在`Maven`看来，不管你是`hosted`也好，`proxy`也好，或者`group`也好，对我都是一样的，我只管根据 `groupId`，`artifactId`，`version`等信息向你要构件。为了方便`Maven`的配置，`nexus`能够将多个仓库，`hosted`或者`proxy`合并成一个`group`，这样，`Maven`只需要依赖于一个`group`，便能使用所有该`group`包含的仓库的内容。

`neuxs`中默认自带了一个名为`Public Repositories`组，点击该组可以对他保护的仓库进行调整。同时创建一个`Group ID`为`public-snapshots`、`Group Name`为`Public Snapshots Repositories`的组，把`Apache Snapshots`、`Codehaus Snapshots`和`Snapshots`加入其中。

到这里`neuxs`的安装配置就完成了.

 

 二、

搭建Nexus：

要先装好jdk

下载地址：[http://www.sonatype.org/nexus/archived\_version?id=38](http://www.sonatype.org/nexus/archived_version?id=38)

1

2

3

`[root@MiWiFi-R1CM-srv src]# wget`

`[root@MiWiFi-R1CM-srv src]# tar -zxf nexus-2.9.0-bundle.tar.gz -C /usr/local/`

`[root@MiWiFi-R1CM-srv src]# mv /usr/local/nexus-2.9.0-bundle /usr/local/nexus`

 

配置nexus（根据需要配置nexus监听的端口号）：

1

`sed` `-i 's/application-port=8081/application-port=8088/g'` `nexus/conf/nexus.properties`

 

启动方法一、

配置环境变量(**RUN\_AS\_USER=root**)表示使用root运行nexus：

1

2

3

4

`[root@MiWiFi-R1CM-srv ~]# echo -e '#!/bin/bash\nexport NEXUS_HOME=/usr/local/nexus\nexport PATH=$PATH:$NEXUS_HOME/bin\nexport RUN_AS_USER=root' >/etc/profile.d/nexus.sh`

`[root@MiWiFi-R1CM-srv ~]# source /etc/profile.d/nexus.sh`

`[root@MiWiFi-R1CM-srv local]# /usr/local/nexus/bin/nexus start`

`[root@MiWiFi-R1CM-srv local]# netstat -tlunp |grep 8088`

 

tcp        0      0 0.0.0.0:8088                0.0.0.0:*                   LISTEN      2671/java

 

启动方法二（service）、

step1（拷贝启动脚本）

1

2

`[root@MiWiFi-R1CM-srv local]# cd /usr/local/nexus/`

`[root@MiWiFi-R1CM-srv nexus]# cp bin/jsw/linux-x86-64/nexus /etc/init.d/`

step2（修改脚本）

1

`[root@MiWiFi-R1CM-srv nexus]# vim /etc/init.d/nexus`

新增六行参数

1

2

3

4

5

6

`# chkconfig: 2345 66 39`

`# description: running&stop nexus service.`

`RUN_AS_USER=root`

`NEXUS_HOME=/opt/pht/nexus-2.5.1-01`

`PLATFORM=linux-x86-64`

`PLATFORM_DIR="${NEXUS_HOME}/bin/jsw/${PLATFORM}"`

 

修改三行参数

1

2

3

`WRAPPER_CMD="${PLATFORM_DIR}/wrapper"`

`WRAPPER_CONF="${PLATFORM_DIR}/../conf/wrapper.conf"`

`PIDDIR="${NEXUS_HOME}"`

 

编辑warpper配置文件

1

`[root@MiWiFi-R1CM-srv nexus]# vim /usr/local/nexus/bin/jsw/linux-x86-64/../conf/wrapper.conf`

修改一行参数：

修改前：

1

`wrapper.java.command=java`

修改后（你的jdk安装目录）：

1

`wrapper.java.command=/usr/jdk1.7.0_67/bin/java`

step3(增加执行权限)

1

`[root@MiWiFi-R1CM-srv nexus]# chmod +x /etc/init.d/nexus`

step4（交给chkconfig管理）

1

`[root@MiWiFi-R1CM-srv nexus]# chkconfig --add nexus`

step5（启动）

1

`[root@MiWiFi-R1CM-srv nexus]# service nexus start`

step6（排错）

1

2

`[root@MiWiFi-R1CM-srv nexus]#  ps -ef |grep nexus`

`root     12762 34939  0 10:08 pts/2`    `00:00:00 grep` `nexus`

nexus进程刚开始会出现，过一会就没有了，继续查看连接状态

1

2

`[root@MiWiFi-R1CM-srv nexus]# netstat -tlun |grep 7777`

`[root@MiWiFi-R1CM-srv nexus]#`

没有连接，查看wrapper日志文件

 

启动报错解决：

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

`wrapper  | --> Wrapper Started as Daemon`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | JVM exited while` `loading the application.`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 2    | wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 3    | wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 4    | wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 5    | wrapper  | Unable to start JVM: No such file` `or directory (2)`

`wrapper  | There were 5 failed launches in` `a row, each lasting less` `than 300 seconds.  Giving up.`

`wrapper  |   There may be a configuration problem: please check the logs.`

`wrapper  | <-- Wrapper Stopped`

 

wrapper配置文件中没有指定jdk的安装路径，修改之。

wrapper.java.command=/usr/jdk1.7.0\_67/bin/

 

继续报错：

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

`wrapper  | --> Wrapper Started as Daemon`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | JVM exited while` `loading the application.`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 2    | wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 3    | wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 4    | wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | Reloading Wrapper configuration...`

`wrapper  | Launching a JVM...`

`wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | JVM exited while` `loading the application.`

`jvm 5    | wrapper  | Unable to start JVM: Permission denied (13)`

`wrapper  | There were 5 failed launches in` `a row, each lasting less` `than 300 seconds.  Giving up.`

`wrapper  |   There may be a configuration problem: please check the logs.`

`wrapper  | <-- Wrapper Stopped`

wrapper配置文件中指定java绝对路径，而不是jdk的bin路径。。。修改之。

wrapper.java.command=/usr/jdk1.7.0\_67/bin/java

 

这次ok了。

访问nexus：

[http://192.168.31.123:8088/nexus/](http://192.168.31.123:8088/nexus/)