说明 亲测原稿,系统CentOS 7.3 在一些企业中,内部开发的组件经常在多个项目中使用。车辆千千万,轮子只有一种。为了避免重复造轮子的过程。大多数就把项目发布到npm服务器。但是企业中很多东西不开源,就只有自己动手搭建npm服务器了。
准备
项目地址 :https://github.com/cnpm/cnpmjs.org 需要MySql:MySql安装教程,其他服务器上的MySql也行 需要Node,NPM:Node.js安装教程
防火墙开启端口(服务默认使用7001和7002)
1 2 3 firewall-cmd --zone=public --add-port=7001/tcp --permanent #添加开启端口,要开启7001;7002;端口 systemctl restart firewalld.service #重启防火墙 systemctl stop firewalld #禁用防火墙(防火墙全部关闭)
下载 将软件包下载到服务器自定义路径(例:/www)
1 2 cd /www git clone https://github.com/cnpm/cnpmjs.org.git
直接下载好ftp上传到服务器也行
创建数据库(没有mysql的先搞定sql) 拉取的代码中 docs/db.sql就是建表sql ,自行导入建表。
1 2 3 create database cnpmjs; use cnpmjs; source docs/db.sql;
配置 下载好 cnpmjs.org 进入 cnpmjs.org/config 目录创建 config.js (默认只有一个index.js,创建config.js后会优先使用里面的配置)
1 2 cd /www/cnpmjs.org/config vi config.js
写入以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 module.exports = { debug: false, database: { db: 'cnpmjs', // 数据库名 host: '127.0.0.1', // 服务器地址 port: 3306, // 端口 username: 'testuser', // 用户名 password: '123456', // 密码 dialect: 'mysql' // 使用mysql,默认为sqlite, 还支持postgres,mariadb,暂时不支持oracle }, enablePrivate: true, // 只有管理员可以发布 npm 包,默认为 false,即任何人都可以发布包 admins: { admin: 'admin@thuol.com' // 管理员权限 邮箱 }, scopes: ['@thuol'], // 私有包必须依附于 scope 下 syncModel: 'exist' // 同步已存在的模块, 默认为 none即不同步,exist只会同步已存在的模块,还有个选项为all,同步所有模块(如果设置了同步。硬盘可能被塞满) };
更改index.js中参数 registryHost 更改成你自己服务器ip(或者域名)加相应的端口默认7001端口,否则拉包可能失败(拉包时域名指向不对),约在第162行
1 2 3 4 5 6 7 8 // registry url name //registryHost: 'r.cnpmjs.org', //默认值 registryHost: '192.168.220.128:7001', /** * registry mode config */ `
注释 bindingHost 这行否则其他电脑无法访问 约在第31行
1 2 3 4 5 registryPort: 7001, webPort: 7002, // bindingHost: '127.0.0.1', // only binding on 127.0.0.1 for local access // default is ctx.protocol protocol: '',
cnpm 默认的两个访问端口是:
1) 7001是 registry 端口,对应 registryPort 配置项 2) 7002是 web 端口,对应 webPort 配置项如果config.js中syncModel 设置为none,拉取共有的包需要更改,sourceNpmRegistry参数,约197行
1 2 3 4 5 6 7 8 9 10 // sync source, upstream registry // If you want to directly sync from official npm's registry // please drop them an email first // sourceNpmRegistry: 'https://registry.npm.taobao.org', sourceNpmRegistry: 'http://192.168.220.128:7001', //共有包地址 sourceNpmWeb: 'https://npm.taobao.org', // upstream registry is base on cnpm/cnpmjs.org or not // if your upstream is official npm registry, please turn it off sourceNpmRegistryIsCNpm: true,
安装启动 安装 进入项目目录
1 2 3 cd /www/cnpmjs.org npm install npm start // 启动cnpm服务
如果提示如下错误,先升级npm再安装
关闭 简单粗暴的方法,启动时有进程 PID,直接结束进程
1 2 netstat -antpl #查看端口占用情况,7001和7002是是该程序 kill 3272 #结束掉相应的进程
测试是否启动 浏览器里访问一下加端口7001和7002出现如下,正常启动
上传npm包到私有cnpm服务器 服务器启动成功后,客户端就可以通过其他机器进行npm包的上传了 客户端进入需要发布的项目目录
1 2 3 4 5 6 7 npm login --registry=http://192.168.220.128:7001 #登录npm 使用私有服务器地址 ######### ## 账号:admin ,(之前config.js配置的账号) ## 密码(首次随意输入) ## 邮箱:admin@thuol.com(之前config.js配置的邮箱) ######### npm publish --registry=http://192.168.220.128:7001 #发布npm包到私有服务器
发布成功可以在 域名(ip)+7002端口中查到
从私有cnpm服务器安装npm包 正常使用后面加上npm私服地址
1 npm install myvue --registry=http://192.168.220.128:7001
nrm管理多个源 客户端每次安装私服npm包都需要 带一长串 –registry=http://………………比较麻烦,解决这问题可以使用nrm工具进行管理
1 2 3 4 5 npm i -g nrm #安装nrm nrm add sifu http://192.168.220.128:7001 #添加私服地址 nrm use sifu #设置使用私服地址(sifu) npm install myvue #直接使用安装