原本使用 cnpmjs.org 搭建私有仓储,但经过一段时间的试用,发现其并非理想的平台。
其社区不活跃,长期未更新。扩展性差,需修改源码。配套客户端实现有兼容问题。
本文是在《在 CentOS 7 平台使用 cnpmjs.org 搭建 npm 私有仓储,并集成 gitlab 认证》基础上,基于 verdaccio 搭建。
1. 升级 node.js 和 npm
1 2 3 4 5 6 7 8 |
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - sudo yum -y install nodejs node -v v8.11.3 npm -v 5.6.0 |
2. 安装 node-gyp 和 node-pre-gyp ,部分库在安装时需要编译,提前安装以防万一。
1 2 3 4 5 6 7 |
npm install --global node-gyp node-pre-gyp node-gyp -v v3.7.0 node-pre-gyp -v v0.6.39 |
3. 安装 verdaccio 和 插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# root 用户需添加 unsafe-perm 参数,否则会报错 user "root" does not have permission to access the dev dir # 参考: # https://github.com/nodejs/node-gyp/issues/454#issuecomment-58792114 # https://docs.npmjs.com/misc/config#unsafe-perm npm install --unsafe-perm --global verdaccio verdaccio -V 3.4.1 # 安装 verdaccio-auth-gitlab 支持 gitlab 个人令牌 npm install --global verdaccio-auth-gitlab 启动 verdaccio 以便初始化配置文件 verdaccio |
4. 修改配置文件
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# # Look here for more config file examples: # https://github.com/verdaccio/verdaccio/tree/master/conf # ## 自定义路径 #url_prefix: 'http://npm.xxxxxx.work/' ## 网络代理 #http_proxy: http://127.0.0.1:1234/ #https_proxy: http://127.0.0.1:1234/ #no_proxy: localhost,127.0.0.1 # 存储路径 storage: ./storage # 插件路径 plugins: ./plugins # 监听全部 IP listen: - 0.0.0.0:4873 # web 站点 web: enable: true title: 'xx npm 私储' # 日志 logs: # 控制台 - {type: stdout, format: pretty, level: info} # 文件(在 systemctl 中相对路径会基于 / 解析?) - {type: file, level: warn, path: /root/.config/verdaccio/verdaccio.log} # 鉴权 auth: ## 使用 gitlab 个人令牌 auth-gitlab: # 服务器域名 url: http://git.xxxxxx.work role: # 检查 user 角色 user: true # 检查 group 所有者角色 groupOwner: false # 检查 group 成员角色 groupMember: false # 检查 project 所有者角色 projectOwner: false # 检查 project 成员角色 projectMember: false cache: # 最大缓存数量 maxCount: 1000 # 最长缓存时间 maxSecond: 300 ## 使用本地注册用户 #htpasswd: # file: ./htpasswd # # 最大注册用户数 -1 表示不允许注册 # #max_users: 1000 # 中间件 middlewares: # npm 审计 audit: enabled: true ## 发布包 #publish: # # 允许离线推送 # allow_offline: true # 上游仓储(多仓储同时使用会影响性能) uplinks: # 官方仓储 npmjs: url: https://registry.npmjs.org/ ## 缓存公共包 #cache: false ## 使用身份认证 #auth: # type: bearer # # process.env['NPM_TOKEN'] # token_env: true # # npm 令牌 # token: '' ## 使用自定义头 #headers: # authorization: '' ## yarn 镜像 #yarnpkg: # url: https://registry.yarnpkg.com/ ## 淘宝镜像 #taonpm: # url: https://registry.npm.taobao.org/ packages: # 私有包 '@xxxxxx/*': # 允许所有用户访问 access: $all # 允许 gitlab 用户发布 publish: $gitlab:user # 公共包 '@*/*': # 允许所有用户访问 access: $all ## 慎用覆盖公共包 #publish: $authenticated # 若本地不存在则请求上游仓储 proxy: npmjs '**': access: $all #publish: $authenticated proxy: npmjs |
5. 添加防火墙例外
1 2 |
# 添加 4873 端口 firewall-cmd --zone=public --add-port=4873/tcp --permanent |
6. 开机启动(推荐使用 pm2 管理,可避免手动配置启动项)
1 2 3 4 5 6 7 8 9 10 11 12 |
# 创建文件 /usr/lib/systemd/system/verdaccio.service [Unit] Description=verdaccio After=network.target [Service] Type=simple User=root Environment=NODE_PATH=/usr/lib/node_modules ExecStart=/usr/bin/verdaccio PrivateTmp=true [Install] WantedBy=multi-user.target |
1 2 |
# 启用服务 systemctl enable verdaccio.service |
7. 配置 nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
upstream npm { server 127.0.0.1:1234; } server { listen 80; server_name npm.xxxxxx.work; access_log /data/logs/nginx/npm.access.log main; error_log /data/logs/nginx/npm.error.log; location / { proxy_pass http://npm; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; } } |
8. 备份私有包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/bin/sh # verdaccio 配置目录 var_workDir=/root/.config/verdaccio # 备份目录 var_backupDir=${var_workDir}/backup # storage 来源目录 var_sourceDir=${var_workDir}/storage # storage 目标目录 var_targetDir=${var_backupDir}/storage # 创建 storage 目标目录 mkdir -p ${var_targetDir} # 私有包 db rsync -a ${var_sourceDir}/.sinopia-db.json ${var_targetDir} # 私有包 @xxxxxx/* rsync -a ${var_sourceDir}/@xxxxxx ${var_targetDir} |
1 2 |
# 修改备份脚本执行权限 chmod a+x /root/.config/verdaccio/backup/backup.sh |
1 2 3 4 5 |
# 编辑定时任务文件 /var/spool/cron/root crontab -e # 每天 00:00 执行备份 0 0 * * * /root/.config/verdaccio/backup/backup.sh |
《使用 verdaccio 搭建 npm 私有仓储,并集成 gitlab 认证》上有1条评论