因?yàn)闃I(yè)務(wù)安全需要等種種原因,不能夠把插件都發(fā)布到公共的npm倉(cāng)庫(kù),所以需要搭建自己的私有npm倉(cāng)庫(kù),最近自己搭建了一個(gè)簡(jiǎn)單的npm倉(cāng)庫(kù),踩了些坑,和大家分享一下,希望能夠幫到有需要的童鞋
本次講述用sinopia從0開(kāi)始搭建npm倉(cāng)庫(kù),講得會(huì)比較細(xì),覺(jué)得啰嗦的童鞋可以只看自己需要的哈
服務(wù)器端
安裝nodejs和npm
因?yàn)槲业膌inux服務(wù)器是centos的,所以我這邊講的是一種在centos上安裝node 環(huán)境
1.安裝wget
yum install -y wget
2.下載最新的node包
可以在nodejs的官網(wǎng)找到最新的下載地址(找到需要的,右鍵復(fù)制鏈接地址就可以拿到最新的地址啦)
wget https://nodejs.org/en/download/node-v10.15.0-linux-x64.tar.xz
解壓安裝包
xz -d node-v10.15.0-linux-x64.tar.xz
tar -xf node-v10.15.0-linux-x64.tar.xz
部署bin文件
重點(diǎn)是要找到你的nodejs的文件路徑(你將node文件解壓到哪里就是哪里。),找不到node路徑的童鞋請(qǐng)執(zhí)行
whereis node
然后執(zhí)行
ln -s node路徑 /usr/bin/node
ln -s node路徑 /usr/bin/npm
eg:
//我的node解壓路徑為/opt/node-v10.15.0-linux-x64/bin/node
ln -s /opt/node-v10.15.0-linux-x64/bin/node /usr/bin/node
ln -s /opt/node-v10.15.0-linux-x64/bin/node /usr/bin/npm
如果出現(xiàn)
ln: failed to create symbolic link ‘/usr/bin/node’: File exists
執(zhí)行:rm /usr/bin/node
查看是否安裝成功
node -v
npm -v
搭建npm倉(cāng)庫(kù)
sinopia安裝
裝好node以后,我們就可以在服務(wù)器直接安裝sinopia了,一行命令全局安裝
npm install -g sinopia
安裝好了可以啟動(dòng)試一下
sinopia
出現(xiàn)下面的結(jié)果說(shuō)明成功跑起來(lái)了,這個(gè)時(shí)候可以打開(kāi)http://localhost:4873/訪(fǎng)問(wèn)了
Sinopia doesn't need superuser privileges. Don't run it under root.
warn --- config file - /root/.config/sinopia/config.yaml
warn --- http address - http://localhost:4873/
如果報(bào)sinopia: command not found,請(qǐng)?zhí)砑雨P(guān)聯(lián),找到你的sinopia路徑(這個(gè)路徑在之前node路徑的子目錄里面)
ln -s /opt/node-v10.15.0-linux-x64/bin/sinopia /usr/bin
pm2
node服務(wù)非常脆弱,一般在實(shí)際中使用都會(huì)配合守護(hù)進(jìn)程。這里我用的是 pm2 做守護(hù)進(jìn)程
安裝
npm install -g pm2
pm2 -v
如果出現(xiàn) pm2: command not found,和sinopia的方法一樣
ln -s /opt/node-v10.15.0-linux-x64/bin/pm2 /usr/bin
通過(guò) PM2 啟動(dòng) sinopia:
pm2 start `which sinopia`
結(jié)果類(lèi)似如下:
┌─────────┬────┬──────┬────────┬────────┬─────┬────────┬───────────┐
│ Name │ id │ mode │ status │ ? │ cpu │ memory │
├─────────┼────┼──────┼────────┼────────┼─────┼────────┼───────────┤
│ sinopia │ 0 │ N/A │ fork │ online │ 0 │ 0% │ 16.7 MB │
└─────────┴────┴──────┴────────┴────────┴─────┴────────┴───────────┘
sinopia配置修改
默認(rèn)情況下,sinopia 的配置是不適合直接使用的,所以我們需要對(duì)它的配置文件按需酌情修改
我們找到上面提到的配置文件目錄,打開(kāi)配置文件進(jìn)行編輯
#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/rlidwka/sinopia/tree/master/conf
#
# path to a directory with all packages
storage: ./storage #npm包存放的路徑
auth:
htpasswd:
file: ./htpasswd #保存用戶(hù)的賬號(hào)密碼等信息
# Maximum amount of users allowed to register, defaults to "+inf".
# You can set this to -1 to disable registration.
max_users: -1 #默認(rèn)為1000,改為-1,禁止注冊(cè)
# a list of other known repositories we can talk to
uplinks:
npmjs:
url: http://registry.npm.taobao.org/ #默認(rèn)為npm的官網(wǎng),由于國(guó)情,修改 url 讓sinopia使用 淘寶的npm鏡像地址
packages: #配置權(quán)限管理
'@*/*':
# scoped packages
access: $all #表示哪一類(lèi)用戶(hù)可以對(duì)匹配的項(xiàng)目進(jìn)行安裝 【$all 表示所有人都可以執(zhí)行對(duì)應(yīng)的操作,$authenticated 表示只有通過(guò)驗(yàn)證的人可以執(zhí)行對(duì)應(yīng)操作,$anonymous 表示只有匿名者可以進(jìn)行對(duì)應(yīng)操作(通常無(wú)用)】
publish: $authenticated #表示哪一類(lèi)用戶(hù)可以對(duì)匹配的項(xiàng)目進(jìn)行發(fā)布
'*':
# allow all users (including non-authenticated users) to read and
# publish all packages
#
# you can specify usernames/groupnames (depending on your auth plugin)
# and three keywords: "$all", "$anonymous", "$authenticated"
access: $all #表示哪一類(lèi)用戶(hù)可以對(duì)匹配的項(xiàng)目進(jìn)行安裝
# allow all known users to publish packages
# (anyone can register by default, remember?)
publish: $authenticated #表示哪一類(lèi)用戶(hù)可以對(duì)匹配的項(xiàng)目進(jìn)行發(fā)布
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs #如其名,這里的值是對(duì)應(yīng)于 uplinks
# log settings
logs:
- {type: stdout, format: pretty, level: http}
#- {type: file, path: sinopia.log, level: info}
# you can specify listen address (or simply a port)
listen: 0.0.0.0:4873 #默認(rèn)沒(méi)有,只能在本機(jī)訪(fǎng)問(wèn),添加后可以通過(guò)外網(wǎng)訪(fǎng)問(wèn)
==listen: 0.0.0.0:4873 這一條一定得加,然后記得把服務(wù)器的4873的端口開(kāi)放==
訪(fǎng)問(wèn)
在自己的電腦瀏覽器上輸入服務(wù)器ip地址+端口號(hào),端口號(hào)默認(rèn)為4873
eg:192.186.1.343:4873
到這里,我們可以成功的在自己的服務(wù)器上搭建我們的私有npm啦~~,喜歡的請(qǐng)贊一下