從0開始使用sinopia搭建私有npm倉庫
因為業務安全需要等種種原因,不能夠把外掛都發布到公共的npm倉庫,所以需要搭建自己的私有npm倉庫,最近自己搭建了一個簡單的npm倉庫,踩了些坑,和大家分享一下,希望能夠幫到有需要的童鞋
本次講述用sinopia從0開始搭建npm倉庫,講得會比較細,覺得囉嗦的童鞋可以只看自己需要的哈([原文連結[1])
伺服器端
安裝nodejs和npm
因為我的linux伺服器是centos的,所以我這邊講的是一種在centos上安裝node 環境
1.安裝wget
yum install -y wget
2.下載最新的node包
可以在ofollow,noindex" target="_blank">nodejs的官網 找到最新的下載地址(找到需要的,右鍵複製連結地址就可以拿到最新的地址啦)
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檔案
重點是要找到你的nodejs的檔案路徑(你將node檔案解壓到哪裡就是哪裡。),找不到node路徑的童鞋請執行
whereis node
然後執行
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
如果出現
ln: failed to create symbolic link ‘/usr/bin/node’: File exists 執行:rm /usr/bin/node
檢視是否安裝成功
node -v npm -v
搭建npm倉庫
sinopia安裝
裝好node以後,我們就可以在伺服器直接安裝sinopia了,一行命令全域性安裝
npm install -g sinopia
安裝好了可以啟動試一下
sinopia
出現下面的結果說明成功跑起來了,這個時候可以開啟http://localhost :4873/訪問了
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/
如果報sinopia: command not found,請新增關聯,找到你的sinopia路徑(這個路徑在之前node路徑的子目錄裡面)
ln -s /opt/node-v10.15.0-linux-x64/bin/sinopia/usr/bin
pm2
node服務非常脆弱,一般在實際中使用都會配合守護程序。這裡我用的是 pm2 做守護程序
安裝
npm install -g pm2 pm2 -v
如果出現 pm2: command not found,和sinopia的方法一樣
ln -s /opt/node-v10.15.0-linux-x64/bin/pm2 /usr/bin
通過 PM2 啟動 sinopia:
pm2 start `which sinopia` 結果類似如下: ┌─────────┬────┬──────┬────────┬────────┬─────┬────────┬───────────┐ │ Name│ id │ mode │ status │ ↺│ cpu │ memory │ ├─────────┼────┼──────┼────────┼────────┼─────┼────────┼───────────┤ │ sinopia │ 0│ N/A│ fork│ online │ 0│ 0%│ 16.7 MB│ └─────────┴────┴──────┴────────┴────────┴─────┴────────┴───────────┘
sinopia配置修改
預設情況下,sinopia 的配置是不適合直接使用的,所以我們需要對它的配置檔案按需酌情修改
我們找到上面提到的配置檔案目錄,開啟配置檔案進行編輯
# # 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#儲存使用者的賬號密碼等資訊 # Maximum amount of users allowed to register, defaults to "+inf". # You can set this to -1 to disable registration. max_users: -1#預設為1000,改為-1,禁止註冊 # a list of other known repositories we can talk to uplinks: npmjs: url: http://registry.npm.taobao.org/#預設為npm的官網,由於國情,修改 url 讓sinopia使用 淘寶的npm映象地址 packages:#配置許可權管理 '@*/*': # scoped packages access: $all#表示哪一類使用者可以對匹配的專案進行安裝 【$all 表示所有人都可以執行對應的操作,$authenticated 表示只有通過驗證的人可以執行對應操作,$anonymous 表示只有匿名者可以進行對應操作(通常無用)】 publish: $authenticated#表示哪一類使用者可以對匹配的專案進行釋出 '*': # 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#表示哪一類使用者可以對匹配的專案進行安裝 # allow all known users to publish packages # (anyone can register by default, remember?) publish: $authenticated#表示哪一類使用者可以對匹配的專案進行釋出 # if package is not available locally, proxy requests to 'npmjs' registry proxy: npmjs#如其名,這裡的值是對應於 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#預設沒有,只能在本機訪問,新增後可以通過外網訪問
==listen: 0.0.0.0:4873 這一條一定得加,然後記得把伺服器的4873的埠開放==
訪問
在自己的電腦瀏覽器上輸入伺服器ip地址+埠號,埠號預設為4873
eg:192.186.1.343:4873
到這裡,我們可以成功的在自己的伺服器上搭建我們的私有npm啦~~