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

28次阅读

共计 3273 个字符,预计需要花费 9 分钟才能阅读完成。

因为业务安全需要等种种原因,不能够把插件都发布到公共的 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 啦~~

正文完
 0