Quy trình thiết lập Nexus Repository Manager phiên bản 3.x trên hệ điều hành Ubuntu 16.04 yêu cầu chuẩn bị môi trường Java phù hợp và cấu hình hệ thống để đảm bảo hiệu năng ổn định.
Yêu cầu nền tảng
- Hệ điều hành: Ubuntu 16.04 LTS (64-bit)
- JDK: Phiên bản OpenJDK 8 hoặc Oracle JDK 8 (không hỗ trợ JDK 11 trở lên cho Nexus OSS 3.29.x)
- Bộ nhớ RAM tối thiểu: 4 GB
- Dung lượng đĩa trống: ≥ 10 GB
Tải và giải nén gói Nexus
Truy cập trang chủ Sonatype để tải phiên bản ổn định nhất của Nexus Repository Manager 3 (ví dụ: nexus-3.29.2-02-unix.tar.gz). Sau khi tải xong, chuyển file vào máy chủ Linux và thực hiện:
tar -xzf nexus-3.29.2-02-unix.tar.gz -C /opt/
Đảm bảo thư mục cài đặt thuộc sở hữu của người dùng chạy dịch vụ (ví dụ: nexus):
sudo groupadd --system nexus
sudo useradd --system --gid nexus --shell /bin/false --create-home --home-dir /opt/nexus nexus
sudo chown -R nexus:nexus /opt/nexus-3.29.2-02/ /opt/sonatype-work/
Cấu hình JVM và khởi động thủ công
Mở tập tin cấu hình JVM tại /opt/nexus-3.29.2-02/bin/nexus.vmoptions và xác minh đường dẫn JDK:
-Xms1200M
-Xmx1200M
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UseG1GC
-XX:G1NewSizePercent=20
-XX:G1MaxNewSizePercent=40
-XX:G1OldPLABSize=16
-XX:G1HeapRegionSize=4M
-XX:G1ReservePercent=15
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=/opt/nexus-3.29.2-02
-Dkaraf.base=/opt/nexus-3.29.2-02
-Dkaraf.etc=/opt/nexus-3.29.2-02/etc
-Djava.io.tmpdir=/opt/nexus-3.29.2-02/tmp
-Dkaraf.data=/opt/sonatype-work/nexus3
-Djava.awt.headless=true
-Dfile.encoding=UTF-8
Nếu gặp lỗi liên quan đến biến INSTALL4J_JAVA_HOME, hãy thiết lập rõ ràng trong tập tin /opt/nexus-3.29.2-02/bin/nexus:
INSTALL4J_JAVA_HOME_OVERRIDE="/usr/lib/jvm/java-8-openjdk-amd64"
Kiểm tra trạng thái và khởi động thử nghiệm:
cd /opt/nexus-3.29.2-02/bin
sudo -u nexus ./nexus start
sudo -u nexus ./nexus status
Đầu ra mong đợi: Started Sonatype Nexus OSS 3.29.2-02.
Cấu hình dịch vụ hệ thống (systemd)
Tạo đơn vị dịch vụ tại /etc/systemd/system/nexus.service:
[Unit]
Description=Nexus Repository Manager
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus-3.29.2-02/bin/nexus start
ExecStop=/opt/nexus-3.29.2-02/bin/nexus stop
Restart=on-failure
RestartSec=5
User=nexus
Group=nexus
WorkingDirectory=/opt/nexus-3.29.2-02
[Install]
WantedBy=multi-user.target
Khởi tạo và kích hoạt dịch vụ:
sudo systemctl daemon-reload
sudo systemctl enable nexus
sudo systemctl start nexus
Điều chỉnh giới hạn tài nguyên hệ thống
Chỉnh sửa /etc/security/limits.conf:
nexus soft nofile 65536
nexus hard nofile 65536
nexus soft nproc 65536
nexus hard nproc 65536
Thêm dòng sau vào /etc/pam.d/common-session và /etc/pam.d/common-session-noninteractive:
session required pam_limits.so
Cập nhật tham số systemd toàn cục bằng cách mở /etc/systemd/system.conf và bật hoặc thiết lập:
DefaultLimitNOFILE=65536
Sau đó khởi động lại systemd: sudo systemctl daemon-reload && sudo reboot.
Truy cập giao diện quản trị
Mở trình duyệt và truy cập http://<ip-may-chu>:8081. Lần đầu đăng nhập sẽ yêu cầu nhập mật khẩu quản trị được sinh tự động tại:
cat /opt/sonatype-work/nexus3/admin.password
Tài khoản mặc định là admin; hệ thống yêu cầu đổi mật khẩu ngay sau lần đăng nhập đầu tiên.
Tích hợp với Maven
Cấu hình ~/.m2/settings.xml để sử dụng kho lưu trữ Nexus làm mirror chính:
<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">
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>your-new-admin-password</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>your-new-admin-password</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-public</id>
<name>Nexus Public Mirror</name>
<url>http://192.168.88.84:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>
Triển khai kho Docker riêng (tùy chọn)
Trong giao diện Nexus, tạo kho kiểu docker (hosted) với các thiết lập:
- Name:
docker-private - HTTP Port:
5000 - Allow anonymous docker pull: Tích chọn nếu cần
Cấu hình Docker client trên máy chủ khác bằng cách chỉnh sửa /etc/docker/daemon.json:
{
"insecure-registries": ["192.168.88.84:5000"]
}
Sau đó khởi động lại daemon Docker: sudo systemctl restart docker. Đăng nhập và đẩy ảnh:
docker login 192.168.88.84:5000
docker tag hello-world:latest 192.168.88.84:5000/hello-world:latest
docker push 192.168.88.84:5000/hello-world:latest