從0開始使用sinopia搭建私有npm倉庫

因爲業務安全需要等種種原因,不能夠把插件都發布到公共的npm倉庫,所以需要搭建自己的私有npm倉庫,最近自己搭建了一個簡單的npm倉庫,踩了些坑,和大家分享一下,希望能夠幫到有需要的童鞋

本次講述用sinopia從0開始搭建npm倉庫,講得會比較細,覺得囉嗦的童鞋可以只看自己需要的哈([原文鏈接[1])

服務器端

安裝nodejs和npm

因爲我的linux服務器是centos的,所以我這邊講的是一種在centos上安裝node 環境

1.安裝wget

yum install -y wget

2.下載最新的node包

可以在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啦~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章