从0开始使用sinopia搭建私有npm仓库

由于业务安全须要等种种缘由,不可以把插件都发布到公共的npm仓库,因此须要搭建本身的私有npm仓库,最近本身搭建了一个简单的npm仓库,踩了些坑,和你们分享一下,但愿可以帮到有须要的童鞋node

本次讲述用sinopia从0开始搭建npm仓库,讲得会比较细,以为啰嗦的童鞋能够只看本身须要的哈([原文连接[1])linux

服务器端

安装nodejs和npm

由于个人linux服务器是centos的,因此我这边讲的是一种在centos上安装node 环境git

1.安装wget

yum install -y wget

2.下载最新的node包

能够在nodejs的官网找到最新的下载地址(找到须要的,右键复制连接地址就能够拿到最新的地址啦)github

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路径的童鞋请执行npm

whereis node

而后执行centos

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/访问了this

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啦~~

相关文章
相关标签/搜索